1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Routlt\Description\ResultDescriptor |
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\Lang\Reflection\AnnotationInterface; |
24
|
|
|
use AppserverIo\Configuration\Interfaces\NodeInterface; |
25
|
|
|
use AppserverIo\Description\DescriptorReferencesTrait; |
26
|
|
|
use AppserverIo\Description\AbstractNameAwareDescriptor; |
27
|
|
|
use AppserverIo\Lang\Reflection\ClassInterface; |
28
|
|
|
use AppserverIo\Routlt\Annotations\Result; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Descriptor implementation for a action result. |
32
|
|
|
* |
33
|
|
|
* @author Tim Wagner <[email protected]> |
34
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
36
|
|
|
* @link http://github.com/appserver-io/routlt |
37
|
|
|
* @link http://www.appserver.io |
38
|
|
|
*/ |
39
|
|
|
class ResultDescriptor extends AbstractNameAwareDescriptor implements ResultDescriptorInterface |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The trait with the references descriptors. |
44
|
|
|
* |
45
|
|
|
* @var AppserverIo\Description\DescriptorReferencesTrait |
46
|
|
|
*/ |
47
|
|
|
use DescriptorReferencesTrait; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The beans class name. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $className; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The action result type. |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $type; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The action result value. |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
protected $result; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The HTTP response code that has to be send. |
72
|
|
|
* |
73
|
6 |
|
* @var string |
74
|
|
|
*/ |
75
|
6 |
|
protected $code = 200; |
76
|
6 |
|
|
77
|
|
|
/** |
78
|
|
|
* Sets the beans class name. |
79
|
|
|
* |
80
|
|
|
* @param string $className The beans class name |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
6 |
|
*/ |
84
|
|
|
public function setClassName($className) |
85
|
6 |
|
{ |
86
|
|
|
$this->className = $className; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the beans class name. |
91
|
|
|
* |
92
|
|
|
* @return string The beans class name |
93
|
|
|
*/ |
94
|
|
|
public function getClassName() |
95
|
6 |
|
{ |
96
|
|
|
return $this->className; |
97
|
6 |
|
} |
98
|
6 |
|
|
99
|
|
|
/** |
100
|
|
|
* Sets the action result type. |
101
|
|
|
* |
102
|
|
|
* @param string $type The action result type |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
1 |
|
*/ |
106
|
|
|
public function setType($type) |
107
|
1 |
|
{ |
108
|
|
|
$this->type = $type; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns the action result type. |
113
|
|
|
* |
114
|
|
|
* @return string The action result type |
115
|
|
|
*/ |
116
|
|
|
public function getType() |
117
|
6 |
|
{ |
118
|
|
|
return $this->type; |
119
|
6 |
|
} |
120
|
6 |
|
|
121
|
|
|
/** |
122
|
|
|
* Sets the action result value. |
123
|
|
|
* |
124
|
|
|
* @param string $result The action result value |
125
|
|
|
* |
126
|
|
|
* @return void |
127
|
1 |
|
*/ |
128
|
|
|
public function setResult($result) |
129
|
1 |
|
{ |
130
|
|
|
$this->result = $result; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns the action result value. |
135
|
|
|
* |
136
|
|
|
* @return string The action result value |
137
|
|
|
*/ |
138
|
|
|
public function getResult() |
139
|
1 |
|
{ |
140
|
|
|
return $this->result; |
141
|
1 |
|
} |
142
|
1 |
|
|
143
|
|
|
/** |
144
|
|
|
* Sets the HTTP response code that has to be send. |
145
|
|
|
* |
146
|
|
|
* @param integer $code The HTTP response code |
147
|
|
|
* |
148
|
|
|
* @return void |
149
|
1 |
|
*/ |
150
|
|
|
public function setCode($code) |
151
|
1 |
|
{ |
152
|
|
|
$this->code = $code; |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Returns the HTTP response code that has to be send. |
157
|
|
|
* |
158
|
|
|
* @return integer The HTTP response code |
159
|
3 |
|
*/ |
160
|
|
|
public function getCode() |
161
|
3 |
|
{ |
162
|
|
|
return $this->code; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns a new descriptor instance. |
167
|
|
|
* |
168
|
|
|
* @return \AppserverIo\Routlt\Description\ResultDescriptorInterface The descriptor instance |
169
|
|
|
*/ |
170
|
|
|
public static function newDescriptorInstance() |
171
|
6 |
|
{ |
172
|
|
|
return new ResultDescriptor(); |
173
|
|
|
} |
174
|
|
|
|
175
|
6 |
|
/** |
176
|
6 |
|
* Returns a new annotation instance for the passed reflection class. |
177
|
6 |
|
* |
178
|
6 |
|
* @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class with the bean configuration |
179
|
|
|
* |
180
|
|
|
* @return \AppserverIo\Lang\Reflection\AnnotationInterface The reflection annotation |
181
|
6 |
|
*/ |
182
|
6 |
|
protected function newAnnotationInstance(ClassInterface $reflectionClass) |
183
|
6 |
|
{ |
184
|
|
|
return $reflectionClass->getAnnotation(Result::ANNOTATION); |
185
|
|
|
} |
186
|
6 |
|
|
187
|
|
|
/** |
188
|
|
|
* Initializes the bean configuration instance from the passed reflection class instance. |
189
|
|
|
* |
190
|
|
|
* @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class with the bean configuration |
191
|
6 |
|
* |
192
|
|
|
* @return \AppserverIo\Routlt\Description\PathDescriptorInterface The initialized descriptor |
193
|
|
|
*/ |
194
|
|
|
public function fromReflectionClass(ClassInterface $reflectionClass) |
195
|
|
|
{ |
196
|
|
|
|
197
|
|
|
// add the annotation alias to the reflection class |
198
|
|
|
$reflectionClass->addAnnotationAlias(Result::ANNOTATION, Result::__getClass()); |
199
|
|
|
|
200
|
|
|
// query if we've an action |
201
|
1 |
|
if ($reflectionClass->implementsInterface('AppserverIo\Routlt\Results\ResultInterface') === false && |
202
|
|
|
$reflectionClass->toPhpReflectionClass()->isAbstract() === false |
203
|
1 |
|
) { |
204
|
|
|
// if not, do nothing |
205
|
|
|
return; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// query if we've a servlet with a @Path annotation |
209
|
|
|
if ($reflectionClass->hasAnnotation(Result::ANNOTATION) === false) { |
210
|
|
|
// if not, do nothing |
211
|
|
|
return; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// create a new annotation instance |
215
|
|
|
$reflectionAnnotation = $this->newAnnotationInstance($reflectionClass); |
216
|
|
|
|
217
|
|
|
// load class name |
218
|
|
|
$this->setClassName($reflectionClass->getName()); |
219
|
|
|
|
220
|
|
|
// initialize the annotation instance |
221
|
|
|
$annotationInstance = $reflectionAnnotation->newInstance( |
222
|
|
|
$reflectionAnnotation->getAnnotationName(), |
223
|
|
|
$reflectionAnnotation->getValues() |
224
|
|
|
); |
225
|
2 |
|
|
226
|
|
|
// load the default name to register in naming directory |
227
|
|
|
if ($nameAttribute = $annotationInstance->getName()) { |
228
|
|
|
$name = $nameAttribute; |
229
|
2 |
|
} else { |
230
|
1 |
|
// if @Annotation(name=****) is NOT set, we use the class name by default |
231
|
1 |
|
$name = $reflectionClass->getShortName(); |
232
|
1 |
|
} |
233
|
|
|
|
234
|
|
|
// prepare and set the name |
235
|
|
|
$this->setName($name); |
236
|
1 |
|
|
237
|
1 |
|
// initialize the descriptor properties from the annotation values |
238
|
1 |
|
$this->setType($annotationInstance->getType()); |
239
|
|
|
$this->setResult($annotationInstance->getResult()); |
240
|
|
|
|
241
|
1 |
|
// set the HTTP response code if given |
242
|
1 |
|
if ($code = $annotationInstance->getCode()) { |
243
|
1 |
|
$this->setCode($code); |
244
|
|
|
} |
245
|
|
|
|
246
|
1 |
|
// initialize the shared flag @Result(shared=true) |
247
|
1 |
|
$this->setShared($annotationInstance->getShared()); |
248
|
1 |
|
|
249
|
1 |
|
// initialize references from the passed reflection class |
250
|
|
|
$this->referencesFromReflectionClass($reflectionClass); |
251
|
|
|
|
252
|
|
|
// return the instance |
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Initializes the result configuration instance from the passed reflection annotation instance. |
258
|
|
|
* |
259
|
|
|
* @param \AppserverIo\Lang\Reflection\AnnotationInterface $reflectionAnnotation The reflection annotation with the result configuration |
260
|
|
|
* |
261
|
|
|
* @return \AppserverIo\Routlt\Description\ResultDescriptorInterface The initialized descriptor |
262
|
|
|
*/ |
263
|
|
|
public function fromReflectionAnnotation(AnnotationInterface $reflectionAnnotation) |
264
|
|
|
{ |
265
|
|
|
|
266
|
|
|
// initialize the annotation instance |
267
|
|
|
$annotationInstance = $reflectionAnnotation->newInstance( |
268
|
|
|
$reflectionAnnotation->getAnnotationName(), |
269
|
|
|
$reflectionAnnotation->getValues() |
270
|
|
|
); |
271
|
|
|
|
272
|
|
|
// initialize the descriptor properties from the annotation values |
273
|
|
|
$this->setName($annotationInstance->getName()); |
274
|
|
|
$this->setType($annotationInstance->getType()); |
275
|
|
|
$this->setResult($annotationInstance->getResult()); |
276
|
|
|
|
277
|
|
|
// set the HTTP response code if given |
278
|
|
|
if ($code = $annotationInstance->getCode()) { |
279
|
|
|
$this->setCode($code); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
// return the instance |
283
|
|
|
return $this; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Initializes a action configuration instance from the passed deployment descriptor node. |
288
|
|
|
* |
289
|
|
|
* @param \SimpleXmlElement $node The deployment node with the action configuration |
290
|
|
|
* |
291
|
|
|
* @return \AppserverIo\Routlt\Description\ActionDescriptorInterface The initialized descriptor |
292
|
|
|
*/ |
293
|
|
|
public function fromDeploymentDescriptor(\SimpleXmlElement $node) |
|
|
|
|
294
|
|
|
{ |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Initializes a action configuration instance from the passed configuration node. |
299
|
|
|
* |
300
|
|
|
* @param \AppserverIo\Configuration\Interfaces\NodeInterface $node The configuration node with the action configuration |
301
|
|
|
* |
302
|
|
|
* @return \AppserverIo\Routlt\Description\ActionDescriptorInterface The initialized descriptor |
303
|
|
|
*/ |
304
|
|
|
public function fromConfiguration(NodeInterface $node) |
|
|
|
|
305
|
|
|
{ |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Merges the passed configuration into this one. Configuration values |
310
|
|
|
* of the passed configuration will overwrite the this one. |
311
|
|
|
* |
312
|
|
|
* @param \AppserverIo\Routlt\Description\ResultDescriptorInterface $resultDescriptor The configuration to merge |
313
|
|
|
* |
314
|
|
|
* @return void |
315
|
|
|
* @throws \AppserverIo\Routlt\Description\DescriptorException Is thrown if the passed descriptor has a different method name |
316
|
|
|
*/ |
317
|
|
|
public function merge(ResultDescriptorInterface $resultDescriptor) |
318
|
|
|
{ |
319
|
|
|
|
320
|
|
|
// check if the classes are equal |
321
|
|
|
if ($this->getName() !== $resultDescriptor->getName()) { |
322
|
|
|
throw new DescriptorException( |
323
|
|
|
sprintf('You try to merge a result configuration for % with %s', $resultDescriptor->getName(), $this->getName()) |
324
|
|
|
); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
// merge the class name |
328
|
|
|
if ($className = $resultDescriptor->getClassName()) { |
329
|
|
|
$this->setClassName($className); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
// merge the type |
333
|
|
|
if ($type = $resultDescriptor->getType()) { |
334
|
|
|
$this->setType($type); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
// merge the result |
338
|
|
|
if ($result = $resultDescriptor->getResult()) { |
339
|
|
|
$this->setResult($result); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
// merge the code |
343
|
|
|
if ($code = $resultDescriptor->getCode()) { |
344
|
|
|
$this->setCode($code); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
// merge the shared flag |
348
|
|
|
$this->setShared($resultDescriptor->isShared()); |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.