1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kami\ApiCoreBundle\Routing; |
4
|
|
|
|
5
|
|
|
use Kami\ApiCoreBundle\Controller\ApiController; |
6
|
|
|
use Kami\ApiCoreBundle\Model\UserAwareInterface; |
7
|
|
|
use Symfony\Component\Config\Loader\Loader; |
8
|
|
|
use Symfony\Component\Routing\Route; |
9
|
|
|
use Symfony\Component\Routing\RouteCollection; |
10
|
|
|
|
11
|
|
|
class ApiCoreRoutingLoader extends Loader |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var bool |
15
|
|
|
*/ |
16
|
|
|
private $loaded = false; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $resources; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $defaultLocale; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $locales; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ApiRoutingLoader constructor. |
35
|
|
|
* @param array $resources |
36
|
|
|
*/ |
37
|
|
|
public function __construct(array $resources, array $locales, $defaultLocale) |
38
|
|
|
{ |
39
|
|
|
$this->resources = $resources; |
40
|
|
|
$this->defaultLocale = $defaultLocale; |
41
|
|
|
$this->locales = $locales; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param mixed $resource |
46
|
|
|
* @param string $type |
47
|
|
|
* @return RouteCollection |
48
|
|
|
*/ |
49
|
|
|
public function load($resource, $type = null) |
50
|
|
|
{ |
51
|
|
|
if (true === $this->loaded) { |
52
|
|
|
throw new \RuntimeException('Do not add the "api" loader twice'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$routes = new RouteCollection(); |
56
|
|
|
|
57
|
|
|
foreach ($this->resources as $resource) { |
|
|
|
|
58
|
|
|
$routes->add( |
59
|
|
|
sprintf('kami.api_core_%s_index', $resource['name']), |
60
|
|
|
$this->createIndexRoute($resource) |
61
|
|
|
); |
62
|
|
|
$routes->add( |
63
|
|
|
sprintf('kami.api_core_%s_filter', $resource['name']), |
64
|
|
|
$this->createFilterRoute($resource) |
65
|
|
|
); |
66
|
|
|
$routes->add( |
67
|
|
|
sprintf('kami.api_core_%s_item', $resource['name']), |
68
|
|
|
$this->createItemRoute($resource) |
69
|
|
|
); |
70
|
|
|
$routes->add( |
71
|
|
|
sprintf('kami.api_core_%s_create', $resource['name']), |
72
|
|
|
$this->createNewRoute($resource) |
73
|
|
|
); |
74
|
|
|
$routes->add( |
75
|
|
|
sprintf('kami.api_core_%s_update', $resource['name']), |
76
|
|
|
$this->createUpdateRoute($resource) |
77
|
|
|
); |
78
|
|
|
$routes->add( |
79
|
|
|
sprintf('kami.api_core_%s_delete', $resource['name']), |
80
|
|
|
$this->createDeleteRoute($resource) |
81
|
|
|
); |
82
|
|
|
if ($resource['entity'] instanceof UserAwareInterface) { |
83
|
|
|
$routes->add( |
84
|
|
|
sprintf('kami.api_core_%s_my', $resource['name']), |
85
|
|
|
$this->createMyRoute($resource) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->loaded = true; |
91
|
|
|
|
92
|
|
|
return $routes; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function supports($resource, $type = null) |
96
|
|
|
{ |
97
|
|
|
return 'kami_api_core' === $type; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
private function createIndexRoute(array $resource) |
102
|
|
|
{ |
103
|
|
|
$path = sprintf('/api/{_locale}{_S}%s{_dot}{_format}', $resource['name']); |
104
|
|
|
$defaults = [ |
105
|
|
|
'_controller' => ApiController::class.'::apiAction', |
106
|
|
|
'_locale' => $this->defaultLocale, |
107
|
|
|
'_entity' => $resource['entity'], |
108
|
|
|
'_strategy' => $resource['strategies']['index'], |
109
|
|
|
'_request_processor' => $resource['request_processor'], |
110
|
|
|
'_sort' => $resource['default_sort'], |
111
|
|
|
'_resource_name' => $resource['name'], |
112
|
|
|
'_sort_direction' => $resource['default_sort_direction'], |
113
|
|
|
'_format' => 'json' |
114
|
|
|
]; |
115
|
|
|
$requirements = [ |
116
|
|
|
'_S' => '/?', |
117
|
|
|
'_dot' => '\.?', |
118
|
|
|
'_locale' => '|'.implode('|', $this->locales), |
119
|
|
|
'_format' => 'json|xml' |
120
|
|
|
]; |
121
|
|
|
$route = new Route($path, $defaults, $requirements, [], '', [], ['GET']); |
122
|
|
|
|
123
|
|
|
return $route; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function createFilterRoute(array $resource) |
127
|
|
|
{ |
128
|
|
|
$path = sprintf('/api/{_locale}{_S}%s/filter{_dot}{_format}', $resource['name']); |
129
|
|
|
$defaults = [ |
130
|
|
|
'_controller' => ApiController::class.'::apiAction', |
131
|
|
|
'_locale' => $this->defaultLocale, |
132
|
|
|
'_entity' => $resource['entity'], |
133
|
|
|
'_strategy' => $resource['strategies']['filter'], |
134
|
|
|
'_request_processor' => $resource['request_processor'], |
135
|
|
|
'_sort' => $resource['default_sort'], |
136
|
|
|
'_sort_direction' => $resource['default_sort_direction'], |
137
|
|
|
'_resource_name' => $resource['name'], |
138
|
|
|
'_format' => 'json' |
139
|
|
|
]; |
140
|
|
|
$requirements = [ |
141
|
|
|
'_S' => '/?', |
142
|
|
|
'_dot' => '\.?', |
143
|
|
|
'_locale' => '|'.implode('|', $this->locales), |
144
|
|
|
'_format' => 'json|xml' |
145
|
|
|
]; |
146
|
|
|
$route = new Route($path, $defaults, $requirements, [], '', [], ['GET']); |
147
|
|
|
|
148
|
|
|
return $route; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
private function createItemRoute(array $resource) |
152
|
|
|
{ |
153
|
|
|
$path = sprintf('/api/{_locale}{_S}%s/{id}{_dot}{_format}', $resource['name']); |
154
|
|
|
$defaults = [ |
155
|
|
|
'_controller' => ApiController::class.'::apiAction', |
156
|
|
|
'_locale' => $this->defaultLocale, |
157
|
|
|
'_entity' => $resource['entity'], |
158
|
|
|
'_strategy' => $resource['strategies']['item'], |
159
|
|
|
'_request_processor' => $resource['request_processor'], |
160
|
|
|
'_resource_name' => $resource['name'], |
161
|
|
|
'_format' => 'json' |
162
|
|
|
]; |
163
|
|
|
$requirements = [ |
164
|
|
|
'_S' => '/?', |
165
|
|
|
'_dot' => '\.?', |
166
|
|
|
'_locale' => '|'.implode('|', $this->locales), |
167
|
|
|
'_format' => 'json|xml', |
168
|
|
|
'id' => '\d+' |
169
|
|
|
]; |
170
|
|
|
$route = new Route($path, $defaults, $requirements, [], '', [], ['GET']); |
171
|
|
|
|
172
|
|
|
return $route; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
private function createUpdateRoute(array $resource) |
176
|
|
|
{ |
177
|
|
|
$path = sprintf('/api/{_locale}{_S}%s/{id}{_dot}{_format}', $resource['name']); |
178
|
|
|
$defaults = [ |
179
|
|
|
'_controller' => ApiController::class.'::apiAction', |
180
|
|
|
'_locale' => $this->defaultLocale, |
181
|
|
|
'_entity' => $resource['entity'], |
182
|
|
|
'_strategy' => $resource['strategies']['update'], |
183
|
|
|
'_request_processor' => $resource['request_processor'], |
184
|
|
|
'_resource_name' => $resource['name'], |
185
|
|
|
'_format' => 'json' |
186
|
|
|
]; |
187
|
|
|
$requirements = [ |
188
|
|
|
'_S' => '/?', |
189
|
|
|
'_dot' => '\.?', |
190
|
|
|
'_locale' => '|'.implode('|', $this->locales), |
191
|
|
|
'_format' => 'json|xml', |
192
|
|
|
'id' => '\d+' |
193
|
|
|
]; |
194
|
|
|
$route = new Route($path, $defaults, $requirements, [], '', [], ['PUT']); |
195
|
|
|
|
196
|
|
|
return $route; |
197
|
|
|
} |
198
|
|
|
private function createNewRoute(array $resource) |
199
|
|
|
{ |
200
|
|
|
$path = sprintf('/api/{_locale}{_S}%s{_dot}{_format}', $resource['name']); |
201
|
|
|
$defaults = [ |
202
|
|
|
'_controller' => ApiController::class.'::apiAction', |
203
|
|
|
'_locale' => $this->defaultLocale, |
204
|
|
|
'_entity' => $resource['entity'], |
205
|
|
|
'_strategy' => $resource['strategies']['create'], |
206
|
|
|
'_request_processor' => $resource['request_processor'], |
207
|
|
|
'_resource_name' => $resource['name'], |
208
|
|
|
'_format' => 'json' |
209
|
|
|
]; |
210
|
|
|
$requirements = [ |
211
|
|
|
'_S' => '/?', |
212
|
|
|
'_dot' => '\.?', |
213
|
|
|
'_locale' => '|'.implode('|', $this->locales), |
214
|
|
|
'_format' => 'json|xml', |
215
|
|
|
'id' => '\d+' |
216
|
|
|
]; |
217
|
|
|
$route = new Route($path, $defaults, $requirements, [], '', [], ['POST']); |
218
|
|
|
|
219
|
|
|
return $route; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
private function createDeleteRoute(array $resource) |
223
|
|
|
{ |
224
|
|
|
$path = sprintf('/api/{_locale}{_S}%s/{id}{_dot}{_format}', $resource['name']); |
225
|
|
|
$defaults = [ |
226
|
|
|
'_controller' => ApiController::class.'::apiAction', |
227
|
|
|
'_locale' => $this->defaultLocale, |
228
|
|
|
'_entity' => $resource['entity'], |
229
|
|
|
'_strategy' => $resource['strategies']['delete'], |
230
|
|
|
'_request_processor' => $resource['request_processor'], |
231
|
|
|
'_resource_name' => $resource['name'], |
232
|
|
|
'_format' => 'json', |
233
|
|
|
]; |
234
|
|
|
$requirements = [ |
235
|
|
|
'_S' => '/?', |
236
|
|
|
'_dot' => '\.?', |
237
|
|
|
'_locale' => '|'.implode('|', $this->locales), |
238
|
|
|
'_format' => 'json|xml', |
239
|
|
|
'id' => '\d+' |
240
|
|
|
]; |
241
|
|
|
$route = new Route($path, $defaults, $requirements, [], '', [], ['DELETE']); |
242
|
|
|
|
243
|
|
|
return $route; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
private function createMyRoute(array $resource) |
247
|
|
|
{ |
248
|
|
|
$path = sprintf('/api/{_locale}{_S}my/%s/{id}{_dot}{_format}', $resource['name']); |
249
|
|
|
$defaults = [ |
250
|
|
|
'_controller' => ApiController::class.'::apiAction', |
251
|
|
|
'_locale' => $this->defaultLocale, |
252
|
|
|
'_entity' => $resource['entity'], |
253
|
|
|
'_strategy' => $resource['strategies']['my'], |
254
|
|
|
'_request_processor' => $resource['request_processor'], |
255
|
|
|
'_resource_name' => $resource['name'], |
256
|
|
|
'_format' => 'json' |
257
|
|
|
]; |
258
|
|
|
$requirements = [ |
259
|
|
|
'_S' => '/?', |
260
|
|
|
'_dot' => '\.?', |
261
|
|
|
'_locale' => '|'.implode('|', $this->locales), |
262
|
|
|
'_format' => 'json|xml', |
263
|
|
|
'id' => '\d+' |
264
|
|
|
]; |
265
|
|
|
$route = new Route($path, $defaults, $requirements, [], '', [], ['DELETE']); |
266
|
|
|
|
267
|
|
|
return $route; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|