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

buildRouteCacheFile()   A

Complexity

Conditions 1
Paths 1

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 1
nc 1
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
        $key = $this->getLocaleEnvKey();
96
        if (null !== $locale) {
97
            putenv("{$key}={$locale}");
98
        }
99
        $app = require $this->getBootstrapPath().'/app.php';
100
        $app->make(Kernel::class)->bootstrap();
101
        $routes = $app['router']->getRoutes();
102
        putenv("{$key}");
103
104
        return $routes;
105
    }
106
107
    /**
108
     * Build the route cache file.
109
     *
110
     * @param \Illuminate\Routing\RouteCollection $routes
111
     *
112
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
113
     *
114
     * @return string
115
     */
116
    protected function buildRouteCacheFile(RouteCollection $routes)
117
    {
118
        $stub = $this->files->get(
119
            realpath(
120
                __DIR__
121
                .DIRECTORY_SEPARATOR.'..'
122
                .DIRECTORY_SEPARATOR.'..'
123
                .DIRECTORY_SEPARATOR.'stubs'
124
                .DIRECTORY_SEPARATOR.'routes.stub'
125
            )
126
        );
127
128
        return str_replace(
129
            [
130
                '{{routes}}',
131
            ],
132
            [
133
                base64_encode(serialize($routes)),
134
            ],
135
            $stub
136
        );
137
    }
138
}
139