Add artisan clear-cache-all command

This commit is contained in:
Afiq Hamzah
2024-08-09 18:25:35 +08:00
parent facf829563
commit 6e71be384e
2 changed files with 80 additions and 0 deletions
+78
View File
@@ -0,0 +1,78 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
class ClearCachesAll extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'clear-caches-all';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Clear all caches in the application';
/**
* Execute the console command.
*/
public function handle()
{
Artisan::call('cache:clear');
// Clear configuration cache
if (file_exists(base_path('bootstrap/cache/config.php'))) {
@unlink(base_path('bootstrap/cache/config.php'));
}
// Clear event cache
if (file_exists(base_path('bootstrap/cache/events.php'))) {
@unlink(base_path('bootstrap/cache/events.php'));
}
// Clear route cache
if (file_exists(base_path('bootstrap/cache/routes.php'))) {
@unlink(base_path('bootstrap/cache/routes.php'));
}
// Clear view cache
$views = glob(storage_path('framework/views/*.php'));
foreach ($views as $view) {
@unlink($view);
}
// Clear compiled caches
$paths = [
base_path('vendor/autoload.php'),
base_path('bootstrap/cache/compiled.php'),
base_path('bootstrap/cache/services.php'),
];
foreach ($paths as $path) {
if (file_exists($path)) {
@unlink($path);
}
}
// Run `composer install`
$output = [];
$returnVar = null;
exec('composer install 2>&1', $output, $returnVar);
// Output the result of the Composer command
if ($returnVar === 0) {
$this->info("Composer install ran successfully:\n" . implode("\n", $output));
} else {
$this->error("Composer install failed:\n" . implode("\n", $output));
}
$this->info("All caches have been cleared.");
}
}
+2
View File
@@ -19,6 +19,8 @@ class Kernel extends ConsoleKernel
Commands\DailyGadai::class,
Commands\MigrationOnePercent::class,
Commands\CawanganDebugConnections::class,
Commands\ClearSessions::class,
Commands\ClearCachesAll::class,
];