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 resource. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
class EntityClosure extends Entity implements EntityInterface |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Holds group info... |
29
|
|
|
*/ |
30
|
|
|
public $group = null; |
31
|
|
|
|
32
|
|
|
// protected $group = null; |
|
|
|
|
33
|
|
|
// protected $group = array( |
|
|
|
|
34
|
|
|
// // 'title' => 'some group title...', |
35
|
|
|
// // 'description' => 'some group description..', |
36
|
|
|
// // 'bb' => 'ss' |
37
|
|
|
// ); |
38
|
|
|
|
39
|
|
|
private $reflection; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Sets and returns the reflection of a function. |
43
|
|
|
* |
44
|
|
|
* @param string $name The REST name of function. |
45
|
|
|
* @return \ReflectionFunction|false |
46
|
|
|
*/ |
47
|
|
|
public function reflectedFunc($name) |
48
|
|
|
{ |
49
|
|
|
if (isset($this->reflection[$name])) { |
50
|
|
|
return $this->reflection[$name]; |
51
|
|
|
} elseif ( isset($this->actions[$name]['action']) |
52
|
|
|
&& $this->actions[$name]['action'] instanceof \Closure |
53
|
|
|
) { |
54
|
|
|
$this->reflection[$name] = new \ReflectionFunction($this->actions[$name]['action']); |
55
|
|
|
|
56
|
|
|
return $this->reflection[$name]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function append(array $def) |
66
|
|
|
{ |
67
|
|
|
parent::_append($def); |
|
|
|
|
68
|
|
|
// if (!isset($def['method'])) { |
|
|
|
|
69
|
|
|
// throw new \RuntimeException('Closure not defining a method, somehting must be wrong!?'); |
|
|
|
|
70
|
|
|
// } |
71
|
|
|
$this->actions[$def['method']] = $def; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
* @codeCoverageIgnore |
77
|
|
|
*/ |
78
|
|
|
public function setActions(array $asso = null) |
79
|
|
|
{ |
80
|
|
|
// obsolete! |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function underlineCall(Router $route) |
87
|
|
|
{ |
88
|
|
|
$method = $this->getMethod($route); |
89
|
|
|
|
90
|
|
|
#try { |
91
|
|
|
$action = $this->getAction($route->getMethod()); |
92
|
|
|
#} catch (\Exception $e) { |
|
|
|
|
93
|
|
|
# throw new \RuntimeException("Resource entity not (yet) implemented.", 501); |
|
|
|
|
94
|
|
|
#} |
95
|
|
|
|
96
|
|
|
$params = $this->getValidatedParams($method, $route->getMethod(), $route->getParams()); |
97
|
|
|
|
98
|
|
|
return call_user_func_array($action, $params); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function parseDocs() |
105
|
|
|
{ |
106
|
|
|
// group class doc |
107
|
|
|
// $docs = Reflection::parsePhpDoc( $this->group ); |
|
|
|
|
108
|
|
|
$docs = $this->group; |
109
|
|
|
|
110
|
|
|
// doc for all methods |
111
|
|
View Code Duplication |
foreach ($this->getActions() as $key => $func) { |
|
|
|
|
112
|
|
|
$ref = $this->reflectedFunc($key); |
113
|
|
|
|
114
|
|
|
$_docs = Reflection::parsePhpDoc($ref); // <- TODO (required args) |
|
|
|
|
115
|
|
|
$_docs['method'] = $key; |
116
|
|
|
// $_docs['path'] = $key . ' '; //. $this->getAction($key); // tood here |
|
|
|
|
117
|
|
|
|
118
|
|
|
$docs['methods'][$key] = $_docs; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $docs; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public function getMethod(Router $route) |
128
|
|
|
{ |
129
|
|
|
$name = $route->getMethod(); |
130
|
|
|
if (false === $method = $this->reflectedFunc($name)) { |
131
|
|
|
throw new \InvalidArgumentException( |
132
|
|
|
"Invalid resource's method ({$name}) specified.", |
133
|
|
|
405 |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $method; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function getAction($method) |
141
|
|
|
{ |
142
|
|
|
return $this->actions[$method]['action']; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/* --- CLOSURE only --- */ |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Adds a redirect. |
149
|
|
|
* |
150
|
|
|
* @param string $location A name |
151
|
|
|
* @param array $resource The resource definition array |
|
|
|
|
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
public function redirect($location) |
155
|
|
|
{ |
156
|
|
|
$this->redirect = $location; |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Group a resource entity. |
163
|
|
|
* |
164
|
|
|
* @param string $name The group name |
|
|
|
|
165
|
|
|
* @return void |
166
|
|
|
*/ |
167
|
|
|
public function group($title) |
168
|
|
|
{ |
169
|
|
|
// var_dump('dd');exit; |
|
|
|
|
170
|
|
|
// public $group = null; |
|
|
|
|
171
|
|
|
// $this->group = ['title'=> 'd', 'documentation'=>'some']; |
|
|
|
|
172
|
|
|
|
173
|
|
|
// TODO retrive phpdoc coment strinfg here! |
174
|
|
|
|
175
|
|
|
// group test |
176
|
|
|
$this->group = array('title' => $title); |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.