forge/app/Console/Commands/UploadAssetsCommand.php

55 lines
1.6 KiB
PHP
Raw Normal View History

2024-06-06 13:18:48 -04:00
<?php
2025-01-30 00:23:55 -05:00
declare(strict_types=1);
2024-06-06 13:18:48 -04:00
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
class UploadAssetsCommand extends Command
2024-06-06 13:18:48 -04:00
{
protected $signature = 'app:upload-assets';
2024-06-19 14:03:36 -04:00
protected $description = 'Uploads assets to Cloudflare R2';
2024-06-06 13:18:48 -04:00
/**
* This command uploads the Vite build assets to Cloudflare R2. Typically, this will be run after the assets have
* been built and the application is ready to deploy from within the production environment build process.
2024-06-06 13:18:48 -04:00
*/
public function handle(): void
2024-06-06 13:18:48 -04:00
{
2024-06-19 14:03:36 -04:00
$this->publishBuildAssets();
$this->publishVendorAssets();
}
protected function publishBuildAssets(): void
{
$this->info('Publishing build assets...');
2024-06-06 13:18:48 -04:00
2024-06-19 14:03:36 -04:00
$assets = File::allFiles(public_path('/build'));
foreach ($assets as $asset) {
2024-06-06 13:18:48 -04:00
$buildDir = 'build/'.$asset->getRelativePathname();
$this->info('Uploading asset to: '.$buildDir);
Storage::disk('r2')->put($buildDir, $asset->getContents());
}
2024-06-19 14:03:36 -04:00
$this->info('Build assets published successfully');
}
protected function publishVendorAssets(): void
{
$this->info('Publishing vendor assets...');
$assets = File::allFiles(public_path('/vendor'));
foreach ($assets as $asset) {
$buildDir = 'vendor/'.$asset->getRelativePathname();
$this->info('Uploading asset to: '.$buildDir);
Storage::disk('r2')->put($buildDir, $asset->getContents());
}
$this->info('Build assets published successfully');
2024-06-06 13:18:48 -04:00
}
}