Skip to content

Commit

Permalink
Add customization optimization commands
Browse files Browse the repository at this point in the history
  • Loading branch information
GianfriAur committed Oct 1, 2024
1 parent 39a60f1 commit 14e1b5f
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Cache/Console/ClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace Illuminate\Cache\Console;

use Illuminate\Cache\CacheManager;
use Illuminate\Console\Attributes\AsOptimize;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'cache:clear')]
#[AsOptimize(name: 'cache', clear: true)]
class ClearCommand extends Command
{
/**
Expand Down
21 changes: 21 additions & 0 deletions src/Illuminate/Console/Attributes/AsOptimize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Illuminate\Console\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class AsOptimize
{

/**
* Create a new class instance.
*
* @param string $name
* @param bool $clear
*/
public function __construct(public string $name, public bool $clear = false)
{
}

}
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Console/ClearCompiledCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Attributes\AsOptimize;
use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'clear-compiled')]
#[AsOptimize(name: 'compiled', clear: true)]
class ClearCompiledCommand extends Command
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Console/ConfigCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Attributes\AsOptimize;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
use Illuminate\Filesystem\Filesystem;
Expand All @@ -10,6 +11,7 @@
use Throwable;

#[AsCommand(name: 'config:cache')]
#[AsOptimize(name: 'config')]
class ConfigCacheCommand extends Command
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Console/ConfigClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Attributes\AsOptimize;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'config:clear')]
#[AsOptimize(name: 'config', clear: true)]
class ConfigClearCommand extends Command
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Console/EventCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Attributes\AsOptimize;
use Illuminate\Console\Command;
use Illuminate\Foundation\Support\Providers\EventServiceProvider;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'event:cache')]
#[AsOptimize(name: 'events')]
class EventCacheCommand extends Command
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Console/EventClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Attributes\AsOptimize;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'event:clear')]
#[AsOptimize(name: 'events', clear: true)]
class EventClearCommand extends Command
{
/**
Expand Down
19 changes: 11 additions & 8 deletions src/Illuminate/Foundation/Console/OptimizeClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Attributes\AsOptimize;
use Illuminate\Console\Command;
use ReflectionAttribute;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'optimize:clear')]
Expand Down Expand Up @@ -31,14 +33,15 @@ public function handle()
{
$this->components->info('Clearing cached bootstrap files.');

collect([
'cache' => fn () => $this->callSilent('cache:clear') == 0,
'compiled' => fn () => $this->callSilent('clear-compiled') == 0,
'config' => fn () => $this->callSilent('config:clear') == 0,
'events' => fn () => $this->callSilent('event:clear') == 0,
'routes' => fn () => $this->callSilent('route:clear') == 0,
'views' => fn () => $this->callSilent('view:clear') == 0,
])->each(fn ($task, $description) => $this->components->task($description, $task));
$commands = collect($this->getApplication()->all())
->mapWithKeys(fn($command, $name) => [$name => collect((new \ReflectionClass($command))->getAttributes(AsOptimize::class))->first()])
->filter(fn(?ReflectionAttribute $attribute) => $attribute !== null)
->filter(fn(ReflectionAttribute $attribute) => ($attribute->getArguments()['clear']) === true)
->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();
}
Expand Down
17 changes: 11 additions & 6 deletions src/Illuminate/Foundation/Console/OptimizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Attributes\AsOptimize;
use Illuminate\Console\Command;
use ReflectionAttribute;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'optimize')]
Expand Down Expand Up @@ -31,12 +33,15 @@ public function handle()
{
$this->components->info('Caching framework bootstrap, configuration, and metadata.');

collect([
'config' => fn () => $this->callSilent('config:cache') == 0,
'events' => fn () => $this->callSilent('event:cache') == 0,
'routes' => fn () => $this->callSilent('route:cache') == 0,
'views' => fn () => $this->callSilent('view:cache') == 0,
])->each(fn ($task, $description) => $this->components->task($description, $task));
$commands = collect($this->getApplication()->all())
->mapWithKeys(fn($command, $name) => [$name => collect((new \ReflectionClass($command))->getAttributes(AsOptimize::class))->first()])
->filter(fn(?ReflectionAttribute $attribute) => $attribute !== null)
->filter(fn(ReflectionAttribute $attribute) => ($attribute->getArguments()['clear']) === false)
->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();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Console/RouteCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Attributes\AsOptimize;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Routing\RouteCollection;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'route:cache')]
#[AsOptimize(name: 'routes')]
class RouteCacheCommand extends Command
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Console/RouteClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Attributes\AsOptimize;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'route:clear')]
#[AsOptimize(name: 'routes', clear: true)]
class RouteClearCommand extends Command
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Console/ViewCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Attributes\AsOptimize;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Symfony\Component\Console\Attribute\AsCommand;
Expand All @@ -10,6 +11,7 @@
use Symfony\Component\Finder\SplFileInfo;

#[AsCommand(name: 'view:cache')]
#[AsOptimize(name: 'views')]
class ViewCacheCommand extends Command
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Console/ViewClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Attributes\AsOptimize;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use RuntimeException;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'view:clear')]
#[AsOptimize(name: 'views', clear: true)]
class ViewClearCommand extends Command
{
/**
Expand Down

0 comments on commit 14e1b5f

Please sign in to comment.