OptimizeApiPlatformCommand.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace Webkul\BagistoApi\Console\Commands;
  3. use Illuminate\Console\Command;
  4. class OptimizeApiPlatformCommand extends Command
  5. {
  6. protected $signature = 'bagisto-api-platform:optimize';
  7. protected $description = 'Full deploy optimization for the API: clears stale caches, then rebuilds the config + route caches and pre-warms the API Platform metadata cache. Run this after every deploy, package update, or endpoint change so no request pays the per-request route rebuild or the cold-start metadata build.';
  8. public function handle(): int
  9. {
  10. $this->components->info('Optimizing the Bagisto API (full optimize + metadata caches)...');
  11. $this->call('optimize:clear');
  12. $this->call('bagisto-api-platform:clear-cache');
  13. $this->call('optimize');
  14. if (! app()->routesAreCached()) {
  15. $this->components->error('The route cache was not built. Without it, API Platform re-registers every route on every request (~0.8s slower per call). Check for a route that cannot be serialized (a closure or a duplicate route name), then re-run this command before deploying.');
  16. return self::FAILURE;
  17. }
  18. $this->call('bagisto-api-platform:warm-cache');
  19. $this->components->info('Bagisto API optimized. Config, events, routes and views are cached and the metadata cache is warm.');
  20. if (config('app.debug')) {
  21. $this->newLine();
  22. $this->components->warn('APP_DEBUG is currently true. For faster responses set APP_DEBUG=false in your .env (debug mode adds error-collector overhead to every request), then re-run this command.');
  23. }
  24. return self::SUCCESS;
  25. }
  26. }