4 * This file is part of Pimple.
6 * Copyright (c) 2009 Fabien Potencier
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is furnished
13 * to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 * Container main class.
32 * @author Fabien Potencier
34 class Container implements \ArrayAccess
36 private $values = array();
39 private $frozen = array();
40 private $raw = array();
41 private $keys = array();
44 * Instantiate the container.
46 * Objects and parameters can be passed as argument to the constructor.
48 * @param array $values The parameters or objects.
50 public function __construct(array $values = array())
52 $this->factories = new \SplObjectStorage();
53 $this->protected = new \SplObjectStorage();
55 foreach ($values as $key => $value) {
56 $this->offsetSet($key, $value);
61 * Sets a parameter or an object.
63 * Objects must be defined as Closures.
65 * Allowing any PHP callable leads to difficult to debug problems
66 * as function names (strings) are callable (creating a function with
67 * the same name as an existing parameter would break your container).
69 * @param string $id The unique identifier for the parameter or object
70 * @param mixed $value The value of the parameter or a closure to define an object
72 * @throws \RuntimeException Prevent override of a frozen service
74 public function offsetSet($id, $value)
76 if (isset($this->frozen[$id])) {
77 throw new \RuntimeException(sprintf('Cannot override frozen service "%s".', $id));
80 $this->values[$id] = $value;
81 $this->keys[$id] = true;
85 * Gets a parameter or an object.
87 * @param string $id The unique identifier for the parameter or object
89 * @return mixed The value of the parameter or an object
91 * @throws \InvalidArgumentException if the identifier is not defined
93 public function offsetGet($id)
95 if (!isset($this->keys[$id])) {
96 throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
100 isset($this->raw[$id])
101 || !is_object($this->values[$id])
102 || isset($this->protected[$this->values[$id]])
103 || !method_exists($this->values[$id], '__invoke')
105 return $this->values[$id];
108 if (isset($this->factories[$this->values[$id]])) {
109 return $this->values[$id]($this);
112 $raw = $this->values[$id];
113 $val = $this->values[$id] = $raw($this);
114 $this->raw[$id] = $raw;
116 $this->frozen[$id] = true;
122 * Checks if a parameter or an object is set.
124 * @param string $id The unique identifier for the parameter or object
128 public function offsetExists($id)
130 return isset($this->keys[$id]);
134 * Unsets a parameter or an object.
136 * @param string $id The unique identifier for the parameter or object
138 public function offsetUnset($id)
140 if (isset($this->keys[$id])) {
141 if (is_object($this->values[$id])) {
142 unset($this->factories[$this->values[$id]], $this->protected[$this->values[$id]]);
145 unset($this->values[$id], $this->frozen[$id], $this->raw[$id], $this->keys[$id]);
150 * Marks a callable as being a factory service.
152 * @param callable $callable A service definition to be used as a factory
154 * @return callable The passed callable
156 * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object
158 public function factory($callable)
160 if (!method_exists($callable, '__invoke')) {
161 throw new \InvalidArgumentException('Service definition is not a Closure or invokable object.');
164 $this->factories->attach($callable);
170 * Protects a callable from being interpreted as a service.
172 * This is useful when you want to store a callable as a parameter.
174 * @param callable $callable A callable to protect from being evaluated
176 * @return callable The passed callable
178 * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object
180 public function protect($callable)
182 if (!method_exists($callable, '__invoke')) {
183 throw new \InvalidArgumentException('Callable is not a Closure or invokable object.');
186 $this->protected->attach($callable);
192 * Gets a parameter or the closure defining an object.
194 * @param string $id The unique identifier for the parameter or object
196 * @return mixed The value of the parameter or the closure defining an object
198 * @throws \InvalidArgumentException if the identifier is not defined
200 public function raw($id)
202 if (!isset($this->keys[$id])) {
203 throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
206 if (isset($this->raw[$id])) {
207 return $this->raw[$id];
210 return $this->values[$id];
214 * Extends an object definition.
216 * Useful when you want to extend an existing object definition,
217 * without necessarily loading that object.
219 * @param string $id The unique identifier for the object
220 * @param callable $callable A service definition to extend the original
222 * @return callable The wrapped callable
224 * @throws \InvalidArgumentException if the identifier is not defined or not a service definition
226 public function extend($id, $callable)
228 if (!isset($this->keys[$id])) {
229 throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
232 if (!is_object($this->values[$id]) || !method_exists($this->values[$id], '__invoke')) {
233 throw new \InvalidArgumentException(sprintf('Identifier "%s" does not contain an object definition.', $id));
236 if (!is_object($callable) || !method_exists($callable, '__invoke')) {
237 throw new \InvalidArgumentException('Extension service definition is not a Closure or invokable object.');
240 $factory = $this->values[$id];
242 $extended = function ($c) use ($callable, $factory) {
243 return $callable($factory($c), $c);
246 if (isset($this->factories[$factory])) {
247 $this->factories->detach($factory);
248 $this->factories->attach($extended);
251 return $this[$id] = $extended;
255 * Returns all defined value names.
257 * @return array An array of value names
259 public function keys()
261 return array_keys($this->values);
265 * Registers a service provider.
267 * @param ServiceProviderInterface $provider A ServiceProviderInterface instance
268 * @param array $values An array of values that customizes the provider
272 public function register(ServiceProviderInterface $provider, array $values = array())
274 $provider->register($this);
276 foreach ($values as $key => $value) {
277 $this[$key] = $value;