This repository was archived by the owner on Jul 14, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathResource.php
More file actions
378 lines (318 loc) · 11 KB
/
Copy pathResource.php
File metadata and controls
378 lines (318 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/*
|--------------------------------------------------------------------------
| Resourceful router
|--------------------------------------------------------------------------
|
| Setup sensible default routes per resource
|
| This tool allows for single level nested resources (e.g. /posts/45/comment/12)
| but no deeper. You can however implement multiple nested resources using:
|
| Resource::route('post')->with('comments')->with('images')->with('category');
|
*/
/**
* todo: namespace on sup resource
* todo: inherit formats on sub resource
*/
class Resource
{
/**
* The root url prepend
* @var string
*/
public $namespace;
/**
* The parent resource singular name
* @var string
*/
public $resource;
/**
* The parent resource plural name
* @var string
*/
public $resources;
/**
* The parent controller
*
* @var string
*/
public $controller;
/**
* values are: fuzzy
* @var mixed|null
*/
public $mode;
/*
* build some basic regexes
*/
private $idNum = '(\d+)'; // and qty of digits
private $idAny = '([^\.\/]*)'; // any until dot or slash is reached
private $formatAny = '\.??([^\.\/]*)'; // dot then any
private $formatNum = '\.??(\d+)'; // dot then digits
private $formatNone = ' '; // turn off formats
private $formatDefaults = '\.??(xml|json|html|csv|pdf)'; // dot the sample extensions
public function __construct($mode = null, $namespace = '')
{
}
/**
* generate plural and singular resource names
* @param $resource
*
* @return array
*/
static function makeResourceNames($resource)
{
$names = [];
$names['resource'] = $resource = str_singular(strtolower($resource)); # \Cake\Utility\Inflector::singularize( strtolower($resource) );
$names['resources'] = str_plural(strtolower($resource)); #\Cake\Utility\Inflector::pluralize( strtolower($resource) );
$names['controller'] = ucfirst($resource).'Controller';
return $names;
}
/**
* Process input options and set defaults
*
* @param $resource
* @param $options
*
* @return array
*/
static function options($resource, $options)
{
/*
* Set default controller name and plural resource name
* Strategy:
* --resources should always be plural
* --controllers should be singular: ResourceController
*/
if(is_null($options))
$options = array();
if(isset($options['uses']))
{
$options['resource'] = $resource;
$options['resources'] = $options['resources'] ?: $resource;
}
else
{
$names = static::makeResourceNames($resource);
$options['resource'] = $names['resource'];
$options['resources'] = $names['resources'];
$options['controller'] = $names['controller'];
}
#$options['mode'] = isset($options['mode']) ? $options['mode'] : null;
if(!isset($options['mode']))
$options['mode'] = Config::get('app.resourcefulMode', 'fuzzy');
/*
* Set additional routes (GET only)
*/
if(isset($options['action']))
{
if(is_string($options['action']))
{
$options['action'] = explode("|", $options['action']);
}
}
else
$options['action'] = null;
if(isset($options['before']) or isset($options['after']))
{
$options['has_filters'] = true;
if(isset($options['before']))
$options['filters']['before'] = $options['before'];
if(isset($options['after']))
$options['filters']['after'] = $options['after'];
}
else
$options['has_filters'] = false;
/*
* Embed the child resource.
* if true: use 'ResourceController' with child's method names
* if false: use 'ChildController' ans standard resourceful method names
*/
if(!isset($options['embed']))
$options['embed'] = false;
/*
* Formats are dot separates extensions on routes (e.g. resource.xml, resource.json)
*
* expects a string separated by pipes (e.g. "json|html|xml" )
*/
if(!isset($options['formats']))
{
$options['formats'] = false;
}
if(isset($options['handles']))
{
$options['namespace'] = rtrim( $options['handles'] , '/').'/';
}
elseif(isset($options['namespace']))
{
$options['namespace'] = rtrim( $options['namespace'] , '/').'/';
}
else
{
$options['namespace'] = "";
}
if($options['embed'])
{
$options['resourceActions'] = [
'index' => '@'. strtolower($options['resources']).'Index',
'indexall' => '@'. 'all'.ucfirst($options['resources']).'Index',
'show' => '@show'. ucfirst($options['resource']),
'new' => '@new'. ucfirst($options['resource']),
'edit' => '@edit'. ucfirst($options['resource']),
'create' => '@create'. ucfirst($options['resource']),
'store' => '@store'. ucfirst($options['resource']),
'update' => '@update'. ucfirst($options['resource']),
'destroy' => '@destroy'.ucfirst($options['resource']),
];
}
else
{
$options['resourceActions'] = [
'index' => '@index',
'indexall' => '@indexAll',
'show' => '@show',
'new' => '@new',
'edit' => '@edit',
'create' => '@create',
'store' => '@store',
'update' => '@update',
'destroy' => '@destroy',
];
}
return $options;
}
/**
* The default entry point
* e.g. Resource::route(...);
*
* acceptable options: uses|resources|action|mode|before|after|handles
*
* @param $resource
* @param null|array $options
*
* @return $this
*/
static function route($resource, $options = [])
{
$options['parent'] = true;
$options = static::options($resource, $options);
$router = new Resource();
$router->resource = $options['resource'];
$router->resources = $options['resources'];
$router->controller = $options['controller'];
$router->namespace = $options['namespace'];
$router->formats = $options['formats'];
if(!isset($options['mode']))
$router->mode = Config::get('app.resourcefulMode', 'fuzzy');
else
$router->mode = $options['mode'];
if($options['has_filters'])
return $router->group($options['filters'], $options);
return $router->chainRoutes($options);
}
protected function chainRoutes($options)
{
$this->makeRoute($options);
return $this;
}
/**
* Make the main resource routes
*
* @param $options
*
* @return $this
*/
protected function makeRoute($options)
{
$parent = $options['parent'];
if($parent)
$pre = $this->namespace;
else
$pre = $this->namespace.$this->resources.'/{pid}/';
if($options['formats'])
$ext = $options['formats'];
else
$ext = $this->formats;
$__ = $options['resourceActions'];
$resource = $options['resource'];
$resources = $options['resources'];
$controller = $options['controller'];
if($options['action'])
{
foreach($options['action'] as $action)
{
Route::get($pre.$resources.'/{id}/'.$action, ['as' => $action."_".$resource, 'uses' => $controller.'@'.$action]);
}
}
if($ext)
{
Route::get($pre.$resources."/create.{format}", $controller.$__['create'])->where('format', $ext);
Route::get($pre.$resources."/{id}/edit.{format}", $controller.$__['edit'])->where('format', $ext);
Route::get($pre.$resources."/{id}.{format}", $controller.$__['show'])->where('format', $ext);
Route::get($pre.$resources.".{format}", $controller.$__['index'])->where('format', $ext);
}
Route::get ($pre.$resources.'/create', ['as' => "create_".$resource,'uses' => $controller.$__['create']]);
Route::get ($pre.$resources.'/{id}/edit', ['as' => "edit_".$resource,'uses' => $controller.$__['edit'] ]);
Route::get ($pre.$resources.'/{id}', ['as' => $resource,'uses' => $controller.$__['show'] ]);
Route::get ($pre.$resources.'/{id}/delete', $controller.$__['destroy']);
Route::delete($pre.$resources.'/{id}', $controller.$__['destroy']);
Route::post ($pre.$resources, $controller.$__['store'] );
Route::put ($pre.$resources.'/{id}', $controller.$__['update'] );
Route::patch ($pre.$resources.'/{id}', $controller.$__['update'] );
if($parent)
{
Route::get ($pre.$resources, ['as' => $resources, 'uses' => $controller.$__['index'] ]);
}
else
{
Route::get ($options['namespace'].$resources, ['as' => 'all_'.$resources, 'uses' => $controller.$__['indexall']]);
Route::get ($pre.$resources, ['as' => $resources, 'uses' => $controller.$__['index'] ]);
}
if($this->mode == 'fuzzy')
{
// attempt to match anything reasonably close
Route::get($pre.$resources.'/{id}/destroy', $controller.$__['destroy']);
Route::get($pre.$resources.'/new', $controller.$__['create']);
}
}
/**
* Make a child resource
*
* @param $resource
* @param array $options
*
* @return $this
*/
public function with($resource, $options = [])
{
$options['parent'] = false;
$options = $this->options($resource, $options);
if($options['has_filters'])
{
return $this->group($options['filters'], $options);
}
else
{
return $this->chainRoutes($options);
}
}
/**
* Wrap routes in a group when filters are required
*
* @param array $filters
* @param array $options
*
* @return $this
*/
protected function group($filters, $options)
{
$me = $this;
Route::group($filters, function() use($me, $options)
{
$me->makeRoute($options);
});
return $this;
}
}