ldc-gen-game-web/app/Console/Commands/Theme/SwitchCommand.php
2023-12-18 18:41:58 +01:00

77 lines
2.2 KiB
PHP

<?php
namespace App\Console\Commands\Theme;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use function Laravel\Prompts\search;
class SwitchCommand extends Command implements PromptsForMissingInput
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'uwil:theme:switch {theme : The theme folder name on theme-merge project}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Switch GGW theme and install it';
/**
* Prompt for missing input arguments using the returned questions.
*
* @return array
*/
protected function promptForMissingArgumentsUsing()
{
return [
'theme' => fn () => search(
label: 'Search for a theme:',
placeholder: 'default-theme',
options: fn (string $value): array => \collect(File::directories(\resource_path('theme')))
->map(fn (string $dir_path) => \pathinfo($dir_path, \PATHINFO_BASENAME))
->filter(fn (string $dir_path) => $value ? Str::of($dir_path)->contains($value) : true)
->filter(fn (string $dir_path) => $dir_path !== 'mounted')
->all()
)
];
}
/**
* Execute the console command.
*
* @return void
*/
public function handle(): void
{
$mount_path = \resource_path('theme/mounted');
/** @var string */
$theme = $this->argument('theme');
if (File::exists($mount_path)) {
\throw_if(
empty(ToolboxHelper::runConsoleCommand("readlink {$mount_path}", false)),
"{$mount_path} is not a link, cannot remove it"
);
\throw_if(
!File::delete($mount_path),
"{$mount_path} Failed to remove file"
);
}
File::link(\resource_path("theme/{$theme}"), $mount_path);
Artisan::call('optimize:clear');
}
}