1 | <?php |
||
84 | class Container extends ObjectAbstract implements ContainerInterface, ScopeInterface, WritableInterface, \ArrayAccess, DelegatorAwareInterface, ExtendedContainerInterface |
||
85 | { |
||
86 | use WritableTrait, |
||
87 | ArrayAccessTrait, |
||
88 | DelegatorAwareTrait, |
||
89 | InstanceFactoryTrait; |
||
90 | |||
91 | /** |
||
92 | * Inject a Phossa2\Config\Config |
||
93 | * |
||
94 | * ```php |
||
95 | * $configData = [ |
||
96 | * // container class |
||
97 | * 'di.class' => 'Phossa2\\Di\\Container', |
||
98 | * |
||
99 | * // container service definitions |
||
100 | * 'di.service' => [ |
||
101 | * // ... |
||
102 | * ], |
||
103 | * |
||
104 | * // init methods to run after container created |
||
105 | * 'di.init' => [ |
||
106 | * 'default' => [], |
||
107 | * 'mystuff' => [ ... ], |
||
108 | * ], |
||
109 | * ]; |
||
110 | * |
||
111 | * // instantiate $config |
||
112 | * $config = new Config(null, null, $configData); |
||
113 | * |
||
114 | * // instantiate container |
||
115 | * $container = new $config['di.class']($config); |
||
116 | * ``` |
||
117 | * |
||
118 | * @param ConfigInterface $config inject the config instance |
||
119 | * @param string $baseNode container's starting node in $config |
||
120 | * @access public |
||
121 | */ |
||
122 | public function __construct( |
||
123 | ConfigInterface $config = null, |
||
124 | /*# string */ $baseNode = 'di' |
||
125 | ) { |
||
126 | // config |
||
127 | $conf = $config ?: new Config(); |
||
128 | |||
129 | // resolver & factory |
||
130 | $this |
||
131 | ->setResolver(new Resolver($this, $conf, $baseNode)) |
||
132 | ->setFactory(new Factory($this->getResolver())); |
||
133 | |||
134 | // alias couple objects |
||
135 | $this->alias('container', $this); |
||
136 | $this->alias('config', $conf); |
||
137 | |||
138 | // run methods in 'di.init' |
||
139 | $this->initContainer(); |
||
140 | } |
||
141 | |||
142 | // ContainerInterface related |
||
143 | |||
144 | /** |
||
145 | * Extensions to the Interop\Container\ContainerInterface |
||
146 | * |
||
147 | * - Accepting second param as object constructor arguments |
||
148 | * - Accpeting $id with scope appended, e.g. 'cache@myScope' |
||
149 | * |
||
150 | * {@inheritDoc} |
||
151 | */ |
||
152 | public function get($id) |
||
165 | |||
166 | /** |
||
167 | * Extensions to the Interop\Container\ContainerInterface |
||
168 | * |
||
169 | * - Accpeting $id with scope appended, e.g. 'cache@myScope' |
||
170 | * |
||
171 | * {@inheritDoc} |
||
172 | */ |
||
173 | public function has($id) |
||
182 | |||
183 | // ExtendedContainerInterface |
||
184 | |||
185 | /** |
||
186 | * {@inheritDoc} |
||
187 | */ |
||
188 | public function one(/*# string */ $id, array $arguments = []) |
||
194 | |||
195 | /** |
||
196 | * {@inheritDoc} |
||
197 | */ |
||
198 | public function run($callable, array $arguments = []) |
||
205 | |||
206 | /** |
||
207 | * {@inheritDoc} |
||
208 | */ |
||
209 | public function param(/*# string */ $name, $value)/*# : bool */ |
||
217 | |||
218 | /** |
||
219 | * {@inheritDoc} |
||
220 | */ |
||
221 | public function alias(/*# string */ $id, $object)/*# : bool */ |
||
222 | { |
||
223 | if ($this->isWritable() && !$this->has($id)) { |
||
224 | return $this->set($id, ['class' => $object, 'skip' => true]); |
||
225 | } |
||
226 | return false; |
||
227 | } |
||
228 | |||
229 | // AutoWiringInterface related |
||
230 | |||
231 | /** |
||
232 | * {@inheritDoc} |
||
233 | */ |
||
234 | public function auto(/*# bool */ $flag = true) |
||
239 | |||
240 | /** |
||
241 | * {@inheritDoc} |
||
242 | */ |
||
243 | public function isAuto()/*# : bool */ |
||
247 | |||
248 | // ReferenceResolveInterface related |
||
249 | |||
250 | /** |
||
251 | * {@inheritDoc} |
||
252 | */ |
||
253 | public function resolve(&$toResolve) |
||
258 | |||
259 | // ScopeInterface related |
||
260 | |||
261 | /** |
||
262 | * @inheritDoc |
||
263 | */ |
||
264 | public function share(/*# bool */ $flag = true) |
||
270 | |||
271 | // WritableInterface related |
||
272 | |||
273 | /** |
||
274 | * {@inheritDoc} |
||
275 | */ |
||
276 | public function set(/*# string */ $id, $value)/*# : bool */ |
||
292 | |||
293 | /** |
||
294 | * Override 'isWritable()' in 'Phossa2\Config\Traits\WritableTrait' |
||
295 | * |
||
296 | * Container's writability is depend on its resolver |
||
297 | * |
||
298 | * {@inheritDoc} |
||
299 | */ |
||
300 | public function isWritable()/*# : bool */ |
||
304 | |||
305 | /** |
||
306 | * Override 'setWritable()' in 'Phossa2\Config\Traits\WritableTrait' |
||
307 | * |
||
308 | * Container's writability is depend on its resolver |
||
309 | * |
||
310 | * {@inheritDoc} |
||
311 | */ |
||
312 | public function setWritable($writable)/*# : bool */ |
||
316 | } |
||
317 |