|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Preetender\Routing; |
|
4
|
|
|
use League\Container\Exception\ContainerException; |
|
5
|
|
|
use Preetender\Routing\Response\ExecuteResponse; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait RouteReflection |
|
9
|
|
|
* @package Preetender\Routing |
|
10
|
|
|
*/ |
|
11
|
|
|
trait RouteReflection |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var array */ |
|
14
|
|
|
protected static $numberOfParameters = []; |
|
15
|
|
|
|
|
16
|
|
|
/** @var */ |
|
17
|
|
|
protected static $reflectionObject; |
|
18
|
|
|
|
|
19
|
|
|
/** @var null */ |
|
20
|
|
|
protected static $resolveMethodName = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* ... |
|
24
|
|
|
* |
|
25
|
|
|
* @return mixed |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function handleRequestCallable() |
|
28
|
|
|
{ |
|
29
|
|
|
static::createReflection(new \ReflectionFunction($this->getCallable())); |
|
|
|
|
|
|
30
|
|
|
$call = call_user_func_array($this->callable, static::resolveParameters()); |
|
|
|
|
|
|
31
|
|
|
return ExecuteResponse::factory($call, $this->request, $this->response); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* ... |
|
36
|
|
|
* |
|
37
|
|
|
* @return mixed |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function handleRequestController() |
|
40
|
|
|
{ |
|
41
|
|
|
$attributes = RouteController::prepare($this->getCallable()); |
|
|
|
|
|
|
42
|
|
|
static::createReflection(new \ReflectionClass($attributes['class']), $attributes['method']); |
|
43
|
|
|
$call = call_user_func_array([$attributes['class'], $attributes['method']], static::resolveParameters()); |
|
44
|
|
|
return ExecuteResponse::factory($call, $this->request, $this->response); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param object $object |
|
49
|
|
|
* @param string|null $method |
|
50
|
|
|
*/ |
|
51
|
|
|
protected static function createReflection($object, string $method = null) |
|
52
|
|
|
{ |
|
53
|
|
|
static::$reflectionObject = $object; |
|
54
|
|
|
static::$resolveMethodName = $method; |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param int $position |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
protected static function getPointersAndReturnRequired(int $position) |
|
62
|
|
|
{ |
|
63
|
|
|
return static::$parameters[ array_flip( static::$numberOfParameters)[$position] ]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* ... |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected static function resolveParameters() |
|
71
|
|
|
{ |
|
72
|
|
|
$attributes = []; |
|
73
|
|
|
$parameters = static::getParametersObject(); |
|
74
|
|
|
if(count($parameters) > 0) { |
|
75
|
|
|
foreach ($parameters as $parameter) { |
|
76
|
|
|
/** @var \ReflectionParameter $parameter */ |
|
77
|
|
|
if(!$parameter->getClass()) { |
|
78
|
|
|
static::$numberOfParameters[] = $parameter->getPosition(); |
|
79
|
|
|
$attributes[$parameter->getPosition()] = static::getPointersAndReturnRequired($parameter->getPosition()); |
|
80
|
|
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
$className = $parameter->getClass()->getName(); |
|
83
|
|
|
if(static::checkClassRegisterContainer($className)) { |
|
84
|
|
|
$attributes[$parameter->getPosition()] = Kernel::getContainer()->get($className); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
return $attributes; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Check whether injected class is registered in container. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $className |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
protected static function checkClassRegisterContainer(string $className) |
|
98
|
|
|
{ |
|
99
|
|
|
if(!Kernel::getContainer()->has($className)) { |
|
100
|
|
|
throw new ContainerException("Class [{$className}] not registered in the container."); |
|
101
|
|
|
} |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get all parameters to object |
|
107
|
|
|
* |
|
108
|
|
|
* @return mixed |
|
109
|
|
|
*/ |
|
110
|
|
|
protected static function getParametersObject() |
|
111
|
|
|
{ |
|
112
|
|
|
if( static::$reflectionObject instanceof \ReflectionClass) { |
|
113
|
|
|
$method = static::$reflectionObject->getMethod( static::$resolveMethodName ); |
|
114
|
|
|
return $method->getParameters(); |
|
115
|
|
|
} |
|
116
|
|
|
if ( static::$reflectionObject instanceof \ReflectionFunction) { |
|
117
|
|
|
return static::$reflectionObject->getParameters(); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.