Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 191 lines (172 sloc) 5.806 kB
e677a53 @fabpot added Pimple code and documentation
authored
1 <?php
2
c1a32f0 @fabpot added PHPDoc to the Pimple class
authored
3 /*
4 * This file is part of Pimple.
5 *
6 * Copyright (c) 2009 Fabien Potencier
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
17 *
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
24 * THE SOFTWARE.
25 */
26
27 /**
28 * Pimple main class.
29 *
30 * @package pimple
31 * @author Fabien Potencier
32 */
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
33 class Pimple implements ArrayAccess
e677a53 @fabpot added Pimple code and documentation
authored
34 {
2bf06b5 @marijn Added constructor arguments.
marijn authored
35 private $values;
36
37 /**
38 * Instantiate the container.
39 *
40 * Objects and parameters can be passed as argument to the constructor.
41 *
42 * @param array $values The parameters or objects.
43 */
44 function __construct (array $values = array())
45 {
46 $this->values = $values;
47 }
e677a53 @fabpot added Pimple code and documentation
authored
48
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
49 /**
50 * Sets a parameter or an object.
51 *
7133d93 @fabpot added a note about why we don't allow any callable anymore
authored
52 * Objects must be defined as Closures.
53 *
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).
57 *
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
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
60 */
61 function offsetSet($id, $value)
e677a53 @fabpot added Pimple code and documentation
authored
62 {
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
63 $this->values[$id] = $value;
e677a53 @fabpot added Pimple code and documentation
authored
64 }
65
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
66 /**
67 * Gets a parameter or an object.
68 *
69 * @param string $id The unique identifier for the parameter or object
70 *
71 * @return mixed The value of the parameter or an object
72 *
73 * @throws InvalidArgumentException if the identifier is not defined
74 */
75 function offsetGet($id)
76 {
3900a1b @mtdowling Allowing null values to be set and retrieved from Pimple. Adding tes…
mtdowling authored
77 if (!array_key_exists($id, $this->values)) {
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
78 throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
79 }
e677a53 @fabpot added Pimple code and documentation
authored
80
e03223d @igorw Enforce services to be closures
igorw authored
81 return $this->values[$id] instanceof Closure ? $this->values[$id]($this) : $this->values[$id];
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
82 }
d079d16 Added __unset and __isset to Pimple.php
Timo Puschkasch authored
83
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
84 /**
85 * Checks if a parameter or an object is set.
86 *
abc727a Added missing annotations
Mateusz Tymek authored
87 * @param string $id The unique identifier for the parameter or object
88 *
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
89 * @return Boolean
90 */
91 function offsetExists($id)
92 {
93 return isset($this->values[$id]);
94 }
d079d16 Added __unset and __isset to Pimple.php
Timo Puschkasch authored
95
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
96 /**
2bce05b @fabpot fixed phpdoc (closes #7)
authored
97 * Unsets a parameter or an object.
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
98 *
abc727a Added missing annotations
Mateusz Tymek authored
99 * @param string $id The unique identifier for the parameter or object
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
100 */
101 function offsetUnset($id)
e677a53 @fabpot added Pimple code and documentation
authored
102 {
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
103 unset($this->values[$id]);
104 }
105
106 /**
107 * Returns a closure that stores the result of the given closure for
108 * uniqueness in the scope of this instance of Pimple.
109 *
110 * @param Closure $callable A closure to wrap for uniqueness
111 *
112 * @return Closure The wrapped closure
113 */
e03223d @igorw Enforce services to be closures
igorw authored
114 function share(Closure $callable)
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
115 {
0e77953 @igorw Make coding style of closures consistent
igorw authored
116 return function ($c) use ($callable) {
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
117 static $object;
e677a53 @fabpot added Pimple code and documentation
authored
118
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
119 if (is_null($object)) {
120 $object = $callable($c);
121 }
e677a53 @fabpot added Pimple code and documentation
authored
122
7294dc2 @fabpot changed API to use ArrayAccess instead of properties to allow any str…
authored
123 return $object;
124 };
125 }
6959e23 @fabpot added a protect() method to be able to have an anonymous function as …
authored
126
127 /**
128 * Protects a callable from being interpreted as a service.
129 *
130 * This is useful when you want to store a callable as a parameter.
131 *
132 * @param Closure $callable A closure to protect from being evaluated
133 *
134 * @return Closure The protected closure
135 */
e03223d @igorw Enforce services to be closures
igorw authored
136 function protect(Closure $callable)
6959e23 @fabpot added a protect() method to be able to have an anonymous function as …
authored
137 {
0e77953 @igorw Make coding style of closures consistent
igorw authored
138 return function ($c) use ($callable) {
6959e23 @fabpot added a protect() method to be able to have an anonymous function as …
authored
139 return $callable;
140 };
141 }
dd218be @fabpot added a way to get the closure defining a service
authored
142
143 /**
144 * Gets a parameter or the closure defining an object.
145 *
146 * @param string $id The unique identifier for the parameter or object
147 *
148 * @return mixed The value of the parameter or the closure defining an object
149 *
150 * @throws InvalidArgumentException if the identifier is not defined
151 */
152 function raw($id)
153 {
a78e1f7 @mtdowling Using array_key_exists for raw instead of isset. Adding coverage.
mtdowling authored
154 if (!array_key_exists($id, $this->values)) {
dd218be @fabpot added a way to get the closure defining a service
authored
155 throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
156 }
157
158 return $this->values[$id];
159 }
7367dca @davedevelopment Adds extend method
davedevelopment authored
160
161 /**
a687126 @fabpot fixed phpdoc
authored
162 * Extends an object definition.
7367dca @davedevelopment Adds extend method
davedevelopment authored
163 *
a687126 @fabpot fixed phpdoc
authored
164 * Useful when you want to extend an existing object definition,
165 * without necessarily loading that object.
7367dca @davedevelopment Adds extend method
davedevelopment authored
166 *
a687126 @fabpot fixed phpdoc
authored
167 * @param string $id The unique identifier for the object
168 * @param Closure $callable A closure to extend the original
7367dca @davedevelopment Adds extend method
davedevelopment authored
169 *
170 * @return Closure The wrapped closure
171 *
172 * @throws InvalidArgumentException if the identifier is not defined
173 */
174 function extend($id, Closure $callable)
175 {
176 if (!array_key_exists($id, $this->values)) {
177 throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
178 }
179
180 $factory = $this->values[$id];
6677eeb @davedevelopment Check definition is actually a Closure
davedevelopment authored
181
182 if (!($factory instanceof Closure)) {
183 throw new InvalidArgumentException(sprintf('Identifier "%s" does not contain an object definition.', $id));
184 }
185
7367dca @davedevelopment Adds extend method
davedevelopment authored
186 return $this->values[$id] = function ($c) use ($callable, $factory) {
187 return $callable($factory($c), $c);
188 };
189 }
e677a53 @fabpot added Pimple code and documentation
authored
190 }
Something went wrong with that request. Please try again.