1 | <?php |
||
40 | class PathDescriptor extends AbstractNameAwareDescriptor implements PathDescriptorInterface |
||
41 | { |
||
42 | |||
43 | /** |
||
44 | * The trait with the references descriptors. |
||
45 | * |
||
46 | * @var AppserverIo\Description\DescriptorReferencesTrait |
||
47 | */ |
||
48 | use DescriptorReferencesTrait; |
||
49 | |||
50 | /** |
||
51 | * The beans class name. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $className; |
||
56 | |||
57 | /** |
||
58 | * The array with the action methods. |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $actions = array(); |
||
63 | |||
64 | /** |
||
65 | * The array with the action results. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $results = array(); |
||
70 | |||
71 | /** |
||
72 | * Sets the beans class name. |
||
73 | * |
||
74 | * @param string $className The beans class name |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | public function setClassName($className) |
||
79 | { |
||
80 | $this->className = $className; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Returns the beans class name. |
||
85 | * |
||
86 | * @return string The beans class name |
||
87 | */ |
||
88 | public function getClassName() |
||
89 | { |
||
90 | return $this->className; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | 4 | * Adds a action method configuration. |
|
95 | * |
||
96 | 4 | * @param \AppserverIo\Routlt\Description\ActionDescriptorInterface $action The action method configuration |
|
97 | 4 | * |
|
98 | * @return void |
||
99 | */ |
||
100 | public function addAction(ActionDescriptorInterface $action) |
||
101 | { |
||
102 | $name = $action->getName(); |
||
103 | foreach ($action->getRequestMethods() as $method) { |
||
104 | 3 | $this->actions[$name][$method] = $action; |
|
105 | } |
||
106 | 3 | } |
|
107 | |||
108 | /** |
||
109 | * Sets the array with the action methods. |
||
110 | * |
||
111 | * @param array $actions The action methods |
||
112 | * |
||
113 | * @return void |
||
114 | */ |
||
115 | public function setActions(array $actions) |
||
116 | 4 | { |
|
117 | $this->actions = $actions; |
||
118 | 4 | } |
|
119 | 4 | ||
120 | /** |
||
121 | * The array with the action methods. |
||
122 | * |
||
123 | * @return array The action methods |
||
124 | */ |
||
125 | public function getActions() |
||
129 | |||
130 | /** |
||
131 | * Adds a result action configuration. |
||
132 | * |
||
133 | * @param \AppserverIo\Routlt\Description\ResultDescriptorInterface $result The action result configuration |
||
134 | * |
||
135 | * @return void |
||
136 | */ |
||
137 | public function addResult(ResultDescriptorInterface $result) |
||
138 | 4 | { |
|
139 | $this->results[$result->getName()] = $result; |
||
140 | 4 | } |
|
141 | 4 | ||
142 | 4 | /** |
|
143 | 4 | * Sets the array with the action results. |
|
144 | 4 | * |
|
145 | * @param array $results The action results |
||
146 | * |
||
147 | * @return void |
||
148 | */ |
||
149 | public function setResults(array $results) |
||
150 | { |
||
151 | $this->results = $results; |
||
152 | } |
||
153 | 1 | ||
154 | /** |
||
155 | 1 | * The array with the action results. |
|
156 | 1 | * |
|
157 | * @return array The action results |
||
158 | */ |
||
159 | public function getResults() |
||
160 | { |
||
161 | return $this->results; |
||
162 | } |
||
163 | 2 | ||
164 | /** |
||
165 | 2 | * Returns a new descriptor instance. |
|
166 | * |
||
167 | * @return \AppserverIo\Routlt\Description\PathDescriptorInterface The descriptor instance |
||
168 | */ |
||
169 | public static function newDescriptorInstance() |
||
170 | { |
||
171 | return new PathDescriptor(); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | 3 | * Returns a new annotation instance for the passed reflection class. |
|
176 | * |
||
177 | 3 | * @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class with the bean configuration |
|
178 | 3 | * |
|
179 | * @return \AppserverIo\Lang\Reflection\AnnotationInterface The reflection annotation |
||
180 | */ |
||
181 | protected function newAnnotationInstance(ClassInterface $reflectionClass) |
||
182 | { |
||
183 | return $reflectionClass->getAnnotation(Path::ANNOTATION); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | 1 | * Initializes the bean configuration instance from the passed reflection class instance. |
|
188 | * |
||
189 | 1 | * @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class with the bean configuration |
|
190 | 1 | * |
|
191 | * @return \AppserverIo\Routlt\Description\PathDescriptorInterface The initialized descriptor |
||
192 | */ |
||
193 | public function fromReflectionClass(ClassInterface $reflectionClass) |
||
194 | { |
||
195 | |||
196 | // add the annotation alias to the reflection class |
||
197 | 2 | $reflectionClass->addAnnotationAlias(Path::ANNOTATION, Path::__getClass()); |
|
198 | $reflectionClass->addAnnotationAlias(Result::ANNOTATION, Result::__getClass()); |
||
199 | 2 | $reflectionClass->addAnnotationAlias(Results::ANNOTATION, Results::__getClass()); |
|
200 | |||
201 | // query if we've an action |
||
202 | if ($reflectionClass->implementsInterface('AppserverIo\Routlt\ActionInterface') === false && |
||
203 | $reflectionClass->toPhpReflectionClass()->isAbstract() === false) { |
||
204 | // if not, do nothing |
||
205 | return; |
||
206 | } |
||
207 | |||
208 | // query if we've a servlet with a @Path annotation |
||
209 | 3 | if ($reflectionClass->hasAnnotation(Path::ANNOTATION) === false) { |
|
210 | // if not, do nothing |
||
211 | 3 | return; |
|
212 | 3 | } |
|
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 | 1 | $annotationInstance = $reflectionAnnotation->newInstance( |
|
222 | $reflectionAnnotation->getAnnotationName(), |
||
223 | 1 | $reflectionAnnotation->getValues() |
|
224 | 1 | ); |
|
225 | |||
226 | // load the default name to register in naming directory |
||
227 | if ($nameAttribute = $annotationInstance->getName()) { |
||
228 | $name = $nameAttribute; |
||
229 | } else { |
||
230 | // if @Annotation(name=****) is NOT set, we use the class name by default |
||
231 | 2 | $name = strtolower(str_replace('\\', '/', $reflectionClass->getName())); |
|
232 | } |
||
233 | 2 | ||
234 | // prepare and set the name |
||
235 | $this->setName(sprintf('/%s', ltrim($name, '/'))); |
||
236 | |||
237 | // we've to check for method annotations that declare the action methods |
||
238 | foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { |
||
239 | if ($action = ActionDescriptor::newDescriptorInstance()->fromReflectionMethod($reflectionMethod)) { |
||
240 | $this->addAction($action); |
||
241 | } |
||
242 | } |
||
243 | 3 | ||
244 | // we've to check for result annotations |
||
245 | 3 | if ($reflectionClass->hasAnnotation(Results::ANNOTATION)) { |
|
246 | 3 | // create the reflection annotation instance |
|
247 | $resultsAnnotation = $reflectionClass->getAnnotation(Results::ANNOTATION); |
||
248 | |||
249 | // initialize the @Results annotation instance |
||
250 | $resultsAnnotationInstance = $resultsAnnotation->newInstance( |
||
251 | $resultsAnnotation->getAnnotationName(), |
||
252 | $resultsAnnotation->getValues() |
||
253 | ); |
||
254 | |||
255 | 1 | // load the results |
|
256 | foreach ($resultsAnnotationInstance->getResults() as $resultAnnotation) { |
||
257 | 1 | $this->addResult(ResultDescriptor::newDescriptorInstance()->fromReflectionAnnotation($resultAnnotation)); |
|
258 | 1 | } |
|
259 | } |
||
260 | |||
261 | // initialize the shared flag @Path(shared=true) |
||
262 | $this->setShared($annotationInstance->getShared()); |
||
263 | |||
264 | // initialize references from the passed reflection class |
||
265 | 2 | $this->referencesFromReflectionClass($reflectionClass); |
|
266 | |||
267 | 2 | // return the instance |
|
268 | return $this; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Initializes a bean configuration instance from the passed deployment descriptor node. |
||
273 | * |
||
274 | * @param \SimpleXmlElement $node The deployment node with the bean configuration |
||
275 | 1 | * |
|
276 | * @return \AppserverIo\Routlt\Description\PathDescriptorInterface The initialized descriptor |
||
277 | 1 | */ |
|
278 | public function fromDeploymentDescriptor(\SimpleXmlElement $node) |
||
281 | |||
282 | /** |
||
283 | * Initializes a bean configuration instance from the passed configuration node. |
||
284 | * |
||
285 | 1 | * @param \AppserverIo\Configuration\Interfaces\NodeInterface $node The configuration node with the bean configuration |
|
286 | * |
||
287 | 1 | * @return \AppserverIo\Routlt\Description\PathDescriptorInterface The initialized descriptor |
|
288 | */ |
||
289 | public function fromConfiguration(NodeInterface $node) |
||
292 | |||
293 | /** |
||
294 | * Merges the passed configuration into this one. Configuration values |
||
295 | * of the passed configuration will overwrite the this one. |
||
296 | * |
||
297 | 4 | * @param \AppserverIo\Routlt\Description\PathDescriptorInterface $pathDescriptor The configuration to merge |
|
298 | * |
||
299 | 4 | * @return void |
|
300 | * @throws \AppserverIo\Routlt\Description\DescriptorException Is thrown if the passed descriptor has a different class name |
||
301 | */ |
||
302 | public function merge(PathDescriptorInterface $pathDescriptor) |
||
340 | } |
||
341 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.