79 lines
2.3 KiB
PHP
79 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
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
|
|
*/
|
|
#[\Override]
|
|
protected $signature = 'uwil:theme:switch {theme : The theme folder name on theme-merge project}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
#[\Override]
|
|
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 (): int | string => search(
|
|
label: 'Search for a theme:',
|
|
options: fn (string $value): array => \collect(File::directories(\resource_path('theme')))
|
|
->map(fn (string $dir_path): string => \pathinfo($dir_path, \PATHINFO_BASENAME))
|
|
->filter(fn (string $dir_path) => $value ? Str::of($dir_path)->contains($value) : true)
|
|
->filter(fn (string $dir_path): bool => $dir_path !== 'mounted')
|
|
->all(),
|
|
placeholder: 'default-theme'
|
|
),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
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');
|
|
}
|
|
}
|