Completed
Push — master ( 4c28cc...314a07 )
by Tim
10s
created

ActionDescriptor::setDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * AppserverIo\Routlt\Description\ActionDescriptor
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\Http\HttpProtocol;
24
use AppserverIo\Routlt\Annotations\Action;
25
use AppserverIo\Lang\Reflection\MethodInterface;
26
use AppserverIo\Lang\Reflection\ReflectionAnnotation;
27
use AppserverIo\Configuration\Interfaces\NodeInterface;
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 ActionDescriptor implements ActionDescriptorInterface
39
{
40
41
    /**
42
     * The path info.
43
     *
44
     * @var string
45
     */
46
    protected $name;
47
48
    /**
49
     * The action method name.
50
     *
51
     * @var string
52
     */
53
    protected $methodName;
54
55
    /**
56
     * The request methods the action is listening to.
57
     *
58
     * @var array
59
     */
60
    protected $requestMethods;
61
62
    /**
63
     * The restrictions for the route placeholders.
64
     *
65
     * @var array
66
     */
67
    protected $restrictions;
68
69
    /**
70
     * The defaults for the route placeholders.
71
     *
72
     * @var array
73
     */
74
    protected $defaults;
75
76
    /**
77
     * Initializes the descriptor with the default
78
     * request methods the action is listening to.
79
     */
80
    public function __construct()
81
    {
82
83
        // initialize restrictions and defaults
84
        $this->defaults = array();
85
        $this->restrictions = array();
86
87
        // initialize the request methods
88
        $this->requestMethods = array(
89
            HttpProtocol::METHOD_CONNECT,
90
            HttpProtocol::METHOD_DELETE,
91
            HttpProtocol::METHOD_GET,
92
            HttpProtocol::METHOD_HEAD,
93
            HttpProtocol::METHOD_OPTIONS,
94
            HttpProtocol::METHOD_POST,
95
            HttpProtocol::METHOD_PUT,
96
            HttpProtocol::METHOD_TRACE
97
        );
98
    }
99
100
    /**
101
     * Sets the action path info.
102
     *
103
     * @param string $name The action path info
104
     *
105
     * @return void
106
     */
107
    public function setName($name)
108
    {
109
        $this->name = $name;
110
    }
111
112
    /**
113
     * Returns the path info.
114
     *
115
     * @return string The path info
116
     */
117
    public function getName()
118
    {
119
        return $this->name;
120
    }
121
122
    /**
123
     * Sets the action method name.
124
     *
125
     * @param string $methodName The action method name
126
     *
127
     * @return void
128
     */
129
    public function setMethodName($methodName)
130
    {
131
        $this->methodName = $methodName;
132
    }
133
134
    /**
135
     * Returns the action method name.
136
     *
137
     * @return string The action method name
138
     */
139
    public function getMethodName()
140
    {
141
        return $this->methodName;
142
    }
143
144
    /**
145
     * Sets the request methods the action is listening to.
146
     *
147
     * @param array $requestMethods The request methods
148
     *
149
     * @return void
150
     */
151
    public function setRequestMethods(array $requestMethods)
152
    {
153
        $this->requestMethods = $requestMethods;
154
    }
155
156
    /**
157
     * Returns the request methods the action is listening to.
158
     *
159
     * @return array The request methods
160
     */
161
    public function getRequestMethods()
162
    {
163
        return $this->requestMethods;
164
    }
165
166
    /**
167
     * Sets the restrictions for the route placeholders.
168
     *
169
     * @param array $restrictions The restrictions for the route placeholders
170
     *
171
     * @return void
172
     */
173
    public function setRestrictions(array $restrictions)
174
    {
175
        $this->restrictions = $restrictions;
176
    }
177
178
    /**
179
     * Returns the restrictions for the route placeholders.
180
     *
181
     * @return array The restrictions for the route placeholders
182
     */
183
    public function getRestrictions()
184
    {
185
        return $this->restrictions;
186
    }
187
188
    /**
189
     * Sets the defaults for the route placeholders.
190
     *
191
     * @param array $defaults The defaults for the route placeholders
192
     *
193
     * @return void
194
     */
195
    public function setDefaults(array $defaults)
196
    {
197
        $this->defaults = $defaults;
198
    }
199
200
    /**
201
     * Returns the defaults for the route placeholders.
202
     *
203
     * @return array The defaults for the route placeholders
204
     */
205
    public function getDefaults()
206
    {
207
        return $this->defaults;
208
    }
209
210
    /**
211
     * Returns a new descriptor instance.
212
     *
213
     * @return \AppserverIo\Routlt\Description\ActionDescriptorInterface The descriptor instance
214
     */
215
    public static function newDescriptorInstance()
216
    {
217
        return new ActionDescriptor();
218
    }
219
220
    /**
221
     * Returns a new annotation instance for the passed reflection method.
222
     *
223
     * @param \AppserverIo\Lang\Reflection\MethodInterface $reflectionMethod The reflection method with the action configuration
224
     *
225
     * @return \AppserverIo\Lang\Reflection\AnnotationInterface The reflection annotation
226
     */
227
    protected function newAnnotationInstance(MethodInterface $reflectionMethod)
228
    {
229
        return $reflectionMethod->getAnnotation(Action::ANNOTATION);
230
    }
231
232
    /**
233
     * Initializes the action configuration instance from the passed reflection method instance.
234
     *
235
     * @param \AppserverIo\Lang\Reflection\MethodInterface $reflectionMethod The reflection method with the action configuration
236
     *
237
     * @return \AppserverIo\Routlt\Description\ActionDescriptorInterface The initialized descriptor
238
     */
239
    public function fromReflectionMethod(MethodInterface $reflectionMethod)
240
    {
241
242
        // add the annotation alias to the reflection method
243
        $reflectionMethod->addAnnotationAlias(Action::ANNOTATION, Action::__getClass());
244
245
        // query if we've a method with a @Action annotation
246
        if ($reflectionMethod->hasAnnotation(Action::ANNOTATION) === false &&
247
            $reflectionMethod->getMethodName() !== 'perform') {
248
            // if not, do nothing
249
            return;
250
251
        // query whether we've the default perform() method WITHOUT an @Action annotation
252
        } elseif ($reflectionMethod->hasAnnotation(Action::ANNOTATION) === false &&
253
            $reflectionMethod->getMethodName() === 'perform') {
254
            // create an annotation instance manually
255
            $reflectionAnnotation = new ReflectionAnnotation(Action::__getClass());
256
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
257
        } else {
258
            // create a new annotation instance by default
259
            $reflectionAnnotation = $this->newAnnotationInstance($reflectionMethod);
260
        }
261
262
        // load method name
263
        $this->setMethodName($reflectionMethod->getMethodName());
264
265
        // initialize the annotation instance
266
        $annotationInstance = $reflectionAnnotation->newInstance(
267
            $reflectionAnnotation->getAnnotationName(),
268
            $reflectionAnnotation->getValues()
269
        );
270
271
        // load the default name to register in naming directory
272
        if (($nameAttribute = $annotationInstance->getName()) || $nameAttribute === '') {
273
            $this->setName($nameAttribute);
274
        } else {
275
            // if @Annotation(name=****) is NOT SET, we use the method name by default
276
            $this->setName('/' . lcfirst(str_replace('Action', '', $reflectionMethod->getMethodName())));
277
        }
278
279
        // initialize the array for the annotated request methods
280
        $annotatedRequestMethods = array();
281
282
        // parse the method for annotated request methods
283
        foreach ($this->getRequestMethods() as $requestMethod) {
284
            // prepare the annotation name, e. g. POST -> Post
285
            $annotationName = ucfirst(strtolower($requestMethod));
286
287
            // query whether the reflection method has been annotated
288
            if ($reflectionMethod->hasAnnotation($annotationName)) {
289
                array_push($annotatedRequestMethods, $requestMethod);
290
            }
291
        }
292
293
        // query whether at least one annotated request method has been found
294
        if (sizeof($annotatedRequestMethods) > 0) {
295
            // if yes, override the default request methods
296
            $this->setRequestMethods($annotatedRequestMethods);
297
        }
298
299
        // initialize the restrictions for the route placeholders
300
        if (is_array($restrictions = $annotationInstance->getRestrictions())) {
301
            foreach ($restrictions as $restriction) {
302
                list($name, $value) = $restriction;
303
                $this->restrictions[$name] = $value;
304
            }
305
        }
306
307
        // initialize the defaults for the route placeholders
308
        if (is_array($defaults = $annotationInstance->getDefaults())) {
309
            foreach ($defaults as $default) {
310
                list($name, $value) = $default;
311
                $this->defaults[$name] = $value;
312
            }
313
        }
314
315
        // return the instance
316
        return $this;
317
    }
318
319
    /**
320
     * Initializes a action configuration instance from the passed deployment descriptor node.
321
     *
322
     * @param \SimpleXmlElement $node The deployment node with the action configuration
323
     *
324
     * @return \AppserverIo\Routlt\Description\ActionDescriptorInterface The initialized descriptor
325
     */
326
    public function fromDeploymentDescriptor(\SimpleXmlElement $node)
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
327
    {
328
    }
329
330
    /**
331
     * Initializes a action configuration instance from the passed configuration node.
332
     *
333
     * @param \AppserverIo\Configuration\Interfaces\NodeInterface $node The configuration node with the action configuration
334
     *
335
     * @return \AppserverIo\Routlt\Description\ActionDescriptorInterface The initialized descriptor
336
     */
337
    public function fromConfiguration(NodeInterface $node)
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
338
    {
339
    }
340
341
    /**
342
     * Merges the passed configuration into this one. Configuration values
343
     * of the passed configuration will overwrite the this one.
344
     *
345
     * @param \AppserverIo\Routlt\Description\ActionDescriptorInterface $actionDescriptor The configuration to merge
346
     *
347
     * @return void
348
     * @throws \AppserverIo\Routlt\Description\DescriptorException Is thrown if the passed descriptor has a different method name
349
     */
350
    public function merge(ActionDescriptorInterface $actionDescriptor)
351
    {
352
353
        // check if the classes are equal
354
        if ($this->getMethodName() !== $actionDescriptor->getMethodName()) {
355
            throw new DescriptorException(
356
                sprintf('You try to merge a action configuration for % with %s', $actionDescriptor->getMethodName(), $this->getMethodName())
357
            );
358
        }
359
360
        // merge the name
361
        if ($name = $actionDescriptor->getName()) {
362
            $this->setName($name);
363
        }
364
    }
365
}
366