GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 54405e...4eecae )
by Marc
02:12 queued 24s
created

cacheRoutesPerLocale()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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