1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CaribouFute\LocaleRoute\Routing; |
4
|
|
|
|
5
|
|
|
use CaribouFute\LocaleRoute\LocaleConfig; |
6
|
|
|
use Illuminate\Routing\ResourceRegistrar as IlluminateResourceRegistrar; |
7
|
|
|
use Illuminate\Routing\Router as IlluminateRouter; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Illuminate\Translation\Translator; |
10
|
|
|
|
11
|
|
|
class ResourceRegistrar extends IlluminateResourceRegistrar |
12
|
|
|
{ |
13
|
|
|
protected $localeConfig; |
14
|
|
|
protected $localeRouter; |
15
|
|
|
protected $translator; |
16
|
|
|
|
17
|
6 |
|
public function __construct( |
18
|
|
|
LocaleConfig $localeConfig, |
19
|
|
|
LocaleRouter $localeRouter, |
20
|
|
|
IlluminateRouter $router, |
21
|
|
|
Translator $translator |
22
|
|
|
) { |
23
|
6 |
|
$this->localeConfig = $localeConfig; |
24
|
6 |
|
$this->localeRouter = $localeRouter; |
25
|
6 |
|
$this->translator = $translator; |
26
|
|
|
|
27
|
6 |
|
parent::__construct($router); |
28
|
|
|
} |
29
|
|
|
|
30
|
6 |
|
protected function getLocaleResourceAction($controller, $method) |
31
|
|
|
{ |
32
|
6 |
|
return $controller . '@' . $method; |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
protected function addResourceIndex($name, $base, $controller, $options): RouteCollection |
36
|
|
|
{ |
37
|
2 |
|
$uri = $this->getResourceUri($name); |
38
|
2 |
|
$name = $this->getResourceRouteName($name, 'index', $options); |
39
|
2 |
|
$action = $this->getLocaleResourceAction($controller, 'index'); |
40
|
|
|
|
41
|
2 |
|
return $this->localeRouter->get($name, $action, $uri); |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
protected function addResourceCreate($name, $base, $controller, $options): RouteCollection |
45
|
|
|
{ |
46
|
3 |
|
$base = $this->getResourceUri($name); |
47
|
3 |
|
$uris = $this->getLocaleUris($base, 'create', $options); |
48
|
3 |
|
$name = $this->getResourceRouteName($name, 'create', $options); |
49
|
3 |
|
$action = $this->getLocaleResourceAction($controller, 'create'); |
50
|
|
|
|
51
|
3 |
|
return $this->localeRouter->get($name, $action, $uris); |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
protected function getLocaleUris($base, $label, $options) |
55
|
|
|
{ |
56
|
3 |
|
$uris = []; |
57
|
|
|
|
58
|
3 |
|
foreach ($this->localeConfig->locales($options) as $locale) { |
59
|
3 |
|
$uris[$locale] = $base . '/' . $this->getTranslation($locale, $label); |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
return $uris; |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
protected function getTranslation($locale, $label) |
66
|
|
|
{ |
67
|
3 |
|
$untranslated = 'route-labels.' . $label; |
68
|
3 |
|
$translated = $this->translator->get($untranslated, [], $locale); |
69
|
3 |
|
return $translated === $untranslated ? $label : $translated; |
70
|
|
|
} |
71
|
|
|
|
72
|
5 |
|
protected function addResourceShow($name, $base, $controller, $options): RouteCollection |
73
|
|
|
{ |
74
|
5 |
|
$uri = $this->getResourceUri($name) . '/{' . $base . '}'; |
75
|
5 |
|
$name = $this->getResourceRouteName($name, 'show', $options); |
76
|
5 |
|
$action = $this->getLocaleResourceAction($controller, 'show'); |
77
|
|
|
|
78
|
5 |
|
return $this->localeRouter->get($name, $action, $uri); |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
protected function addResourceEdit($name, $base, $controller, $options): RouteCollection |
82
|
|
|
{ |
83
|
3 |
|
$base = $this->getResourceUri($name) . '/{' . $base . '}'; |
84
|
3 |
|
$uris = $this->getLocaleUris($base, 'edit', $options); |
85
|
3 |
|
$name = $this->getResourceRouteName($name, 'edit', $options); |
86
|
3 |
|
$action = $this->getLocaleResourceAction($controller, 'edit'); |
87
|
|
|
|
88
|
3 |
|
return $this->localeRouter->get($name, $action, $uris); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Route a resource to a controller. |
93
|
|
|
* |
94
|
|
|
* @param string $name |
95
|
|
|
* @param string $controller |
96
|
|
|
* @param array $options |
97
|
|
|
* @return \Illuminate\Routing\RouteCollection |
98
|
|
|
*/ |
99
|
6 |
|
public function register($name, $controller, array $options = []) |
100
|
|
|
{ |
101
|
6 |
|
if (isset($options['parameters']) && !isset($this->parameters)) { |
102
|
|
|
$this->parameters = $options['parameters']; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// If the resource name contains a slash, we will assume the developer wishes to |
106
|
|
|
// register these resource routes with a prefix so we will set that up out of |
107
|
|
|
// the box so they don't have to mess with it. Otherwise, we will continue. |
108
|
6 |
|
if (Str::contains($name, '/')) { |
109
|
|
|
$this->prefixedResource($name, $controller, $options); |
110
|
|
|
|
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// We need to extract the base resource from the resource name. Nested resources |
115
|
|
|
// are supported in the framework, but we need to know what name to use for a |
116
|
|
|
// place-holder on the route parameters, which should be the base resources. |
117
|
6 |
|
$base = $this->getResourceWildcard(last(explode('.', $name))); |
118
|
|
|
|
119
|
6 |
|
$defaults = $this->resourceDefaults; |
120
|
|
|
|
121
|
6 |
|
$collection = new RouteCollection; |
122
|
|
|
|
123
|
6 |
|
foreach ($this->getResourceMethods($defaults, $options) as $m) { |
124
|
6 |
|
$collection = $this->registerMethod($m, $name, $base, $controller, $options, $collection); |
125
|
|
|
} |
126
|
|
|
|
127
|
6 |
|
return $collection; |
128
|
|
|
} |
129
|
|
|
|
130
|
6 |
|
protected function registerMethod( |
131
|
|
|
string $method, |
132
|
|
|
$name, |
133
|
|
|
$base, |
134
|
|
|
$controller, |
135
|
|
|
$options, |
136
|
|
|
RouteCollection $collection |
137
|
|
|
): RouteCollection { |
138
|
6 |
|
$addResourceMethod = 'addResource' . ucfirst($method); |
139
|
6 |
|
$routeCollection = $this->$addResourceMethod($name, $base, $controller, $options); |
140
|
|
|
|
141
|
6 |
|
$routeArray = is_a($routeCollection, RouteCollection::class) ? |
142
|
6 |
|
$routeCollection->getRoutes() : |
143
|
5 |
|
[$routeCollection]; |
144
|
|
|
|
145
|
6 |
|
foreach ($routeArray as $route) { |
146
|
6 |
|
$collection->add($route); |
147
|
|
|
} |
148
|
|
|
|
149
|
6 |
|
return $collection; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|