1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CaribouFute\LocaleRoute\Routing; |
4
|
|
|
|
5
|
|
|
use CaribouFute\LocaleRoute\Prefix\Route as PrefixRoute; |
6
|
|
|
use CaribouFute\LocaleRoute\Prefix\Url as PrefixUrl; |
7
|
|
|
use CaribouFute\LocaleRoute\Routing\RouteCollection; |
8
|
|
|
use Illuminate\Contracts\Routing\Registrar as IlluminateRouter; |
9
|
|
|
use Illuminate\Routing\Route; |
10
|
|
|
|
11
|
|
|
class Router |
12
|
|
|
{ |
13
|
|
|
protected $router; |
14
|
|
|
protected $url; |
15
|
|
|
protected $route; |
16
|
|
|
|
17
|
68 |
|
public function __construct(IlluminateRouter $router, PrefixUrl $url, PrefixRoute $route) |
18
|
|
|
{ |
19
|
68 |
|
$this->router = $router; |
20
|
68 |
|
$this->url = $url; |
21
|
68 |
|
$this->route = $route; |
22
|
68 |
|
} |
23
|
|
|
|
24
|
21 |
|
public function get($uri, $action = []) |
25
|
|
|
{ |
26
|
21 |
|
return $this->makeRoute('get', $uri, $action); |
27
|
|
|
} |
28
|
|
|
|
29
|
3 |
|
public function post($uri, $action = []) |
30
|
|
|
{ |
31
|
3 |
|
return $this->makeRoute('post', $uri, $action); |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
public function put($uri, $action = []) |
35
|
|
|
{ |
36
|
3 |
|
return $this->makeRoute('put', $uri, $action); |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
public function patch($uri, $action = []) |
40
|
|
|
{ |
41
|
3 |
|
return $this->makeRoute('patch', $uri, $action); |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
public function delete($uri, $action = []) |
45
|
|
|
{ |
46
|
3 |
|
return $this->makeRoute('delete', $uri, $action); |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
public function options($uri, $action = []) |
50
|
|
|
{ |
51
|
3 |
|
return $this->makeRoute('options', $uri, $action); |
52
|
|
|
} |
53
|
|
|
|
54
|
36 |
|
public function makeRoute($method, $uri, $action = []) |
55
|
|
|
{ |
56
|
36 |
|
$route = $this->router->$method($uri, $action); |
57
|
36 |
|
$route = $this->addLocale($route, $action); |
58
|
36 |
|
$this->refreshRoutes(); |
59
|
|
|
|
60
|
36 |
|
return $route; |
61
|
|
|
} |
62
|
|
|
|
63
|
32 |
|
public function addLocale(Route $route, $action = []) |
64
|
|
|
{ |
65
|
32 |
|
$locale = $this->getActionLocale($route); |
66
|
|
|
|
67
|
32 |
|
if (!$locale) { |
68
|
1 |
|
return $route; |
69
|
|
|
} |
70
|
|
|
|
71
|
31 |
|
$route = $this->switchRouteLocale($locale, $route); |
72
|
31 |
|
$route = $this->switchUrlLocale($locale, $route, $action); |
73
|
|
|
|
74
|
31 |
|
$route->middleware($this->makeSetSessionLocale($locale)); |
75
|
|
|
|
76
|
31 |
|
return $route; |
77
|
|
|
} |
78
|
|
|
|
79
|
33 |
|
public function getActionLocale(Route $route) |
80
|
|
|
{ |
81
|
33 |
|
$locales = $this->getActionLocales($route); |
82
|
|
|
|
83
|
33 |
|
return is_array($locales) ? last($locales) : $locales; |
84
|
|
|
} |
85
|
|
|
|
86
|
35 |
|
public function getActionLocales(Route $route) |
87
|
|
|
{ |
88
|
35 |
|
$action = $route->getAction(); |
89
|
|
|
|
90
|
35 |
|
return is_array($action) && isset($action['locale']) ? $action['locale'] : []; |
91
|
|
|
} |
92
|
|
|
|
93
|
32 |
|
public function switchRouteLocale($locale, Route $route) |
94
|
|
|
{ |
95
|
32 |
|
$name = $route->getName(); |
96
|
32 |
|
if (!$name) { |
97
|
1 |
|
return $route; |
98
|
|
|
} |
99
|
|
|
|
100
|
31 |
|
$name = $this->route->switchLocale($locale, $name); |
101
|
31 |
|
$route = $this->setRouteName($name, $route); |
102
|
|
|
|
103
|
31 |
|
return $route; |
104
|
|
|
} |
105
|
|
|
|
106
|
31 |
|
public function setRouteName($name, Route $route) |
107
|
|
|
{ |
108
|
31 |
|
$action = $route->getAction(); |
109
|
31 |
|
$action['as'] = $name; |
110
|
31 |
|
$route->setAction($action); |
111
|
|
|
|
112
|
31 |
|
return $route; |
113
|
|
|
} |
114
|
|
|
|
115
|
31 |
|
public function switchUrlLocale($locale, Route $route, array $action = []) |
116
|
|
|
{ |
117
|
31 |
|
$uri = $this->url->switchLocale($locale, $route->uri(), $action); |
118
|
31 |
|
$route->setUri($uri); |
119
|
|
|
|
120
|
31 |
|
return $route; |
121
|
|
|
} |
122
|
|
|
|
123
|
31 |
|
public function makeSetSessionLocale($locale) |
124
|
|
|
{ |
125
|
31 |
|
return 'locale.session:' . $locale; |
126
|
|
|
} |
127
|
|
|
|
128
|
30 |
|
public function refreshRoutes() |
129
|
|
|
{ |
130
|
30 |
|
$routeCollection = new RouteCollection; |
131
|
30 |
|
$routeCollection->hydrate($this->router->getRoutes()); |
132
|
30 |
|
$routeCollection->refresh(); |
133
|
30 |
|
$this->router->setRoutes($routeCollection); |
134
|
30 |
|
} |
135
|
|
|
|
136
|
5 |
|
public function __call($method, $arguments) |
137
|
|
|
{ |
138
|
5 |
|
return call_user_func_array([$this->router, $method], $arguments); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|