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

LoadsTranslatedCachedRoutes::loadCachedRoutes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 2
nc 2
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
     * Catch your Http Request.
19
     *
20
     * @var Illuminate\Http\Request
21
     */
22
    protected $request;
23
24
    /**
25
     * Load the cached routes for the application.
26
     *
27
     * @return void
28
     */
29
    protected function loadCachedRoutes()
30
    {
31
        $localization = $this->getI18nService();
32
33
        $locale = $localization->routePrefix();
34
35
        // First, try to load the routes specifically cached for this locale
36
        // if they do not exist, write a warning to the log and load the default
37
        // routes instead. Note that this is guaranteed to exist, because the
38
        // 'cached routes' check in the Application checks its existence.
39
40
        $path = $this->makeLocaleRoutesPath($locale);
41
42
        if (!file_exists($path)) {
43
            Log::warning("Routes cached, but no cached routes found for locale '{$locale}'!");
44
45
            $path = $this->getDefaultCachedRoutePath();
46
        }
47
48
        $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...
49
            require $path;
50
        });
51
    }
52
53
    /**
54
     * Returns the path to the cached routes file for a given locale.
55
     *
56
     * @param string|null $locale
57
     *
58
     * @return string
59
     */
60 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...
61
    {
62
        $path = $this->getDefaultCachedRoutePath();
63
        if ($locale === null) {
64
            return $path;
65
        }
66
67
        return substr($path, 0, -4).'_'.$locale.'.php';
68
    }
69
70
    /**
71
     * Returns the path to the standard cached routes file.
72
     *
73
     * @return string
74
     */
75
    protected function getDefaultCachedRoutePath()
76
    {
77
        return $this->app->getCachedRoutesPath();
78
    }
79
80
    /**
81
     * @return \RichanFongdasen\I18n\I18nService
82
     */
83
    protected function getI18nService()
84
    {
85
        return new I18nService($this->request);
86
    }
87
}
88