1 | <?php |
||
88 | class Container extends ObjectAbstract implements ContainerInterface, ResolverAwareInterface, FactoryAwareInterface, ScopeInterface, ExtendedContainerInterface, DelegatorAwareInterface, \ArrayAccess, WritableInterface |
||
89 | { |
||
90 | use ContainerTrait, ArrayAccessTrait, DelegatorAwareTrait; |
||
91 | |||
92 | /** |
||
93 | * Inject a Phossa2\Config\Config |
||
94 | * |
||
95 | * ```php |
||
96 | * $configData = [ |
||
97 | * // container class |
||
98 | * 'di.class' => 'Phossa2\\Di\\Container', |
||
99 | * |
||
100 | * // container service definitions |
||
101 | * 'di.service' => [ |
||
102 | * // ... |
||
103 | * ], |
||
104 | * |
||
105 | * // interface/classname mappings |
||
106 | * 'di.mapping' => [ |
||
107 | * ], |
||
108 | * |
||
109 | * // init methods to run after container created |
||
110 | * 'di.init' => [ |
||
111 | * 'default' => [], |
||
112 | * 'mystuff' => [ ... ], |
||
113 | * ], |
||
114 | * ]; |
||
115 | * |
||
116 | * // instantiate $config |
||
117 | * $config = new Config(null, null, $configData); |
||
118 | * |
||
119 | * // instantiate container |
||
120 | * $container = new $config['di.class']($config); |
||
121 | * ``` |
||
122 | * |
||
123 | * @param ConfigInterface $config inject the config instance |
||
124 | * @param string $baseNode container's starting node in $config |
||
125 | * @param bool $writable able to inject object to the container |
||
126 | * @access public |
||
127 | */ |
||
128 | public function __construct( |
||
129 | ConfigInterface $config = null, |
||
130 | /*# string */ $baseNode = 'di', |
||
131 | /*# bool */ $writable = true |
||
132 | ) { |
||
133 | if (null === $config) { |
||
134 | $config = new Config(); |
||
135 | } |
||
136 | |||
137 | if ($config instanceof WritableInterface) { |
||
138 | $this->setWritable($writable); |
||
139 | $config->setWritable($writable); |
||
140 | } |
||
141 | |||
142 | $this->setResolver(new Resolver($this, $config, $baseNode)) |
||
143 | ->setFactory(new Factory($this))->registerSelf()->initContainer(); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Extensions to the Interop\Container\ContainerInterface |
||
148 | * |
||
149 | * - Accepting second param as object constructor arguments |
||
150 | * - Accpeting $id with scope appended, e.g. 'cache@myScope' |
||
151 | * |
||
152 | * {@inheritDoc} |
||
153 | */ |
||
154 | public function get($id) |
||
169 | |||
170 | /** |
||
171 | * Extensions to the Interop\Container\ContainerInterface |
||
172 | * |
||
173 | * - Accpeting $id with scope appended, e.g. 'cache@myScope' |
||
174 | * |
||
175 | * {@inheritDoc} |
||
176 | */ |
||
177 | public function has($id) |
||
186 | |||
187 | /** |
||
188 | * {@inheritDoc} |
||
189 | */ |
||
190 | public function set(/*# string */ $id, $value) |
||
206 | |||
207 | /** |
||
208 | * {@inheritDoc} |
||
209 | */ |
||
210 | public function one(/*# string */ $id, array $arguments = []) |
||
218 | |||
219 | /** |
||
220 | * {@inheritDoc} |
||
221 | */ |
||
222 | public function run($callable, array $arguments = []) |
||
230 | |||
231 | /** |
||
232 | * {@inheritDoc} |
||
233 | */ |
||
234 | public function resolve(&$toResolve) |
||
239 | |||
240 | /** |
||
241 | * - Overwrite `setDelegator()` from DelegatorAwareTrait |
||
242 | * - Update resolver $object_resolver |
||
243 | * |
||
244 | * {@inheritDoc} |
||
245 | */ |
||
246 | public function setDelegator(DelegatorInterface $delegator) |
||
252 | |||
253 | /** |
||
254 | * Register $this as 'di.service.container' |
||
255 | * |
||
256 | * - Later, $this can be referenced as '${#container}' anywhere |
||
257 | * |
||
258 | * - $skipCommon is to demonstrate skipping execute common methods for objects. |
||
259 | * |
||
260 | * instead of just do |
||
261 | * `$container->set($name, $object)` |
||
262 | * |
||
263 | * you may do |
||
264 | * $container->set($name, ['class' => $object, 'skip' => true]); |
||
265 | * |
||
266 | * @param bool $skipCommon skip common methods normally after instantiation |
||
267 | * @return $this |
||
268 | * @access protected |
||
269 | */ |
||
270 | protected function registerSelf(/*# bool */ $skipCommon = false) |
||
281 | } |
||
282 |