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

RouteTranslationsCacheCommand::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 View Code Duplication
    protected function cacheRoutesPerLocale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61
        // Store the default routes cache,
62
        // this way the Application will detect that routes are cached.
63
        $allLocales = $this->getSupportedLocales();
64
65
        array_push($allLocales, null);
66
67
        foreach ($allLocales as $locale) {
68
69
            $routes = $this->getFreshApplicationRoutes($locale);
70
71
            if (count($routes) == 0) {
72
                $this->error("Your application doesn't have any routes.");
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
     * @return \Illuminate\Routing\RouteCollection
91
     */
92
    protected function getFreshApplicationRoutes($locale = null)
93
    {
94
        $app = require $this->getBootstrapPath() . '/app.php';
95
96
        if (null !== $locale) {
97
98
            $default = config('i18n.fallback_language');
99
100
            config(['i18n.fallback_language' => $locale]);
101
102
            $app->make(Kernel::class)->bootstrap();
103
104
            config(['i18n.fallback_language' => $default]);
105
106
        } else {
107
108
            $app->make(Kernel::class)->bootstrap();
109
        }
110
111
        return $app['router']->getRoutes();
112
    }
113
114
    /**
115
     * Build the route cache file.
116
     *
117
     * @param  \Illuminate\Routing\RouteCollection $routes
118
     * @return string
119
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
120
     */
121 View Code Duplication
    protected function buildRouteCacheFile(RouteCollection $routes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
    {
123
        $stub = $this->files->get(
124
            realpath(
125
                __DIR__
126
                . DIRECTORY_SEPARATOR . '..'
127
                . DIRECTORY_SEPARATOR . '..'
128
                . DIRECTORY_SEPARATOR . '..'
129
                . DIRECTORY_SEPARATOR . 'stubs'
130
                . DIRECTORY_SEPARATOR . 'routes.stub'
131
            )
132
        );
133
134
        return str_replace(
135
            [
136
                '{{routes}}',
137
            ],
138
            [
139
                base64_encode(serialize($routes))
140
            ],
141
            $stub
142
        );
143
    }
144
}
145