generated from filamentphp/plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hook the routes cache command to core artisan commands
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |