|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2010-2018 Nevraxe inc. & Marc André Audet <[email protected]>. All rights reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* Redistribution and use in source and binary forms, with or without modification, are |
|
9
|
|
|
* permitted provided that the following conditions are met: |
|
10
|
|
|
* |
|
11
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this list of |
|
12
|
|
|
* conditions and the following disclaimer. |
|
13
|
|
|
* |
|
14
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, this list |
|
15
|
|
|
* of conditions and the following disclaimer in the documentation and/or other materials |
|
16
|
|
|
* provided with the distribution. |
|
17
|
|
|
* |
|
18
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
19
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
20
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
21
|
|
|
* DISCLAIMED. IN NO EVENT SHALL NEVRAXE INC. & MARC ANDRÉ AUDET BE LIABLE FOR ANY |
|
22
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
23
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
24
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
25
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
26
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
27
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
namespace Cervo; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
use Cervo\Exceptions\ControllerReflection\InvalidControllerException; |
|
36
|
|
|
use Cervo\Interfaces\ControllerInterface; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Class ControllerReflection |
|
41
|
|
|
* @package Cervo |
|
42
|
|
|
*/ |
|
43
|
|
|
final class ControllerReflection |
|
44
|
|
|
{ |
|
45
|
|
|
private $controller_class; |
|
46
|
|
|
private $parameters = []; |
|
47
|
|
|
|
|
48
|
|
|
public function __construct(Context $context, Route $route) |
|
49
|
|
|
{ |
|
50
|
|
|
if (!is_subclass_of($route->getControllerClass(), ControllerInterface::class)) { |
|
|
|
|
|
|
51
|
|
|
throw new InvalidControllerException; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->controller_class = $route->getControllerClass(); |
|
55
|
|
|
|
|
56
|
|
|
try { |
|
57
|
|
|
|
|
58
|
|
|
$reflection = new \ReflectionClass($this->controller_class); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($reflection->getConstructor()->getParameters() as $parameter) { |
|
61
|
|
|
|
|
62
|
|
|
if ($parameter->getClass() === null) { |
|
63
|
|
|
|
|
64
|
|
|
if (!$parameter->isArray()) { |
|
65
|
|
|
$this->parameters[] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null; |
|
66
|
|
|
} elseif ($parameter->getName() == 'parameters') { |
|
67
|
|
|
$this->parameters[] = $route->getParameters(); |
|
68
|
|
|
} elseif ($parameter->getName() == 'arguments') { |
|
69
|
|
|
$this->parameters[] = $route->getArguments(); |
|
70
|
|
|
} else { |
|
71
|
|
|
$this->parameters[] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : []; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} else { |
|
75
|
|
|
$this->parameters[] = $context->getSingletons()->get($parameter->getClass()->getName()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
} catch (\ReflectionException $e) { |
|
81
|
|
|
// The contructor isn't defined |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function __invoke() : void |
|
86
|
|
|
{ |
|
87
|
|
|
(new $this->controller_class(...$this->parameters))(); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|