PageRenderTime 65ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteCacheCommand.php

https://bitbucket.org/Temesky/resource-management-system-backend
PHP | 98 lines | 62 code | 10 blank | 26 comment | 2 complexity | cd06244e352d91015b581d9b964a9e0e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Illuminate\Foundation\Console;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Filesystem\Filesystem;
  5. use Illuminate\Routing\RouteCollection;
  6. use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
  7. class RouteCacheCommand extends Command
  8. {
  9. /**
  10. * The console command name.
  11. *
  12. * @var string
  13. */
  14. protected $name = 'route:cache';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Create a route cache file for faster route registration';
  21. /**
  22. * The filesystem instance.
  23. *
  24. * @var \Illuminate\Filesystem\Filesystem
  25. */
  26. protected $files;
  27. /**
  28. * Create a new route command instance.
  29. *
  30. * @param \Illuminate\Filesystem\Filesystem $files
  31. * @return void
  32. */
  33. public function __construct(Filesystem $files)
  34. {
  35. parent::__construct();
  36. $this->files = $files;
  37. }
  38. /**
  39. * Execute the console command.
  40. *
  41. * @return void
  42. */
  43. public function fire()
  44. {
  45. $this->call('route:clear');
  46. $routes = $this->getFreshApplicationRoutes();
  47. if (count($routes) == 0) {
  48. return $this->error("Your application doesn't have any routes.");
  49. }
  50. foreach ($routes as $route) {
  51. $route->prepareForSerialization();
  52. }
  53. $this->files->put(
  54. $this->laravel->getCachedRoutesPath(), $this->buildRouteCacheFile($routes)
  55. );
  56. $this->info('Routes cached successfully!');
  57. }
  58. /**
  59. * Boot a fresh copy of the application and get the routes.
  60. *
  61. * @return \Illuminate\Routing\RouteCollection
  62. */
  63. protected function getFreshApplicationRoutes()
  64. {
  65. $app = require $this->laravel->bootstrapPath().'/app.php';
  66. $app->make(ConsoleKernelContract::class)->bootstrap();
  67. return $app['router']->getRoutes();
  68. }
  69. /**
  70. * Build the route cache file.
  71. *
  72. * @param \Illuminate\Routing\RouteCollection $routes
  73. * @return string
  74. */
  75. protected function buildRouteCacheFile(RouteCollection $routes)
  76. {
  77. $stub = $this->files->get(__DIR__.'/stubs/routes.stub');
  78. return str_replace('{{routes}}', base64_encode(serialize($routes)), $stub);
  79. }
  80. }