b2in/app/Console/Commands/PublishFluxAssets.php

84 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use RuntimeException;
class PublishFluxAssets extends Command
{
protected $signature = 'flux:publish-assets {--force : Overwrite existing published Flux assets}';
protected $description = 'Publish Flux JavaScript assets to public/flux for static delivery (like livewire:publish --assets)';
public function handle(): int
{
$sourceDir = $this->resolveSourceDirectory();
$targetDir = public_path('flux');
if (! File::isDirectory($targetDir)) {
File::makeDirectory($targetDir, 0755, true);
}
$files = [
'flux.js',
'flux.min.js',
'editor.js',
'editor.min.js',
'editor.css',
];
$published = 0;
foreach ($files as $file) {
$source = $sourceDir.'/'.$file;
if (! is_file($source)) {
continue;
}
$target = $targetDir.'/'.$file;
if (is_file($target) && ! $this->option('force') && hash_file('sha256', $source) === hash_file('sha256', $target)) {
$this->line(" <comment>•</comment> {$file} (unverändert)");
continue;
}
File::copy($source, $target);
$this->line(" <info>✓</info> {$file}");
$published++;
}
if ($published === 0) {
$this->info('Flux-Assets sind bereits aktuell.');
return self::SUCCESS;
}
$this->newLine();
$this->info("{$published} Flux-Asset(s) nach public/flux/ veröffentlicht.");
return self::SUCCESS;
}
private function resolveSourceDirectory(): string
{
$proDist = base_path('vendor/livewire/flux-pro/dist');
if (is_dir($proDist)) {
return $proDist;
}
$freeDist = base_path('vendor/livewire/flux/dist');
if (is_dir($freeDist)) {
return $freeDist;
}
throw new RuntimeException('Flux vendor assets not found. Run composer install first.');
}
}