Completed
Pull Request — master (#16)
by
unknown
01:14
created

getFreshApplicationRoutes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace RichanFongdasen\I18n\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Contracts\Console\Kernel;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Routing\RouteCollection;
9
use RichanFongdasen\I18n\Traits\TranslatedRouteCommandContext;
10
11
class RouteTranslationsCacheCommand extends Command
12
{
13
    use TranslatedRouteCommandContext;
14
15
    /**
16
     * @var string
17
     */
18
    protected $name = 'route:trans:cache';
19
20
    /**
21
     * @var string
22
     */
23
    protected $description = 'Create a route cache file for faster route registration for all locales';
24
25
    /**
26
     * The filesystem instance.
27
     *
28
     * @var Filesystem
29
     */
30
    protected $files;
31
32
    /**
33
     * Create a new route command instance.
34
     *
35
     * @param Filesystem $files
36
     */
37
    public function __construct(Filesystem $files)
38
    {
39
        parent::__construct();
40
41
        $this->files = $files;
42
    }
43
44
    /**
45
     * Execute the console command.
46
     */
47
    public function handle()
48
    {
49
        $this->call('route:trans:clear');
50
51
        $this->cacheRoutesPerLocale();
52
53
        $this->info('Routes cached successfully for all locales!');
54
    }
55
56
    /**
57
     * Cache the routes separately for each locale.
58
     */
59
    protected function cacheRoutesPerLocale()
60
    {
61
        // Store the default routes cache,
62
        // this way the Application will detect that routes are cached.
63
        $allLocales = array_keys($this->getSupportedLocales());
64
65
        array_push($allLocales, null);
66
67
        foreach ($allLocales as $locale) {
68
            $routes = $this->getFreshApplicationRoutes($locale);
69
70
            if (count($routes) == 0) {
71
                $this->error("Your application doesn't have any routes.");
72
73
                return;
74
            }
75
76
            foreach ($routes as $route) {
77
                $route->prepareForSerialization();
78
            }
79
80
            $this->files->put(
81
                $this->makeLocaleRoutesPath($locale), $this->buildRouteCacheFile($routes)
82
            );
83
        }
84
    }
85
86
    /**
87
     * Boot a fresh copy of the application and get the routes.
88
     *
89
     * @param string|null $locale
90
     *
91
     * @return \Illuminate\Routing\RouteCollection
92
     */
93
    protected function getFreshApplicationRoutes($locale = null)
94
    {
95
        $app = require $this->getBootstrapPath().'/app.php';
96
97
        if (null !== $locale) {
98
            $key = $this->getLocaleEnvKey();
99
100
            putenv("{$key}={$locale}");
101
102
            $app->make(Kernel::class)->bootstrap();
103
104
            $routes = $app['router']->getRoutes();
105
106
            putenv("{$key}");
107
108
            return $routes;
109
        }
110
111
        $app->make(Kernel::class)->bootstrap();
112
113
        return $app['router']->getRoutes();
114
    }
115
116
    /**
117
     * Build the route cache file.
118
     *
119
     * @param \Illuminate\Routing\RouteCollection $routes
120
     *
121
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
122
     *
123
     * @return string
124
     */
125
    protected function buildRouteCacheFile(RouteCollection $routes)
126
    {
127
        $stub = $this->files->get(
128
            realpath(
129
                __DIR__
130
                .DIRECTORY_SEPARATOR.'..'
131
                .DIRECTORY_SEPARATOR.'..'
132
                .DIRECTORY_SEPARATOR.'stubs'
133
                .DIRECTORY_SEPARATOR.'routes.stub'
134
            )
135
        );
136
137
        return str_replace(
138
            [
139
                '{{routes}}',
140
            ],
141
            [
142
                base64_encode(serialize($routes)),
143
            ],
144
            $stub
145
        );
146
    }
147
}
148