Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Console;
4
 
5
use Illuminate\Console\Command;
6
use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Routing\RouteCollection;
9
use Symfony\Component\Console\Attribute\AsCommand;
10
 
11
#[AsCommand(name: 'route:cache')]
12
class RouteCacheCommand extends Command
13
{
14
    /**
15
     * The console command name.
16
     *
17
     * @var string
18
     */
19
    protected $name = 'route:cache';
20
 
21
    /**
22
     * The name of the console command.
23
     *
24
     * This name is used to identify the command during lazy loading.
25
     *
26
     * @var string|null
27
     *
28
     * @deprecated
29
     */
30
    protected static $defaultName = 'route:cache';
31
 
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Create a route cache file for faster route registration';
38
 
39
    /**
40
     * The filesystem instance.
41
     *
42
     * @var \Illuminate\Filesystem\Filesystem
43
     */
44
    protected $files;
45
 
46
    /**
47
     * Create a new route command instance.
48
     *
49
     * @param  \Illuminate\Filesystem\Filesystem  $files
50
     * @return void
51
     */
52
    public function __construct(Filesystem $files)
53
    {
54
        parent::__construct();
55
 
56
        $this->files = $files;
57
    }
58
 
59
    /**
60
     * Execute the console command.
61
     *
62
     * @return void
63
     */
64
    public function handle()
65
    {
66
        $this->callSilent('route:clear');
67
 
68
        $routes = $this->getFreshApplicationRoutes();
69
 
70
        if (count($routes) === 0) {
71
            return $this->components->error("Your application doesn't have any routes.");
72
        }
73
 
74
        foreach ($routes as $route) {
75
            $route->prepareForSerialization();
76
        }
77
 
78
        $this->files->put(
79
            $this->laravel->getCachedRoutesPath(), $this->buildRouteCacheFile($routes)
80
        );
81
 
82
        $this->components->info('Routes cached successfully.');
83
    }
84
 
85
    /**
86
     * Boot a fresh copy of the application and get the routes.
87
     *
88
     * @return \Illuminate\Routing\RouteCollection
89
     */
90
    protected function getFreshApplicationRoutes()
91
    {
92
        return tap($this->getFreshApplication()['router']->getRoutes(), function ($routes) {
93
            $routes->refreshNameLookups();
94
            $routes->refreshActionLookups();
95
        });
96
    }
97
 
98
    /**
99
     * Get a fresh application instance.
100
     *
101
     * @return \Illuminate\Contracts\Foundation\Application
102
     */
103
    protected function getFreshApplication()
104
    {
105
        return tap(require $this->laravel->bootstrapPath().'/app.php', function ($app) {
106
            $app->make(ConsoleKernelContract::class)->bootstrap();
107
        });
108
    }
109
 
110
    /**
111
     * Build the route cache file.
112
     *
113
     * @param  \Illuminate\Routing\RouteCollection  $routes
114
     * @return string
115
     */
116
    protected function buildRouteCacheFile(RouteCollection $routes)
117
    {
118
        $stub = $this->files->get(__DIR__.'/stubs/routes.stub');
119
 
120
        return str_replace('{{routes}}', var_export($routes->compile(), true), $stub);
121
    }
122
}