Additional implementation proposal to the optimization commands #52971
-
Good morning, My proposal is simple, I would like to be able to extend the commands that run in Personally I believe that this functionality could be useful in order to improve the deployment process I thought of various ways to implement this functionality (obviously if it's something you think could be useful) The best implementation that comes to my mind without compromising the performance of Laravel in any way is the following creating the AsOptimize attribute, something like namespace Illuminate\Console\Attribute;
#[\Attribute(\Attribute::TARGET_CLASS)]
class AsOptimize
{
public function __construct(
public string $name,
public bool $clear = false,
) { }
} Assignment of the attribute to currently existing commands namespace Illuminate\Foundation\Console;
/* ... */
use Illuminate\Console\Attribute\AsOptimize;
#[AsCommand(name: 'route:cache')]
#[AsOptimize(name: 'routes')]
class RouteCacheCommand extends Command { /* ... */ } namespace Illuminate\Foundation\Console;
/* ... */
use Illuminate\Console\Attribute\AsOptimize;
#[AsCommand(name: 'route:clear')]
#[AsOptimize(name: 'routes', clear: true)]
class RouteClearCommand extends Command { /* ... */ } Finally modify the two commands, like: #[AsCommand(name: 'optimize:clear')]
class OptimizeClearCommand extends Command
{
/* ... */
public function handle()
{
$this->components->info('Caching framework bootstrap, configuration, and metadata.');
// get all command
$commands = collect($this->getApplication()->all())
// get first attribute of tyle AsOptimize if exist
->mapWithKeys(fn($command, $name) => [$name => collect((new \ReflectionClass($command))->getAttributes(AsOptimize::class))->first()])
// filter only command with AsOptimize attribute
->filter(fn(?ReflectionAttribute $attribute) => $attribute !== null)
// get only commands registered for clear
->filter(fn(ReflectionAttribute $attribute) => ($attribute->getArguments()['clear']) === true)
// prepare tesks
->mapWithKeys(fn(ReflectionAttribute $attribute, $command) => [
$attribute->getArguments()['name'] => fn() => $this->callSilent($command) == 0
]);
$commands->each(fn($task, $description) => $this->components->task($description, $task));
$this->newLine();
}
} If you think something like this would be useful, of course I propose to develop it myself tell me what you think or if you have any suggestions |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I have prepared the imprementation I have in mind GianfriAur@14e1b5f |
Beta Was this translation helpful? Give feedback.
-
I actually had a similar idea in this PR, so yeah, I like the idea quite much 👍🏻 |
Beta Was this translation helpful? Give feedback.
I actually had a similar idea in this PR, so yeah, I like the idea quite much 👍🏻