|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Di |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Di\Factory; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Di\Exception\LogicException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* FactoryHelperTrait |
|
21
|
|
|
* |
|
22
|
|
|
* Create service instance here |
|
23
|
|
|
* |
|
24
|
|
|
* @package Phossa2\Di |
|
25
|
|
|
* @author Hong Zhang <[email protected]> |
|
26
|
|
|
* @version 2.0.0 |
|
27
|
|
|
* @since 2.0.0 added |
|
28
|
|
|
*/ |
|
29
|
|
|
trait FactoryHelperTrait |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Instantiate service object from classname |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $class |
|
36
|
|
|
* @param array $args |
|
37
|
|
|
* @return object |
|
38
|
|
|
* @throws LogicException if something goes wrong |
|
39
|
|
|
* @access protected |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function constructObject(/*# string */ $class, array $args) |
|
42
|
|
|
{ |
|
43
|
|
|
$reflector = new \ReflectionClass($class); |
|
44
|
|
|
$constructor = $reflector->getConstructor(); |
|
45
|
|
|
|
|
46
|
|
|
// not constructor defined |
|
47
|
|
|
if (is_null($constructor)) { |
|
48
|
|
|
$obj = $reflector->newInstanceWithoutConstructor(); |
|
49
|
|
|
|
|
50
|
|
|
// normal class with constructor |
|
51
|
|
|
} else { |
|
52
|
|
|
$args = $this->matchArguments($constructor->getParameters(), $args); |
|
53
|
|
|
$obj = $reflector->newInstanceArgs($args); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $obj; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Match provided arguments with a method/function's reflection parameters |
|
61
|
|
|
* |
|
62
|
|
|
* @param \ReflectionParameter[] $reflectionParameters |
|
63
|
|
|
* @param array $providedArguments |
|
64
|
|
|
* @return array the resolved arguments |
|
65
|
|
|
* @throws LogicException |
|
66
|
|
|
* @access protected |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function matchArguments( |
|
69
|
|
|
array $reflectionParameters, |
|
70
|
|
|
array $providedArguments |
|
71
|
|
|
)/*# : array */ { |
|
72
|
|
|
// result |
|
73
|
|
|
$resolvedArguments = []; |
|
74
|
|
|
foreach ($reflectionParameters as $i => $param) { |
|
75
|
|
|
$class = $param->getClass(); |
|
76
|
|
|
|
|
77
|
|
|
if ($this->isTypeMatched($class, $providedArguments)) { |
|
78
|
|
|
$resolvedArguments[$i] = array_shift($providedArguments); |
|
79
|
|
|
|
|
80
|
|
|
} elseif ($this->isRequiredClass($param, $providedArguments)) { |
|
81
|
|
|
$resolvedArguments[$i] = $this->getObjectByClass($class->getName()); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
return array_merge($resolvedArguments, $providedArguments); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Try best to guess parameter and argument are the same type |
|
89
|
|
|
* |
|
90
|
|
|
* @param null|\ReflectionClass $class |
|
91
|
|
|
* @param array $arguments |
|
92
|
|
|
* @return bool |
|
93
|
|
|
* @access protected |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function isTypeMatched($class, array $arguments)/*# : bool */ |
|
96
|
|
|
{ |
|
97
|
|
|
if (empty($arguments)) { |
|
98
|
|
|
return false; |
|
99
|
|
|
} elseif (null !== $class) { |
|
100
|
|
|
return is_a($arguments[0], $class->getName()); |
|
101
|
|
|
} else { |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Is $param required and is a class/interface |
|
108
|
|
|
* |
|
109
|
|
|
* @param \ReflectionParameter $param |
|
110
|
|
|
* @param array $arguments |
|
111
|
|
|
* @return bool |
|
112
|
|
|
* @throws LogicException if mismatched arguments |
|
113
|
|
|
* @access protected |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function isRequiredClass( |
|
116
|
|
|
\ReflectionParameter $param, |
|
117
|
|
|
array $arguments |
|
118
|
|
|
)/*# : bool */ { |
|
119
|
|
|
$optional = $param->isOptional(); |
|
120
|
|
|
if ($param->getClass()) { |
|
121
|
|
|
return !$optional || !empty($arguments); |
|
122
|
|
|
} else { |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get callable parameters |
|
129
|
|
|
* |
|
130
|
|
|
* @param callable $callable |
|
131
|
|
|
* @return \ReflectionParameter[] |
|
132
|
|
|
* @throws LogicException if something goes wrong |
|
133
|
|
|
* @access protected |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function getCallableParameters(callable $callable)/*# : array */ |
|
136
|
|
|
{ |
|
137
|
|
|
// array type |
|
138
|
|
|
if (is_array($callable)) { |
|
139
|
|
|
$reflector = new \ReflectionClass($callable[0]); |
|
140
|
|
|
$method = $reflector->getMethod($callable[1]); |
|
141
|
|
|
|
|
142
|
|
|
// object with __invoke() defined |
|
143
|
|
|
} elseif ($this->isInvocable($callable)) { |
|
144
|
|
|
$reflector = new \ReflectionClass($callable); |
|
145
|
|
|
$method = $reflector->getMethod('__invoke'); |
|
146
|
|
|
|
|
147
|
|
|
// simple function |
|
148
|
|
|
} else { |
|
149
|
|
|
$method = new \ReflectionFunction($callable); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $method->getParameters(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Is $var a non-closure object with '__invoke()' defined ? |
|
157
|
|
|
* |
|
158
|
|
|
* @param mixed $var |
|
159
|
|
|
* @return bool |
|
160
|
|
|
* @access protected |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function isInvocable($var)/*# : bool */ |
|
163
|
|
|
{ |
|
164
|
|
|
return is_object($var) && |
|
165
|
|
|
!$var instanceof \Closure && |
|
166
|
|
|
method_exists($var, '__invoke'); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
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.