|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Codeburner Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Alex Rohleder <[email protected]> |
|
7
|
|
|
* @copyright 2016 Alex Rohleder |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Codeburner\Router\Collectors; |
|
12
|
|
|
|
|
13
|
|
|
use Codeburner\Router\Resource; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Methods for enable the collector to be resourceful and make |
|
17
|
|
|
* easily to build apis routes. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Alex Rohleder <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
trait ResourceCollectorTrait |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
abstract public function set($method, $pattern, $action); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* A map of all routes of resources. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
protected $map = [ |
|
34
|
|
|
"index" => ["get", "/{name}"], |
|
35
|
|
|
"make" => ["get", "/{name}/make"], |
|
36
|
|
|
"create" => ["post", "/{name}"], |
|
37
|
|
|
"show" => ["get", "/{name}/{id:int+}"], |
|
38
|
|
|
"edit" => ["get", "/{name}/{id:int+}/edit"], |
|
39
|
|
|
"update" => ["put", "/{name}/{id:int+}"], |
|
40
|
|
|
"delete" => ["delete", "/{name}/{id:int+}"], |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. |
|
45
|
|
|
* Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, |
|
46
|
|
|
* a resourceful route declares them in a single line of code. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $controller The controller name. |
|
49
|
|
|
* @param array $options Some options like, "as" to name the route pattern, "only" to |
|
50
|
|
|
* explicit say that only this routes will be registered, and |
|
51
|
|
|
* "except" that register all the routes except the indicates. |
|
52
|
|
|
* @return Resource |
|
53
|
|
|
*/ |
|
54
|
|
|
|
|
55
|
9 |
|
public function resource($controller, array $options = array()) |
|
56
|
|
|
{ |
|
57
|
9 |
|
$name = isset($options["prefix"]) ? $options["prefix"] : ""; |
|
58
|
9 |
|
$name .= $this->getResourceName($controller, $options); |
|
59
|
9 |
|
$actions = $this->getResourceActions($options); |
|
60
|
9 |
|
$resource = new Resource; |
|
61
|
|
|
|
|
62
|
9 |
|
foreach ($actions as $action => $map) { |
|
63
|
9 |
|
$resource->set($this->set($map[0], $this->getResourcePath($action, $map[1], $name, $options), [$controller, $action])); |
|
64
|
9 |
|
} |
|
65
|
|
|
|
|
66
|
9 |
|
return $resource; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Collect several resources at same time. |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $controllers Several controller names as parameters or an array with all controller names. |
|
73
|
|
|
* @return Resource |
|
74
|
|
|
*/ |
|
75
|
|
|
|
|
76
|
1 |
|
public function resources(array $controllers) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$resource = new Resource; |
|
79
|
1 |
|
foreach ($controllers as $controller) |
|
80
|
1 |
|
$resource->set($this->resource($controller)); |
|
81
|
1 |
|
return $resource; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $controller |
|
86
|
|
|
* @param array $options |
|
87
|
|
|
* |
|
88
|
|
|
* @return mixed |
|
89
|
|
|
*/ |
|
90
|
|
|
|
|
91
|
9 |
|
protected function getResourceName($controller, array $options) |
|
92
|
|
|
{ |
|
93
|
9 |
|
return isset($options["as"]) ? $options["as"] : str_replace("controller", "", strtolower($controller)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param array $options |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
|
|
101
|
9 |
|
protected function getResourceActions(array $options) |
|
102
|
|
|
{ |
|
103
|
9 |
|
return isset($options["only"]) ? array_intersect_key($this->map, array_flip((array) $options["only"])) : |
|
104
|
9 |
|
(isset($options["except"]) ? array_diff_key($this->map, array_flip((array) $options["except"])) : $this->map); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $action |
|
109
|
|
|
* @param string $path |
|
110
|
|
|
* @param string $name |
|
111
|
|
|
* @param string[] $options |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
|
|
116
|
9 |
|
protected function getResourcePath($action, $path, $name, array $options) |
|
117
|
|
|
{ |
|
118
|
9 |
|
return str_replace("{name}", $name, |
|
119
|
9 |
|
$action === "make" && isset($options["translate"]["make"]) ? str_replace("make", $options["translate"]["make"], $path) : |
|
120
|
9 |
|
($action === "edit" && isset($options["translate"]["edit"]) ? str_replace("edit", $options["translate"]["edit"], $path) : $path)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|