|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CaribouFute\LocaleRoute\Routing; |
|
4
|
|
|
|
|
5
|
|
|
use CaribouFute\LocaleRoute\LocaleConfig; |
|
6
|
|
|
use CaribouFute\LocaleRoute\Prefix\Route as PrefixRoute; |
|
7
|
|
|
use CaribouFute\LocaleRoute\Prefix\Url as PrefixUrl; |
|
8
|
|
|
use Closure; |
|
9
|
|
|
use Illuminate\Foundation\Application; |
|
10
|
|
|
use Illuminate\Routing\Route; |
|
11
|
|
|
|
|
12
|
|
|
class LocaleRouter |
|
13
|
|
|
{ |
|
14
|
|
|
protected $localeConfig; |
|
15
|
|
|
protected $router; |
|
16
|
|
|
protected $prefixRoute; |
|
17
|
|
|
protected $prefixUrl; |
|
18
|
|
|
protected $app; |
|
19
|
|
|
|
|
20
|
53 |
|
public function __construct( |
|
21
|
|
|
LocaleConfig $localeConfig, |
|
22
|
|
|
Router $router, |
|
23
|
|
|
PrefixRoute $prefixRoute, |
|
24
|
|
|
PrefixUrl $prefixUrl, |
|
25
|
|
|
Application $app |
|
26
|
|
|
) { |
|
27
|
53 |
|
$this->localeConfig = $localeConfig; |
|
28
|
53 |
|
$this->router = $router; |
|
29
|
53 |
|
$this->prefixRoute = $prefixRoute; |
|
30
|
53 |
|
$this->prefixUrl = $prefixUrl; |
|
31
|
53 |
|
$this->app = $app; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
public function any($route, $action, $options = []): RouteCollection |
|
35
|
|
|
{ |
|
36
|
1 |
|
return $this->makeRoutes('any', $route, $action, $options); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
21 |
|
public function get($route, $action, $options = []): RouteCollection |
|
40
|
|
|
{ |
|
41
|
21 |
|
return $this->makeRoutes('get', $route, $action, $options); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
public function post($route, $action, $options = []): RouteCollection |
|
45
|
|
|
{ |
|
46
|
1 |
|
return $this->makeRoutes('post', $route, $action, $options); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function put($route, $action, $options = []): RouteCollection |
|
50
|
|
|
{ |
|
51
|
1 |
|
return $this->makeRoutes('put', $route, $action, $options); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
public function patch($route, $action, $options = []): RouteCollection |
|
55
|
|
|
{ |
|
56
|
1 |
|
return $this->makeRoutes('patch', $route, $action, $options); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function delete($route, $action, $options = []): RouteCollection |
|
60
|
|
|
{ |
|
61
|
1 |
|
return $this->makeRoutes('delete', $route, $action, $options); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
public function options($route, $action, $options = []): RouteCollection |
|
65
|
|
|
{ |
|
66
|
1 |
|
return $this->makeRoutes('options', $route, $action, $options); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
27 |
|
public function makeRoutes($method, $route, $action, $options = []): RouteCollection |
|
70
|
|
|
{ |
|
71
|
27 |
|
$options = is_string($options) ? $this->convertOptionUrlsToArray($options) : $options; |
|
72
|
27 |
|
$routeCollection = new RouteCollection(); |
|
73
|
|
|
|
|
74
|
27 |
|
foreach ($this->localeConfig->locales($options) as $locale) { |
|
75
|
27 |
|
$routeObject = $this->makeRoute($locale, $method, $route, $action, $options); |
|
76
|
27 |
|
$routeCollection->add($routeObject); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
27 |
|
return $routeCollection; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
8 |
|
protected function convertOptionUrlsToArray($options): array |
|
83
|
|
|
{ |
|
84
|
8 |
|
$newOptions = []; |
|
85
|
|
|
|
|
86
|
8 |
|
foreach ($this->localeConfig->locales() as $locale) { |
|
87
|
8 |
|
$newOptions[$locale] = $options; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
8 |
|
return $newOptions; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
27 |
|
public function makeRoute($locale, $method, $route, $action, $options = []): Route |
|
94
|
|
|
{ |
|
95
|
27 |
|
$url = $this->prefixUrl->rawRouteUrl($locale, $route, $options); |
|
96
|
|
|
|
|
97
|
27 |
|
$action = $this->convertToControllerAction($action); |
|
98
|
27 |
|
$action = $this->fillAction($locale, $route, $action, $options); |
|
99
|
27 |
|
$middleware = $options['middleware'] ?? []; |
|
100
|
|
|
|
|
101
|
27 |
|
return $this->router->$method($url, $action)->middleware($middleware); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
27 |
|
protected function convertToControllerAction($action) |
|
105
|
|
|
{ |
|
106
|
27 |
|
if (is_array($action) && count($action) >= 2) { |
|
107
|
1 |
|
return ['uses' => $action[0] . '@' . $action[1]]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
26 |
|
return is_string($action) || is_a($action, Closure::class) ? |
|
111
|
26 |
|
['uses' => $action] : |
|
112
|
26 |
|
$action; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
27 |
|
protected function fillAction($locale, $route, $action, $options): array |
|
116
|
|
|
{ |
|
117
|
27 |
|
$action['locale'] = $locale; |
|
118
|
27 |
|
$action['as'] = $route; |
|
119
|
|
|
|
|
120
|
27 |
|
if (isset($options['add_locale_to_url'])) { |
|
121
|
1 |
|
$action['add_locale_to_url'] = $options['add_locale_to_url']; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
27 |
|
return $action; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
6 |
|
public function resource($route, $controller, $options = []) |
|
128
|
|
|
{ |
|
129
|
6 |
|
$registrar = $this->app->make(ResourceRegistrar::class); |
|
130
|
6 |
|
$registrar->register($route, $controller, $options); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
3 |
|
public function apiResource($route, $controller, array $options = []) |
|
134
|
|
|
{ |
|
135
|
3 |
|
$only = ['index', 'show', 'store', 'update', 'destroy']; |
|
136
|
|
|
|
|
137
|
3 |
|
if (isset($options['except'])) { |
|
138
|
1 |
|
$only = array_diff($only, (array) $options['except']); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
3 |
|
$this->resource( |
|
142
|
3 |
|
$route, |
|
143
|
3 |
|
$controller, |
|
144
|
3 |
|
array_merge([ |
|
145
|
3 |
|
'only' => $only, |
|
146
|
3 |
|
], $options) |
|
147
|
3 |
|
); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|