1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Routlt\Description\PathDescriptor |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link http://github.com/appserver-io/routlt |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Routlt\Description; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Routlt\Annotations\Path; |
24
|
|
|
use AppserverIo\Lang\Reflection\ClassInterface; |
25
|
|
|
use AppserverIo\Configuration\Interfaces\NodeInterface; |
26
|
|
|
use AppserverIo\Description\DescriptorReferencesTrait; |
27
|
|
|
use AppserverIo\Description\AbstractNameAwareDescriptor; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Annotation to map a request path info to an action method. |
31
|
|
|
* |
32
|
|
|
* @author Tim Wagner <[email protected]> |
33
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
34
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
35
|
|
|
* @link http://github.com/appserver-io/routlt |
36
|
|
|
* @link http://www.appserver.io |
37
|
|
|
*/ |
38
|
|
|
class PathDescriptor extends AbstractNameAwareDescriptor implements PathDescriptorInterface |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The trait with the references descriptors. |
43
|
|
|
* |
44
|
|
|
* @var AppserverIo\Description\DescriptorReferencesTrait |
45
|
|
|
*/ |
46
|
|
|
use DescriptorReferencesTrait; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The beans class name. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $className; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The array with the action methods. |
57
|
|
|
* |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected $actions = array(); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The array with the action results. |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $results = array(); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sets the beans class name. |
71
|
|
|
* |
72
|
|
|
* @param string $className The beans class name |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
4 |
|
public function setClassName($className) |
77
|
|
|
{ |
78
|
4 |
|
$this->className = $className; |
79
|
4 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the beans class name. |
83
|
|
|
* |
84
|
|
|
* @return string The beans class name |
85
|
|
|
*/ |
86
|
2 |
|
public function getClassName() |
87
|
|
|
{ |
88
|
2 |
|
return $this->className; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Adds a action method configuration. |
93
|
|
|
* |
94
|
|
|
* @param \AppserverIo\Routlt\Description\ActionDescriptorInterface $action The action method configuration |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
4 |
|
public function addAction(ActionDescriptorInterface $action) |
99
|
|
|
{ |
100
|
|
|
|
101
|
|
|
// extract the action name |
102
|
4 |
|
$name = $action->getName(); |
103
|
|
|
|
104
|
|
|
// set the action for all request methods |
105
|
4 |
|
foreach ($action->getRequestMethods() as $method) { |
106
|
4 |
|
$this->actions[$name][$method] = $action; |
107
|
4 |
|
} |
108
|
4 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Sets the array with the action methods. |
112
|
|
|
* |
113
|
|
|
* @param array $actions The action methods |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
1 |
|
public function setActions(array $actions) |
118
|
|
|
{ |
119
|
1 |
|
$this->actions = $actions; |
120
|
1 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* The array with the action methods. |
124
|
|
|
* |
125
|
|
|
* @return array The action methods |
126
|
|
|
*/ |
127
|
2 |
|
public function getActions() |
128
|
|
|
{ |
129
|
2 |
|
return $this->actions; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Adds a result action configuration. |
134
|
|
|
* |
135
|
|
|
* @param \AppserverIo\Routlt\Description\ResultConfigurationDescriptorInterface $result The action result configuration |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
3 |
|
public function addResult(ResultConfigurationDescriptorInterface $result) |
140
|
|
|
{ |
141
|
3 |
|
$this->results[$result->getName()] = $result; |
142
|
3 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Sets the array with the action results. |
146
|
|
|
* |
147
|
|
|
* @param array $results The action results |
148
|
|
|
* |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
1 |
|
public function setResults(array $results) |
152
|
|
|
{ |
153
|
1 |
|
$this->results = $results; |
154
|
1 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* The array with the action results. |
158
|
|
|
* |
159
|
|
|
* @return array The action results |
160
|
|
|
*/ |
161
|
2 |
|
public function getResults() |
162
|
|
|
{ |
163
|
2 |
|
return $this->results; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns a new descriptor instance. |
168
|
|
|
* |
169
|
|
|
* @return \AppserverIo\Routlt\Description\PathDescriptorInterface The descriptor instance |
170
|
|
|
*/ |
171
|
1 |
|
public static function newDescriptorInstance() |
172
|
|
|
{ |
173
|
1 |
|
return new PathDescriptor(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns a new annotation instance for the passed reflection class. |
178
|
|
|
* |
179
|
|
|
* @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class with the bean configuration |
180
|
|
|
* |
181
|
|
|
* @return \AppserverIo\Lang\Reflection\AnnotationInterface The reflection annotation |
182
|
|
|
*/ |
183
|
|
|
protected function newAnnotationInstance(ClassInterface $reflectionClass) |
184
|
|
|
{ |
185
|
|
|
return $reflectionClass->getAnnotation(Path::ANNOTATION); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Initializes the bean configuration instance from the passed reflection class instance. |
190
|
|
|
* |
191
|
|
|
* @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class with the bean configuration |
192
|
|
|
* |
193
|
|
|
* @return \AppserverIo\Routlt\Description\PathDescriptorInterface The initialized descriptor |
194
|
|
|
*/ |
195
|
6 |
|
public function fromReflectionClass(ClassInterface $reflectionClass) |
196
|
|
|
{ |
197
|
|
|
|
198
|
|
|
// query if we've an action |
199
|
6 |
|
if ($reflectionClass->implementsInterface('AppserverIo\Routlt\ActionInterface') === false && |
200
|
6 |
|
$reflectionClass->toPhpReflectionClass()->isAbstract() === false) { |
201
|
|
|
// if not, do nothing |
202
|
1 |
|
return; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// try to load the annotation instance |
206
|
5 |
|
$annotationInstance = $this->getClassAnnotation($reflectionClass, Path::class); |
207
|
|
|
|
208
|
|
|
// query if we've a servlet with a @Path annotation |
209
|
5 |
|
if ($annotationInstance === null) { |
210
|
|
|
// if not, do nothing |
211
|
1 |
|
return; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// load class name |
215
|
4 |
|
$this->setClassName($reflectionClass->getName()); |
216
|
|
|
|
217
|
|
|
// load the default name to register in naming directory |
218
|
4 |
|
if ($nameAttribute = $annotationInstance->getName()) { |
219
|
3 |
|
$name = $nameAttribute; |
220
|
3 |
|
} else { |
221
|
|
|
// if @Annotation(name=****) is NOT set, we use the class name by default |
222
|
1 |
|
$name = strtolower(str_replace('\\', '/', $reflectionClass->getName())); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
// prepare and set the name |
226
|
4 |
|
$this->setName(sprintf('/%s', ltrim($name, '/'))); |
227
|
|
|
|
228
|
|
|
// we've to check for method annotations that declare the action methods |
229
|
4 |
|
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { |
230
|
4 |
|
if ($action = ActionDescriptor::newDescriptorInstance()->fromReflectionMethod($reflectionMethod)) { |
231
|
4 |
|
$this->addAction($action); |
232
|
4 |
|
} |
233
|
4 |
|
} |
234
|
|
|
|
235
|
|
|
// load the results |
236
|
4 |
|
foreach ($annotationInstance->getResults() as $resultAnnotation) { |
237
|
3 |
|
$this->addResult(ResultConfigurationDescriptor::newDescriptorInstance()->fromAnnotation($resultAnnotation)); |
238
|
4 |
|
} |
239
|
|
|
|
240
|
|
|
// initialize the shared flag @Path(shared=true) |
241
|
4 |
|
$this->setShared($annotationInstance->getShared()); |
242
|
|
|
|
243
|
|
|
// initialize references from the passed reflection class |
244
|
4 |
|
$this->referencesFromReflectionClass($reflectionClass); |
245
|
|
|
|
246
|
|
|
// return the instance |
247
|
4 |
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Initializes a bean configuration instance from the passed deployment descriptor node. |
252
|
|
|
* |
253
|
|
|
* @param \SimpleXmlElement $node The deployment node with the bean configuration |
254
|
|
|
* |
255
|
|
|
* @return \AppserverIo\Routlt\Description\PathDescriptorInterface The initialized descriptor |
256
|
|
|
*/ |
257
|
1 |
|
public function fromDeploymentDescriptor(\SimpleXmlElement $node) |
|
|
|
|
258
|
|
|
{ |
259
|
1 |
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Initializes a bean configuration instance from the passed configuration node. |
263
|
|
|
* |
264
|
|
|
* @param \AppserverIo\Configuration\Interfaces\NodeInterface $node The configuration node with the bean configuration |
265
|
|
|
* |
266
|
|
|
* @return \AppserverIo\Routlt\Description\PathDescriptorInterface The initialized descriptor |
267
|
|
|
*/ |
268
|
|
|
public function fromConfiguration(NodeInterface $node) |
|
|
|
|
269
|
|
|
{ |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Merges the passed configuration into this one. Configuration values |
274
|
|
|
* of the passed configuration will overwrite the this one. |
275
|
|
|
* |
276
|
|
|
* @param \AppserverIo\Routlt\Description\PathDescriptorInterface $pathDescriptor The configuration to merge |
277
|
|
|
* |
278
|
|
|
* @return void |
279
|
|
|
* @throws \AppserverIo\Routlt\Description\DescriptorException Is thrown if the passed descriptor has a different class name |
280
|
|
|
*/ |
281
|
2 |
|
public function merge(PathDescriptorInterface $pathDescriptor) |
282
|
|
|
{ |
283
|
|
|
|
284
|
|
|
// check if the classes are equal |
285
|
2 |
|
if ($this->getClassName() !== $pathDescriptor->getClassName()) { |
286
|
2 |
|
throw new DescriptorException( |
287
|
1 |
|
sprintf('You try to merge a path configuration for % with %s', $pathDescriptor->getClassName(), $this->getClassName()) |
288
|
1 |
|
); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
// merge the name |
292
|
1 |
|
if ($name = $pathDescriptor->getName()) { |
293
|
1 |
|
$this->setName($name); |
294
|
1 |
|
} |
295
|
|
|
|
296
|
|
|
// merge the shared flag |
297
|
1 |
|
$this->setShared($pathDescriptor->isShared()); |
298
|
|
|
|
299
|
|
|
// merge the action method descriptors |
300
|
1 |
|
foreach ($pathDescriptor->getActions() as $action) { |
301
|
2 |
|
$this->addAction($action); |
302
|
1 |
|
} |
303
|
|
|
|
304
|
|
|
// merge the result descriptors |
305
|
1 |
|
foreach ($pathDescriptor->getResults() as $result) { |
306
|
1 |
|
$this->addResult($result); |
307
|
1 |
|
} |
308
|
|
|
|
309
|
|
|
// merge the EPB references |
310
|
1 |
|
foreach ($pathDescriptor->getEpbReferences() as $epbReference) { |
311
|
1 |
|
$this->addEpbReference($epbReference); |
312
|
1 |
|
} |
313
|
|
|
|
314
|
|
|
// merge the EPB references |
315
|
1 |
|
foreach ($pathDescriptor->getResReferences() as $resReference) { |
316
|
1 |
|
$this->addResReference($resReference); |
317
|
1 |
|
} |
318
|
1 |
|
} |
319
|
|
|
} |
320
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.