Skip to content

Commit

Permalink
Hook the routes cache command to core artisan commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Voltra authored Feb 15, 2024
1 parent 095ba9f commit 2ad06f2
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
14 changes: 14 additions & 0 deletions config/filament-fabricator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@

'enable-view-page' => false,

/**
* Whether to hook into artisan's core commands to clear and refresh page route caches along with the rest.
* Disable for manual control over cache.
*
* This is the list of commands that will be hooked into:
* - cache:clear -> clear routes cache
* - config:cache -> refresh routes cache
* - config:clear -> clear routes cache
* - optimize -> refresh routes cache
* - optimize:clear -> clear routes cache
* - route:clear -> clear routes cache
*/
'hook-to-commands' => true,

/*
* This is the name of the table that will be created by the migration and
* used by the above page-model shipped with this package.
Expand Down
7 changes: 7 additions & 0 deletions src/FilamentFabricatorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Z3d0X\FilamentFabricator;

use Illuminate\Console\Events\CommandFinished;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use ReflectionClass;
Expand All @@ -12,6 +14,7 @@
use Symfony\Component\Finder\SplFileInfo;
use Z3d0X\FilamentFabricator\Facades\FilamentFabricator;
use Z3d0X\FilamentFabricator\Layouts\Layout;
use Z3d0X\FilamentFabricator\Listeners\OptimizeWithLaravel;
use Z3d0X\FilamentFabricator\Observers\PageRoutesObserver;
use Z3d0X\FilamentFabricator\PageBlocks\PageBlock;
use Z3d0X\FilamentFabricator\Services\PageRoutesService;
Expand Down Expand Up @@ -103,6 +106,10 @@ public function packageBooted()
parent::packageBooted();

FilamentFabricator::getPageModel()::observe(PageRoutesObserver::class);

if ((bool) config('filament-fabricator.hook-to-commands')) {
Event::listen(CommandFinished::class, OptimizeWithLaravel::class);
}
}

protected function registerComponentsFromDirectory(string $baseClass, array $register, ?string $directory, ?string $namespace): void
Expand Down
66 changes: 66 additions & 0 deletions src/Listeners/OptimizeWithLaravel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Z3d0X\FilamentFabricator\Listeners;

use Illuminate\Console\Command;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Support\Facades\Artisan;
use Z3d0X\FilamentFabricator\Commands\ClearRoutesCacheCommand;

class OptimizeWithLaravel
{
const COMMANDS = [
'cache:clear',
'config:cache',
'config:clear',
'optimize',
'optimize:clear',
'route:clear',
];

const REFRESH_COMMANDS = [
'config:cache',
'optimize',
];

public function handle(CommandFinished $event): void
{
if (! $this->shouldHandleEvent($event)) {
return;
}

if ($this->shouldRefresh($event)) {
$this->refresh();
} else {
$this->clear();
}
}

public function shouldHandleEvent(CommandFinished $event)
{
return $event->exitCode === Command::SUCCESS
&& in_array($event->command, static::COMMANDS);
}

public function shouldRefresh(CommandFinished $event)
{
return in_array($event->command, static::REFRESH_COMMANDS);
}

public function refresh()
{
$this->callCommand([
'--refresh' => true,
]);
}

public function clear()
{
$this->callCommand();
}

public function callCommand(array $params = [])
{
Artisan::call(ClearRoutesCacheCommand::class, $params);
}
}

0 comments on commit 2ad06f2

Please sign in to comment.