Files
rspade_system/app/RSpade/Commands/Restricted/Optimize_Cache_Command.php
root f6fac6c4bc Fix bin/publish: copy docs.dist from project root
Fix bin/publish: use correct .env path for rspade_system
Fix bin/publish script: prevent grep exit code 1 from terminating script

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-21 02:08:33 +00:00

162 lines
5.3 KiB
PHP
Executable File

<?php
namespace App\RSpade\Commands\Restricted;
use Illuminate\Console\Command;
class Optimize_Cache_Command extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'optimize:cache';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Cache all cacheable Laravel components (config, routes, views, events)';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// Check if we're in production mode
if (!app()->environment('production')) {
throw new \RuntimeException(
"The optimize:cache command should NEVER be called except in production.\n\n" .
"In development mode, all caching should be disabled for proper development workflow.\n" .
"Caching in development prevents you from seeing changes immediately and causes confusion.\n\n" .
"Current environment: " . app()->environment() . "\n\n" .
"If you need to test caching behavior, use a staging environment configured as 'production'."
);
}
$this->info('🚀 Optimizing application caches for production...');
$this->newLine();
// First clear all existing caches to start fresh
$this->info('Clearing existing caches...');
$this->call('cache:clear');
$this->newLine();
$steps = [
'config' => 'Configuration',
'routes' => 'Routes',
'views' => 'Views',
'events' => 'Events',
];
$failed = [];
// For each cache type, we need to remove the override temporarily
// and call the original Laravel command
foreach ($steps as $cache_type => $label) {
$this->info("Caching {$label}...");
try {
$exit_code = $this->cache_component($cache_type);
if ($exit_code === 0) {
$this->line("{$label} cached successfully");
} else {
$failed[] = $label;
$this->error(" ✗ Failed to cache {$label}");
}
} catch (\Exception $e) {
$failed[] = $label;
$this->error(" ✗ Error caching {$label}: " . $e->getMessage());
}
$this->newLine();
}
if (empty($failed)) {
$this->info('✅ All caches optimized successfully!');
return 0;
} else {
$this->error('⚠️ Some caches failed: ' . implode(', ', $failed));
return 1;
}
}
/**
* Cache a specific component
*
* @param string $type
* @return int
*/
protected function cache_component($type)
{
// Temporarily remove the override and call the original command
$app = app();
$command_name = '';
$original_class = '';
switch ($type) {
case 'config':
$command_name = 'command.config.cache';
$original_class = \Illuminate\Foundation\Console\ConfigCacheCommand::class;
break;
case 'routes':
$command_name = 'command.route.cache';
$original_class = \Illuminate\Foundation\Console\RouteCacheCommand::class;
break;
case 'views':
$command_name = 'command.view.cache';
$original_class = \Illuminate\Foundation\Console\ViewCacheCommand::class;
break;
case 'events':
$command_name = 'command.event.cache';
$original_class = \Illuminate\Foundation\Console\EventCacheCommand::class;
break;
default:
throw new \Exception("Unknown cache type: {$type}");
}
// Store the current override
$override = $app->bound($command_name) ? $app->make($command_name) : null;
// Temporarily bind the original Laravel command
$app->singleton($command_name, function () use ($original_class, $type) {
switch ($type) {
case 'config':
case 'routes':
return new $original_class(app('files'));
case 'views':
return new $original_class(app('files'), app('view'));
case 'events':
return new $original_class();
default:
throw new \Exception("Cannot create command for type: {$type}");
}
});
// Call the command using the correct artisan command name
$artisan_command = str_replace('command.', '', $command_name);
$artisan_command = str_replace('.', ':', $artisan_command);
$result = $this->call($artisan_command);
// Restore the override if it existed
if ($override) {
$app->singleton($command_name, function () use ($override) {
return $override;
});
}
return $result;
}
}