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\Entity; |
14
|
|
|
|
15
|
|
|
use Apix\Entity, |
16
|
|
|
Apix\Entity\EntityInterface, |
17
|
|
|
Apix\Reflection, |
18
|
|
|
Apix\Router; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Represents a class based entity resource. |
22
|
|
|
*/ |
23
|
|
|
class EntityClass extends Entity implements EntityInterface |
24
|
|
|
{ |
25
|
|
|
protected $controller; |
26
|
|
|
|
27
|
|
|
private $reflection; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Sets and returns a reflection of the entity class. |
31
|
|
|
* |
32
|
|
|
* @return \ReflectionClass|false |
33
|
|
|
*/ |
34
|
|
|
public function reflectedClass() |
35
|
|
|
{ |
36
|
|
|
if (null === $this->reflection) { |
37
|
|
|
try { |
38
|
|
|
$this->reflection = new \ReflectionClass( |
39
|
|
|
$this->controller['name'] |
40
|
|
|
); |
41
|
|
|
} catch (\Exception $Exception) { |
42
|
|
|
throw new \RuntimeException('Resource entity class not (yet) implemented.', 501); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->reflection; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function append(array $def) |
53
|
|
|
{ |
54
|
|
|
parent::_append($def); |
|
|
|
|
55
|
|
|
|
56
|
|
|
if (isset($def['controller'])) { |
57
|
|
|
$this->controller = $def['controller']; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function underlineCall(Router $route) |
65
|
|
|
{ |
66
|
|
|
$name = $this->controller['name']; |
67
|
|
|
$args = $this->controller['args']; |
68
|
|
|
|
69
|
|
|
// if (!isset($entity->controller['name'])) { |
|
|
|
|
70
|
|
|
// $entity->controller['name'] = $route->controller_name; |
|
|
|
|
71
|
|
|
// } |
72
|
|
|
|
73
|
|
|
// just created a deependency here!!!!! |
74
|
|
|
// if (!isset($args)) { |
|
|
|
|
75
|
|
|
// $args = $route->controller_args; |
|
|
|
|
76
|
|
|
// } |
77
|
|
|
|
78
|
|
|
$method = $this->getMethod($route); |
79
|
|
|
|
80
|
|
|
$params = $this->getValidatedParams($method, $route->getMethod(), $route->getParams()); |
81
|
|
|
|
82
|
|
|
return call_user_func_array( |
83
|
|
|
array( |
84
|
|
|
new $name($args), |
85
|
|
|
$route->getAction()), |
86
|
|
|
$params |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function parseDocs() |
94
|
|
|
{ |
95
|
|
|
// group class doc |
96
|
|
|
$docs = Reflection::parsePhpDoc( |
97
|
|
|
$this->reflectedClass()->getDocComment() |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$actions = $this->getActions(); |
101
|
|
|
|
102
|
|
|
// doc for all methods |
103
|
|
View Code Duplication |
foreach ($this->getMethods() as $key => $method) { |
|
|
|
|
104
|
|
|
if ( $key = array_search($method->name, $actions) ) { |
105
|
|
|
|
106
|
|
|
$_docs = Reflection::parsePhpDoc( $method ); |
107
|
|
|
$_docs['method'] = $key; |
108
|
|
|
|
109
|
|
|
// temp |
110
|
|
|
$docs['methods'][$key] = $_docs; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $docs; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function getMethod(Router $route) |
121
|
|
|
{ |
122
|
|
|
$name = $route->getAction(); |
123
|
|
|
if (false === $this->reflectedClass()->hasMethod($name)) { |
124
|
|
|
throw new \InvalidArgumentException( |
125
|
|
|
"Invalid resource's method ({$route->getMethod()}) specified.", |
126
|
|
|
405 |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->reflectedClass()->getMethod($name); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function setActions(array $asso = null) |
137
|
|
|
{ |
138
|
|
|
$funcs = array(); |
139
|
|
|
foreach ($this->getMethods() as $ref) { |
140
|
|
|
$funcs[] = $ref->name; |
141
|
|
|
} |
142
|
|
|
$routes = Router::$actions; |
143
|
|
|
|
144
|
|
|
$this->actions = array_intersect($routes, $funcs); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns the class methods. Use internalaly. |
149
|
|
|
* @return array An array of \ReflectionMethod objects. |
150
|
|
|
*/ |
151
|
|
|
public function getMethods() |
152
|
|
|
{ |
153
|
|
|
return $this->reflectedClass()->getMethods( |
154
|
|
|
\ReflectionMethod::IS_STATIC | \ReflectionMethod::IS_PUBLIC |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.