1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of the Apix Project. |
6
|
|
|
* |
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
8
|
|
|
* |
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Apix; |
14
|
|
|
|
15
|
|
|
use Apix\Entity, |
16
|
|
|
Apix\Entity\EntityInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Represents a collection of resources. |
20
|
|
|
*/ |
21
|
|
|
class Resources |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $resources = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var EntityInterface |
31
|
|
|
*/ |
32
|
|
|
protected $entity = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sets an entity object. |
36
|
|
|
* |
37
|
|
|
* @param EntityInterface $entity An entity object |
38
|
|
|
*/ |
39
|
|
|
public function setEntity(EntityInterface $entity) |
40
|
|
|
{ |
41
|
|
|
$this->entity = $entity; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Gets the current entity object. |
46
|
|
|
* |
47
|
|
|
* @return EntityInterface |
48
|
|
|
*/ |
49
|
|
|
public function getEntity() |
50
|
|
|
{ |
51
|
|
|
return $this->entity; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Adds a resource entity. |
56
|
|
|
* |
57
|
|
|
* @param string $name A resource name |
58
|
|
|
* @param array $resource A resource definition array |
|
|
|
|
59
|
|
|
* @return Entity |
60
|
|
|
*/ |
61
|
|
|
public function add($name, array $resources) |
62
|
|
|
{ |
63
|
|
|
switch(true): |
64
|
|
|
|
65
|
|
|
case isset($resources['action']) |
66
|
|
|
&& $resources['action'] instanceof \Closure: |
67
|
|
|
$this->setEntity( |
68
|
|
|
new Entity\EntityClosure() |
69
|
|
|
); |
70
|
|
|
break; |
71
|
|
|
|
72
|
|
|
case isset($resources['controller']): |
73
|
|
|
default: |
74
|
|
|
$this->setEntity( |
75
|
|
|
new Entity\EntityClass() |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
endswitch; |
79
|
|
|
|
80
|
|
|
if (!isset($this->resources[$name])) { |
81
|
|
|
$entity = get_class($this->getEntity()); |
82
|
|
|
$this->resources[$name] = new $entity(); //new Entity($group); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
$this->resources[$name]->append($resources); |
85
|
|
|
|
86
|
|
|
return $this->resources[$name]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Checks wether a specified resource name exists. |
91
|
|
|
* |
92
|
|
|
* @param string $name The resource name to check |
93
|
|
|
* @return boolean |
94
|
|
|
*/ |
95
|
|
|
public function has($name) |
96
|
|
|
{ |
97
|
|
|
return isset($this->resources[$name]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns all the resources. |
102
|
|
|
* |
103
|
|
|
* @return array The array of resources |
104
|
|
|
*/ |
105
|
|
|
public function toArray() |
106
|
|
|
{ |
107
|
|
|
return $this->resources; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Gets the specified resource entity. |
112
|
|
|
* |
113
|
|
|
* @param string $name The resource name to retrieve. |
114
|
|
|
* @throws /DomainException 404 |
115
|
|
|
* @return Entity/EntityInterface |
|
|
|
|
116
|
|
|
*/ |
117
|
|
|
public function getResource($name) |
118
|
|
|
{ |
119
|
|
|
if (isset($this->resources[$name])) { |
120
|
|
|
return $this->resources[$name]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
throw new \DomainException( |
124
|
|
|
sprintf('Invalid resource entity specified (%s).', $name), 404 |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Gets the specified ressource entity from a route object. |
130
|
|
|
* |
131
|
|
|
* @param Router $route The resource route object. |
132
|
|
|
* @param boolean $follow Wether to handle the default actions. |
133
|
|
|
* @throws /DomainException 404 |
134
|
|
|
* @return Entity/EntityInterface |
|
|
|
|
135
|
|
|
*/ |
136
|
|
|
public function get(Router &$route, $follow=true) |
137
|
|
|
{ |
138
|
|
|
$entity = $this->getResource( |
139
|
|
|
$route->getName() |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
// swap if aliased/redirected |
143
|
|
|
if ($redirect = $entity->getRedirect()) { |
144
|
|
|
$entity = $this->getResource($redirect); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// handles the default actions but do not override a local action definition. |
148
|
|
|
if ($follow) { |
149
|
|
|
|
150
|
|
|
$method = $route->getMethod(); |
151
|
|
|
|
152
|
|
|
if ( $method == 'HEAD' && $entity->hasMethod('GET') ) { |
153
|
|
|
$route->setMethod('GET'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ( |
157
|
|
|
( $redirect = $entity->getDefaultAction($method) ) |
158
|
|
|
&& !$entity->hasMethod($method) |
159
|
|
|
) { |
160
|
|
|
$entity = $this->getResource($redirect); |
161
|
|
|
#$route->setParams(array('entity' => clone $entity)); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// set this entity route. |
166
|
|
|
$entity->setRoute($route); |
167
|
|
|
|
168
|
|
|
return $entity; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.