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\Container; |
18
|
|
|
use Phossa2\Di\Traits\FactoryTrait; |
19
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
20
|
|
|
use Phossa2\Di\Interfaces\FactoryInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Factory |
24
|
|
|
* |
25
|
|
|
* Wrapper of factorying methods for container |
26
|
|
|
* |
27
|
|
|
* @package Phossa2\Di |
28
|
|
|
* @author Hong Zhang <[email protected]> |
29
|
|
|
* @see FactoryInterface |
30
|
|
|
* @version 2.0.0 |
31
|
|
|
* @since 2.0.0 added |
32
|
|
|
*/ |
33
|
|
|
class Factory extends ObjectAbstract implements FactoryInterface |
34
|
|
|
{ |
35
|
|
|
use FactoryTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Container $container |
39
|
|
|
* @access public |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Container $container) |
42
|
|
|
{ |
43
|
|
|
$this->master = $container; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
public function createInstance(/*# string */ $rawId, array $arguments) |
50
|
|
|
{ |
51
|
|
|
// get resolved definition |
52
|
|
|
$def = $this->getDefinition($rawId, $arguments); |
53
|
|
|
|
54
|
|
|
// arguments |
55
|
|
|
$args = isset($def['args']) ? $def['args'] : []; |
56
|
|
|
|
57
|
|
|
if (is_string($def['class'])) { |
58
|
|
|
$obj = $this->constructObject($def['class'], $args); |
59
|
|
|
|
60
|
|
|
} else { |
61
|
|
|
$obj = $this->executeCallable($def['class'], $args); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// execute after-creation methods |
65
|
|
|
$this->afterCreation($obj, $def); |
66
|
|
|
|
67
|
|
|
return $obj; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
*/ |
73
|
|
|
public function executeCallable($callable, array $arguments = []) |
74
|
|
|
{ |
75
|
|
|
// not callable |
76
|
|
|
if (!is_callable($callable)) { |
77
|
|
|
return $callable; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!empty($arguments)) { |
81
|
|
|
$params = $this->getCallableParameters($callable); |
82
|
|
|
$args = $this->matchArguments($params, $arguments); |
83
|
|
|
return call_user_func_array($callable, $args); |
84
|
|
|
} else { |
85
|
|
|
return call_user_func($callable); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
*/ |
92
|
|
|
public function executeMethodBatch(array $methods, $object = null) |
93
|
|
|
{ |
94
|
|
|
$merged = $this->mergeMethods($methods); |
95
|
|
|
|
96
|
|
|
foreach ($merged as $method) { |
97
|
|
|
$this->executeMethod($method, $object); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* if $object provided, build callable like [$object, $method] and execute it. |
103
|
|
|
* |
104
|
|
|
* method: |
105
|
|
|
* - ['function', [ arguments...]] |
106
|
|
|
* |
107
|
|
|
* - [ callable, [ arguments ...]] |
108
|
|
|
* |
109
|
|
|
* - ['method', [ arguments ...]] |
110
|
|
|
* convert to [[$object, 'method'], [ ... ]] |
111
|
|
|
* |
112
|
|
|
* @param mixed method |
113
|
|
|
* @param object|null $object to construct callable |
114
|
|
|
* @throws LogicException if something goes wrong |
115
|
|
|
* @access protected |
116
|
|
|
*/ |
117
|
|
|
protected function executeMethod($method, $object = null) |
118
|
|
|
{ |
119
|
|
|
$callable = $method[0]; |
120
|
|
|
$arguments = isset($method[1]) ? $method[1] : []; |
121
|
|
|
|
122
|
|
|
// rebuild callable from $object |
123
|
|
|
if (is_string($callable) && |
124
|
|
|
method_exists($object, $callable) |
125
|
|
|
) { |
126
|
|
|
$callable = [$object, $callable]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->executeCallable($callable, $arguments); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Things to do after an object created. |
134
|
|
|
* |
135
|
|
|
* @param object $object |
136
|
|
|
* @param array $definition service definition for $object |
137
|
|
|
* @access protected |
138
|
|
|
*/ |
139
|
|
|
protected function afterCreation($object, array $definition) |
140
|
|
|
{ |
141
|
|
|
// execute methods of THIS object |
142
|
|
|
if (isset($definition['methods'])) { |
143
|
|
|
$this->executeMethodBatch($definition['methods'], $object); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// execute common methods for all objects |
147
|
|
|
if (!isset($definition['skip']) || !$definition['skip']) { |
148
|
|
|
$this->executeCommonBatch($object); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Execute common methods defined in 'di.common' for all objects |
154
|
|
|
* |
155
|
|
|
* Methods are in the form of |
156
|
|
|
* |
157
|
|
|
* [ interfaceOrClassname, [methodOrCallable, ArgumentsArray]], |
158
|
|
|
* [ testCallable($obj, $container), [methodOrCallable, ArgumentsArray], |
159
|
|
|
* ... |
160
|
|
|
* |
161
|
|
|
* @param object $object |
162
|
|
|
* @return $this |
163
|
|
|
* @access protected |
164
|
|
|
*/ |
165
|
|
|
protected function executeCommonBatch($object) |
166
|
|
|
{ |
167
|
|
|
$methods = []; |
168
|
|
|
|
169
|
|
|
// get methods from 'di.common' |
170
|
|
|
if ($this->master->getResolver()->hasInSection('', 'common')) { |
171
|
|
|
$methods = $this->mergeMethods( |
172
|
|
|
$this->master->getResolver()->getInSection('', 'common') |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
foreach ($methods as $method) { |
177
|
|
|
$tester = $method[0]; |
178
|
|
|
if (is_string($tester) && is_a($object, $tester) || |
179
|
|
|
call_user_func($tester, $object, $this->master) |
180
|
|
|
) { |
181
|
|
|
$this->executeMethod($method[1], $object); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|