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
|
8 |
|
public function resource($controller, array $options = array()) |
56
|
|
|
{ |
57
|
8 |
|
$name = isset($options["prefix"]) ? $options["prefix"] : ""; |
58
|
8 |
|
$name .= $this->getResourceName($controller, $options); |
59
|
8 |
|
$actions = $this->getResourceActions($options); |
60
|
8 |
|
$resource = new Resource; |
61
|
|
|
|
62
|
8 |
|
foreach ($actions as $action => $map) { |
63
|
8 |
|
$resource->set($this->set($map[0], $this->getResourcePath($action, $map[1], $name, $options), [$controller, $action])); |
64
|
8 |
|
} |
65
|
|
|
|
66
|
8 |
|
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
|
|
|
public function resources(array $controllers) |
77
|
|
|
{ |
78
|
|
|
$resource = new Resource; |
79
|
|
|
foreach ($controllers as $controller) |
80
|
|
|
$resource->set($this->resource($controller)); |
81
|
|
|
return $resource; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $controller |
86
|
|
|
* @param array $options |
87
|
|
|
* |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
|
91
|
8 |
|
protected function getResourceName($controller, array $options) |
92
|
|
|
{ |
93
|
8 |
|
return isset($options["as"]) ? $options["as"] : str_replace("controller", "", strtolower($controller)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param array $options |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
|
101
|
8 |
|
protected function getResourceActions(array $options) |
102
|
|
|
{ |
103
|
8 |
|
return isset($options["only"]) ? array_intersect_key($this->map, array_flip((array) $options["only"])) : |
104
|
8 |
|
(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
|
8 |
|
protected function getResourcePath($action, $path, $name, array $options) |
117
|
|
|
{ |
118
|
8 |
|
return str_replace("{name}", $name, |
119
|
8 |
|
$action === "make" && isset($options["translate"]["make"]) ? str_replace("make", $options["translate"]["make"], $path) : |
120
|
8 |
|
($action === "edit" && isset($options["translate"]["edit"]) ? str_replace("edit", $options["translate"]["edit"], $path) : $path)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|