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; } }