1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cervo package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2010-2019 Nevraxe inc. & Marc André Audet <[email protected]>. |
7
|
|
|
* |
8
|
|
|
* @package Cervo |
9
|
|
|
* @author Marc André Audet <[email protected]> |
10
|
|
|
* @copyright 2010 - 2019 Nevraxe inc. & Marc André Audet |
11
|
|
|
* @license See LICENSE.md MIT |
12
|
|
|
* @link https://github.com/Nevraxe/Cervo |
13
|
|
|
* @since 5.0.0 |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
declare(strict_types=1); |
17
|
|
|
|
18
|
|
|
namespace Cervo; |
19
|
|
|
|
20
|
|
|
use Cervo\Exceptions\ControllerReflection\InvalidControllerException; |
21
|
|
|
use Cervo\Interfaces\ControllerInterface; |
22
|
|
|
use Cervo\Utils\ClassUtils; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class ControllerReflection |
26
|
|
|
* @package Cervo |
27
|
|
|
*/ |
28
|
|
|
final class ControllerReflection |
29
|
|
|
{ |
30
|
|
|
/** @var Context */ |
31
|
|
|
private $context; |
32
|
|
|
|
33
|
|
|
/** @var Route */ |
34
|
|
|
private $route; |
35
|
|
|
|
36
|
|
|
/** @var array */ |
37
|
|
|
private $parameters = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* ControllerReflection constructor. |
41
|
|
|
* |
42
|
|
|
* @param Context $context |
43
|
|
|
* @param Route $route |
44
|
|
|
*/ |
45
|
|
|
public function __construct(Context $context, Route $route) |
46
|
|
|
{ |
47
|
|
|
if (!ClassUtils::implements($route->getControllerClass(), ControllerInterface::class)) { |
48
|
|
|
throw new InvalidControllerException; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->context = $context; |
52
|
|
|
$this->route = $route; |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
$reflection = new \ReflectionClass($this->route->getControllerClass()); |
56
|
|
|
} catch (\ReflectionException $e) { |
57
|
|
|
// TODO: Log |
58
|
|
|
// The contructor isn't defined, so we ignore the exception and move on |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
foreach ($reflection->getConstructor()->getParameters() as $parameter) { |
63
|
|
|
$this->parameters[] = $this->getParameterValue($parameter); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function __invoke(): void |
68
|
|
|
{ |
69
|
|
|
$controllerClass = $this->route->getControllerClass(); |
70
|
|
|
(new $controllerClass(...$this->parameters))(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function getParameterValue(\ReflectionParameter $parameter) |
74
|
|
|
{ |
75
|
|
|
try { |
76
|
|
|
$class = $parameter->getClass(); |
77
|
|
|
} catch (\ReflectionException $e) { |
78
|
|
|
// TODO: Log |
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($class === null) { |
83
|
|
|
|
84
|
|
|
if ($parameter->isArray()) { |
85
|
|
|
|
86
|
|
|
if ($parameter->name == 'parameters') { |
87
|
|
|
return $this->route->getParameters(); |
88
|
|
|
} elseif ($parameter->name == 'arguments') { |
89
|
|
|
return $this->route->getArguments(); |
90
|
|
|
} else { |
91
|
|
|
|
92
|
|
|
try { |
93
|
|
|
return $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : []; |
94
|
|
|
} catch (\ReflectionException $e) { |
95
|
|
|
// TODO: Log |
96
|
|
|
return []; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} else { |
102
|
|
|
|
103
|
|
|
try { |
104
|
|
|
return $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null; |
105
|
|
|
} catch (\ReflectionException $e) { |
106
|
|
|
// TODO: Log |
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} elseif ($parameter->getClass()->name == Context::class) { |
113
|
|
|
return $this->context; |
114
|
|
|
} else { |
115
|
|
|
return $this->context->getSingletons()->get($parameter->getClass()->name); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|