1 | <?php |
||
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) |
||
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() |
||
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) |
||
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) |
||
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) |
||
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) |
||
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.