1
|
|
|
<?php namespace App\Services\Routing; |
2
|
|
|
|
3
|
|
|
use Illuminate\Container\Container; |
4
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
5
|
|
|
use Illuminate\Routing\ControllerInspector; |
6
|
|
|
use Illuminate\Routing\Router; |
7
|
|
|
|
8
|
|
|
class LocalizedRouter extends Router |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Create a new Router instance. |
12
|
|
|
* |
13
|
|
|
* @param \Illuminate\Contracts\Events\Dispatcher $events |
14
|
|
|
* @param \Illuminate\Container\Container $container |
15
|
|
|
*/ |
16
|
19 |
|
public function __construct(Dispatcher $events, Container $container = null) |
17
|
|
|
{ |
18
|
19 |
|
parent::__construct($events, $container); |
19
|
|
|
|
20
|
19 |
|
$this->routes = new LocalizedRouteCollection; |
21
|
19 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Add a route to the underlying route collection. |
25
|
|
|
* |
26
|
|
|
* @param array|string $methods |
27
|
|
|
* @param string $uri |
28
|
|
|
* @param \Closure|array|string $action |
29
|
|
|
* |
30
|
|
|
* @return \Illuminate\Routing\Route |
31
|
|
|
*/ |
32
|
19 |
|
protected function addRoute($methods, $uri, $action) |
33
|
|
|
{ |
34
|
|
|
// Now we apply our Localization modifications. |
35
|
19 |
|
$uri = $this->localizeUris($uri); |
36
|
|
|
|
37
|
19 |
|
return parent::addRoute($methods, $uri, $action); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Route a controller to a URI with wildcard routing. |
42
|
|
|
* |
43
|
|
|
* @param string $uri |
44
|
|
|
* @param string $controller |
45
|
|
|
* @param array $names |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
* |
49
|
|
|
* @deprecated since version 5.2. |
50
|
|
|
*/ |
51
|
|
|
public function controller($uri, $controller, $names = []) |
52
|
|
|
{ |
53
|
|
|
$prepended = $controller; |
54
|
|
|
|
55
|
|
|
// First, we will check to see if a controller prefix has been registered in |
56
|
|
|
// the route group. If it has, we will need to prefix it before trying to |
57
|
|
|
// reflect into the class instance and pull out the method for routing. |
58
|
|
|
if (!empty($this->groupStack)) { |
59
|
|
|
$prepended = $this->prependGroupUses($controller); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$controllerInspector = new ControllerInspector; |
63
|
|
|
$routable = $controllerInspector->getRoutable($prepended, $uri); |
64
|
|
|
|
65
|
|
|
// Now we apply our Localization modifications. |
66
|
|
|
foreach ($routable as &$routes) { |
67
|
|
|
foreach ($routes as &$route) { |
68
|
|
|
$route['plain'] = $this->localizeUris($route['plain']); |
69
|
|
|
unset($route); |
70
|
|
|
} |
71
|
|
|
unset($routes); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// When a controller is routed using this method, we use Reflection to parse |
75
|
|
|
// out all of the routable methods for the controller, then register each |
76
|
|
|
// route explicitly for the developers, so reverse routing is possible. |
77
|
|
|
foreach ($routable as $method => $routes) { |
78
|
|
|
foreach ($routes as $route) { |
79
|
|
|
$this->registerInspected($route, $controller, $method, $names); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->addFallthroughRoute($controller, $uri); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Route a resource to a controller. |
88
|
|
|
* |
89
|
|
|
* @param string $name |
90
|
|
|
* @param string $controller |
91
|
|
|
* @param array $options |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
19 |
|
public function resource($name, $controller, array $options = []) |
96
|
|
|
{ |
97
|
19 |
|
if ($this->container && $this->container->bound(LocalizedResourceRegistrar::class)) { |
98
|
|
|
$registrar = $this->container->make(LocalizedResourceRegistrar::class); |
99
|
|
|
} else { |
100
|
19 |
|
$registrar = new LocalizedResourceRegistrar($this); |
101
|
|
|
} |
102
|
|
|
|
103
|
19 |
|
$registrar->register($name, $controller, $options); |
104
|
19 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Add a fallthrough route for a controller. |
108
|
|
|
* |
109
|
|
|
* @param string $controller |
110
|
|
|
* @param string $uri |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
* |
114
|
|
|
* @deprecated since version 5.2. |
115
|
|
|
*/ |
116
|
|
|
protected function addFallthroughRoute($controller, $uri) |
117
|
|
|
{ |
118
|
|
|
$localizedUri = app('translator')->get('routes.' . $uri . '.'); |
119
|
|
|
if (false !== strpos($localizedUri, '.')) { |
120
|
|
|
$localizedUri = $uri; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
parent::addFallthroughRoute($controller, $localizedUri); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $uri |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
19 |
|
private function localizeUris($uri) |
132
|
|
|
{ |
133
|
19 |
|
$uriExploded = explode('/', trim($uri, '/')); |
134
|
19 |
|
$localizedUriTranslationBitParts = []; |
135
|
19 |
|
while (list($level, $bitName) = each($uriExploded)) { |
136
|
19 |
|
if ($level == 0) { |
137
|
19 |
|
$localizedUriTranslationBitParts[$level] = 'routes.' . $bitName . '.'; |
138
|
|
|
} else { |
139
|
19 |
|
$localizedUriTranslationBitParts[$level] = trim($localizedUriTranslationBitParts[$level - 1], '.') . '.' . $bitName; |
140
|
|
|
} |
141
|
|
|
} |
142
|
19 |
|
foreach ($localizedUriTranslationBitParts as $level => &$translationBitPart) { |
143
|
19 |
|
$phraseToGetTranslationFor = $translationBitPart; |
144
|
19 |
|
if (preg_match('#(?<!routes)\.\{[^\}]+\}\.#', $translationBitPart)) { // For lower-level paths, in order not to hit 'routes.' index. |
145
|
|
|
$phraseToGetTranslationFor = preg_replace('#\{[^\}]+\}\.?#', '', $translationBitPart); |
146
|
|
|
} |
147
|
19 |
|
$translatedPhrase = app('translator')->get($phraseToGetTranslationFor); |
148
|
19 |
|
if (false !== strpos($translatedPhrase, '.')) { |
149
|
19 |
|
$translationBitPart = $uriExploded[$level]; |
150
|
|
|
} else { |
151
|
19 |
|
$translationBitPart = $translatedPhrase; |
152
|
|
|
} |
153
|
19 |
|
unset($translationBitPart); // Delete the reference (won't delete the original). |
154
|
|
|
} |
155
|
|
|
|
156
|
19 |
|
return implode('/', $localizedUriTranslationBitParts); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: