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

getDefaultCachedRoutePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace RichanFongdasen\I18n\Traits;
4
5
use Illuminate\Support\Facades\Log;
6
use RichanFongdasen\I18n\I18nService;
7
8
/**
9
 * LoadsTranslatedCachedRoutes.
10
 *
11
 * Add this trait to your App\RouteServiceProvider to load
12
 * translated cached routes for the active locale, instead
13
 * of the default locale's routes (irrespective of active).
14
 */
15
trait LoadsTranslatedCachedRoutes
16
{
17
    /**
18
     * Load the cached routes for the application.
19
     *
20
     * @return void
21
     */
22
    protected function loadCachedRoutes()
23
    {
24
        $localization = $this->getI18nService();
25
26
        $locale = $localization->routePrefix();
27
28
        // First, try to load the routes specifically cached for this locale
29
        // if they do not exist, write a warning to the log and load the default
30
        // routes instead. Note that this is guaranteed to exist, because the
31
        // 'cached routes' check in the Application checks its existence.
32
33
        $path = $this->makeLocaleRoutesPath($locale);
34
35
        if (!file_exists($path)) {
36
            Log::warning("Routes cached, but no cached routes found for locale '{$locale}'!");
37
38
            $path = $this->getDefaultCachedRoutePath();
39
        }
40
41
        $this->app->booted(function () use ($path) {
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
            require $path;
43
        });
44
    }
45
46
    /**
47
     * Returns the path to the cached routes file for a given locale.
48
     *
49
     * @param string|null $locale
50
     *
51
     * @return string
52
     */
53 View Code Duplication
    protected function makeLocaleRoutesPath($locale = null)
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...
54
    {
55
        $path = $this->getDefaultCachedRoutePath();
56
        if ($locale === null) {
57
            return $path;
58
        }
59
60
        return substr($path, 0, -4).'_'.$locale.'.php';
61
    }
62
63
    /**
64
     * Returns the path to the standard cached routes file.
65
     *
66
     * @return string
67
     */
68
    protected function getDefaultCachedRoutePath()
69
    {
70
        return $this->app->getCachedRoutesPath();
71
    }
72
73
    /**
74
     * @return \RichanFongdasen\I18n\I18nService
75
     */
76
    protected function getI18nService()
77
    {
78
        return app(I18nService::class);
79
    }
80
}
81