1 | <?php |
||
84 | class Container extends ObjectAbstract implements ContainerInterface, ScopeInterface, WritableInterface, \ArrayAccess, DelegatorAwareInterface, ExtendedContainerInterface |
||
85 | { |
||
86 | use WritableTrait, |
||
87 | ArrayAccessTrait, |
||
88 | DelegatorAwareTrait, |
||
89 | ExtendedContainerTrait; |
||
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 | // set resolver |
||
127 | $this->setResolver( |
||
128 | new Resolver($this, $config ?: new Config(), $baseNode) |
||
129 | ); |
||
130 | |||
131 | // set factory |
||
132 | $this->setFactory(new Factory($this->getResolver())); |
||
133 | |||
134 | // run methods in 'di.init' |
||
135 | $this->initContainer(); |
||
136 | } |
||
137 | |||
138 | // ContainerInterface related |
||
139 | |||
140 | /** |
||
141 | * Extensions to the Interop\Container\ContainerInterface |
||
142 | * |
||
143 | * - Accepting second param as object constructor arguments |
||
144 | * - Accpeting $id with scope appended, e.g. 'cache@myScope' |
||
145 | * |
||
146 | * {@inheritDoc} |
||
147 | */ |
||
148 | public function get($id) |
||
149 | { |
||
150 | if ($this->has($id)) { |
||
151 | $args = func_num_args() > 1 ? func_get_arg(1) : []; |
||
152 | $this->resolve($args); |
||
153 | return $this->getInstance($id, $args); |
||
154 | } else { |
||
155 | throw new NotFoundException( |
||
156 | Message::get(Message::DI_SERVICE_NOTFOUND, $id), |
||
157 | Message::DI_SERVICE_NOTFOUND |
||
158 | ); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Extensions to the Interop\Container\ContainerInterface |
||
164 | * |
||
165 | * - Accpeting $id with scope appended, e.g. 'cache@myScope' |
||
166 | * |
||
167 | * {@inheritDoc} |
||
168 | */ |
||
169 | public function has($id) |
||
178 | |||
179 | // ExtendedContainerInterface |
||
180 | |||
181 | /** |
||
182 | * {@inheritDoc} |
||
183 | */ |
||
184 | public function one(/*# string */ $id, array $arguments = []) |
||
190 | |||
191 | // WritableInterface related |
||
192 | |||
193 | /** |
||
194 | * {@inheritDoc} |
||
195 | */ |
||
196 | public function set(/*# string */ $id, $value) |
||
213 | |||
214 | /** |
||
215 | * Override 'isWritable()' in 'Phossa2\Config\Traits\WritableTrait' |
||
216 | * |
||
217 | * Container's writability is depend on its resolver |
||
218 | * |
||
219 | * {@inheritDoc} |
||
220 | */ |
||
221 | public function isWritable()/*# : bool */ |
||
225 | |||
226 | /** |
||
227 | * Override 'setWritable()' in 'Phossa2\Config\Traits\WritableTrait' |
||
228 | * |
||
229 | * Container's writability is depend on its resolver |
||
230 | * |
||
231 | * {@inheritDoc} |
||
232 | */ |
||
233 | public function setWritable($writable)/*# : bool */ |
||
237 | |||
238 | /** |
||
239 | * execute init methods defined in 'di.init' node |
||
240 | * |
||
241 | * @return $this |
||
242 | * @throws RuntimeException if anything goes wrong |
||
243 | * @access protected |
||
244 | */ |
||
245 | protected function initContainer() |
||
256 | } |
||
257 |