|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ChinLeung\LaravelMultilingualRoutes; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Routing\Route; |
|
6
|
|
|
use Illuminate\Routing\RouteCollection; |
|
7
|
|
|
use Illuminate\Routing\Router; |
|
8
|
|
|
use Illuminate\Support\Arr; |
|
9
|
|
|
|
|
10
|
|
|
class MultilingualRegistrar |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Constructor of the class. |
|
14
|
|
|
* |
|
15
|
|
|
* @param \Illuminate\Routing\Router $router |
|
16
|
|
|
* @return void |
|
|
|
|
|
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct(Router $router) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->router = $router; |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Register the routes. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $key |
|
27
|
|
|
* @param mixed $handle |
|
28
|
|
|
* @param array $locales |
|
29
|
|
|
* @param array $options |
|
30
|
|
|
* @return \Illuminate\Routing\RouteCollection |
|
31
|
|
|
*/ |
|
32
|
|
|
public function register(string $key, $handle, array $locales, array $options) : RouteCollection |
|
33
|
|
|
{ |
|
34
|
|
|
$collection = new RouteCollection; |
|
35
|
|
|
|
|
36
|
|
|
$method = $this->getRequestMethodFromOptions($options); |
|
37
|
|
|
|
|
38
|
|
|
foreach ($locales as $locale) { |
|
39
|
|
|
$collection->add( |
|
40
|
|
|
$this |
|
41
|
|
|
->registerRoute($method, $key, $handle, $locale) |
|
42
|
|
|
->name($this->generateNameForLocaleFromOptions($locale, $key, $options)) |
|
43
|
|
|
->prefix($this->generatePrefixForLocale($locale)) |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $collection; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Register a single route. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $method |
|
54
|
|
|
* @param string $key |
|
55
|
|
|
* @param mixed $handle |
|
56
|
|
|
* @param string $locale |
|
57
|
|
|
* @return \Illuminate\Routing\Route |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function registerRoute(string $method, string $key, $handle, string $locale) : Route |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->router->{strtolower($method)}( |
|
62
|
|
|
trans("routes.$key", [], $locale), |
|
63
|
|
|
$handle |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Retrieve the request method from the options. |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $options |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function getRequestMethodFromOptions(array $options) : string |
|
74
|
|
|
{ |
|
75
|
|
|
return $options['method'] ?? 'get'; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Generate the name of the route based on the options. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $locale |
|
82
|
|
|
* @param string $key |
|
83
|
|
|
* @param array $options |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function generateNameForLocaleFromOptions(string $locale, string $key, array $options) : string |
|
87
|
|
|
{ |
|
88
|
|
|
if ($name = Arr::get($options, "names.$locale")) { |
|
89
|
|
|
return "$locale.$name"; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return sprintf( |
|
93
|
|
|
'%s.%s', |
|
94
|
|
|
$locale, |
|
95
|
|
|
Arr::get($options, 'name', $key) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Generate the prefix of the route based on the options. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $locale |
|
103
|
|
|
* @return string|null |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function generatePrefixForLocale(string $locale) : ?string |
|
106
|
|
|
{ |
|
107
|
|
|
if ($locale != config('app.fallback_locale')) { |
|
108
|
|
|
return $locale; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return config('laravel-multilingual-routes.prefix_fallback') |
|
112
|
|
|
? $locale |
|
113
|
|
|
: null; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.