1
|
|
|
<?php namespace App\Services\Routing; |
2
|
|
|
|
3
|
|
|
use Illuminate\Routing\Route; |
4
|
|
|
use Illuminate\Routing\RouteCollection; |
5
|
|
|
|
6
|
|
|
class LocalizedRouteCollection extends RouteCollection |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* {@inheritdoc} |
10
|
|
|
*/ |
11
|
15 |
|
protected function addLookups($route) |
12
|
|
|
{ |
13
|
15 |
|
$locale = app('translator')->getLocale(); |
14
|
|
|
|
15
|
|
|
// If the route has a name, we will add it to the name look-up table so that we |
16
|
|
|
// will quickly be able to find any route associate with a name and not have |
17
|
|
|
// to iterate through every route every time we need to perform a look-up. |
18
|
15 |
|
$action = $route->getAction(); |
19
|
|
|
|
20
|
15 |
|
if (isset($action['as'])) { |
21
|
15 |
|
$this->nameList[$action['as']][$locale] = $route; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
// When the route is routing to a controller we will also store the action that |
25
|
|
|
// is used by the route. This will let us reverse route to controllers while |
26
|
|
|
// processing a request and easily generate URLs to the given controllers. |
27
|
15 |
|
if (isset($action['controller'])) { |
28
|
15 |
|
$this->addToActionList($action, $route); |
29
|
|
|
} |
30
|
15 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function getByName($name) |
36
|
|
|
{ |
37
|
|
|
$locale = app('translator')->getLocale(); |
38
|
|
|
if (is_array($this->nameList[$name])) { // array with localized routes |
39
|
|
|
return $this->nameList[$name][$locale]; |
40
|
|
|
} elseif ($this->nameList[$name] instanceof Route) { // no localization available, so it'll be a route object |
41
|
|
|
return $this->nameList[$name]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function getByAction($action) |
51
|
|
|
{ |
52
|
|
|
$locale = app('translator')->getLocale(); |
53
|
|
|
if (is_array($this->actionList[$action])) { // array with localized routes |
54
|
|
|
return $this->actionList[$action][$locale]; |
55
|
|
|
} elseif ($this->actionList[$action] instanceof Route) { // no localization available, so it'll be a route object |
56
|
|
|
return $this->actionList[$action]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
15 |
|
public function refreshNameLookups() |
66
|
|
|
{ |
67
|
15 |
|
$this->nameList = []; |
68
|
|
|
|
69
|
|
|
/** @var \Illuminate\Routing\Route $route */ |
70
|
15 |
|
foreach ($this->allRoutes as $route) { |
71
|
15 |
|
if ($route->getName()) { |
72
|
15 |
|
if (isset($route->getAction()['locale'])) { |
73
|
15 |
|
$this->nameList[$route->getName()][$route->getAction()['locale']] = $route; |
74
|
|
|
} else { |
75
|
15 |
|
$this->nameList[$route->getName()] = $route; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
15 |
|
} |
80
|
|
|
} |
81
|
|
|
|