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
31
* @author Fabien Potencier
33
class Pimple implements ArrayAccess
35
protected $values = array();
38
* Instantiate the container.
40
* Objects and parameters can be passed as argument to the constructor.
42
* @param array $values The parameters or objects.
44
public function __construct (array $values = array())
46
$this->values = $values;
50
* Sets a parameter or an object.
52
* Objects must be defined as Closures.
54
* Allowing any PHP callable leads to difficult to debug problems
55
* as function names (strings) are callable (creating a function with
56
* the same a name as an existing parameter would break your container).
58
* @param string $id The unique identifier for the parameter or object
59
* @param mixed $value The value of the parameter or a closure to defined an object
61
public function offsetSet($id, $value)
63
$this->values[$id] = $value;
67
* Gets a parameter or an object.
69
* @param string $id The unique identifier for the parameter or object
71
* @return mixed The value of the parameter or an object
73
* @throws InvalidArgumentException if the identifier is not defined
75
public function offsetGet($id)
77
if (!array_key_exists($id, $this->values)) {
78
throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
81
$isFactory = is_object($this->values[$id]) && method_exists($this->values[$id], '__invoke');
83
return $isFactory ? $this->values[$id]($this) : $this->values[$id];
87
* Checks if a parameter or an object is set.
89
* @param string $id The unique identifier for the parameter or object
93
public function offsetExists($id)
95
return array_key_exists($id, $this->values);
99
* Unsets a parameter or an object.
101
* @param string $id The unique identifier for the parameter or object
103
public function offsetUnset($id)
105
unset($this->values[$id]);
109
* Returns a closure that stores the result of the given closure for
110
* uniqueness in the scope of this instance of Pimple.
112
* @param Closure $callable A closure to wrap for uniqueness
114
* @return Closure The wrapped closure
116
public static function share(Closure $callable)
118
return function ($c) use ($callable) {
121
if (null === $object) {
122
$object = $callable($c);
130
* Protects a callable from being interpreted as a service.
132
* This is useful when you want to store a callable as a parameter.
134
* @param Closure $callable A closure to protect from being evaluated
136
* @return Closure The protected closure
138
public static function protect(Closure $callable)
140
return function ($c) use ($callable) {
146
* Gets a parameter or the closure defining an object.
148
* @param string $id The unique identifier for the parameter or object
150
* @return mixed The value of the parameter or the closure defining an object
152
* @throws InvalidArgumentException if the identifier is not defined
154
public function raw($id)
156
if (!array_key_exists($id, $this->values)) {
157
throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
160
return $this->values[$id];
164
* Extends an object definition.
166
* Useful when you want to extend an existing object definition,
167
* without necessarily loading that object.
169
* @param string $id The unique identifier for the object
170
* @param Closure $callable A closure to extend the original
172
* @return Closure The wrapped closure
174
* @throws InvalidArgumentException if the identifier is not defined
176
public function extend($id, Closure $callable)
178
if (!array_key_exists($id, $this->values)) {
179
throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
182
$factory = $this->values[$id];
184
if (!($factory instanceof Closure)) {
185
throw new InvalidArgumentException(sprintf('Identifier "%s" does not contain an object definition.', $id));
188
return $this->values[$id] = function ($c) use ($callable, $factory) {
189
return $callable($factory($c), $c);
194
* Returns all defined value names.
196
* @return array An array of value names
198
public function keys()
200
return array_keys($this->values);