| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 2 | +// phpcs:ignoreFile | |
| 3 | 3 | /* |
| 4 | 4 | * This file is part of Pimple. |
| 5 | 5 | * |
| 6 | 6 | * Copyright (c) 2009 Fabien Potencier |
| @@ -30,252 +30,241 @@ | ||
| 30 | 30 | * Container main class. |
| 31 | 31 | * |
| 32 | 32 | * @author Fabien Potencier |
| 33 | 33 | */ |
| 34 | -class Container implements \ArrayAccess | |
| 35 | -{ | |
| 36 | - private $values = array(); | |
| 37 | - private $factories; | |
| 38 | - private $protected; | |
| 39 | - private $frozen = array(); | |
| 40 | - private $raw = array(); | |
| 41 | - private $keys = array(); | |
| 34 | +class Container implements \ArrayAccess { | |
| 42 | 35 | |
| 43 | - /** | |
| 44 | - * Instantiate the container. | |
| 45 | - * | |
| 46 | - * Objects and parameters can be passed as argument to the constructor. | |
| 47 | - * | |
| 48 | - * @param array $values The parameters or objects. | |
| 49 | - */ | |
| 50 | - public function __construct(array $values = array()) | |
| 51 | - { | |
| 52 | - $this->factories = new \SplObjectStorage(); | |
| 53 | - $this->protected = new \SplObjectStorage(); | |
| 36 | + private $values = array(); | |
| 37 | + private $factories; | |
| 38 | + private $protected; | |
| 39 | + private $frozen = array(); | |
| 40 | + private $raw = array(); | |
| 41 | + private $keys = array(); | |
| 54 | 42 | |
| 55 | - foreach ($values as $key => $value) { | |
| 56 | - $this->offsetSet($key, $value); | |
| 57 | - } | |
| 58 | - } | |
| 43 | + /** | |
| 44 | + * Instantiate the container. | |
| 45 | + * | |
| 46 | + * Objects and parameters can be passed as argument to the constructor. | |
| 47 | + * | |
| 48 | + * @param array $values The parameters or objects. | |
| 49 | + */ | |
| 50 | + public function __construct( array $values = array() ) { | |
| 51 | + $this->factories = new \SplObjectStorage(); | |
| 52 | + $this->protected = new \SplObjectStorage(); | |
| 59 | 53 | |
| 60 | - /** | |
| 61 | - * Sets a parameter or an object. | |
| 62 | - * | |
| 63 | - * Objects must be defined as Closures. | |
| 64 | - * | |
| 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). | |
| 68 | - * | |
| 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 | |
| 71 | - * @throws \RuntimeException Prevent override of a frozen service | |
| 72 | - */ | |
| 73 | - public function offsetSet($id, $value) | |
| 74 | - { | |
| 75 | - if (isset($this->frozen[$id])) { | |
| 76 | - throw new \RuntimeException(sprintf('Cannot override frozen service "%s".', $id)); | |
| 77 | - } | |
| 54 | + foreach ( $values as $key => $value ) { | |
| 55 | + $this->offsetSet( $key, $value ); | |
| 56 | + } | |
| 57 | + } | |
| 78 | 58 | |
| 79 | - $this->values[$id] = $value; | |
| 80 | - $this->keys[$id] = true; | |
| 81 | - } | |
| 59 | + /** | |
| 60 | + * Sets a parameter or an object. | |
| 61 | + * | |
| 62 | + * Objects must be defined as Closures. | |
| 63 | + * | |
| 64 | + * Allowing any PHP callable leads to difficult to debug problems | |
| 65 | + * as function names (strings) are callable (creating a function with | |
| 66 | + * the same name as an existing parameter would break your container). | |
| 67 | + * | |
| 68 | + * @param string $id The unique identifier for the parameter or object | |
| 69 | + * @param mixed $value The value of the parameter or a closure to define an object | |
| 70 | + * @throws \RuntimeException Prevent override of a frozen service | |
| 71 | + */ | |
| 72 | + public function offsetSet( $id, $value ) { | |
| 73 | + if ( isset( $this->frozen[ $id ] ) ) { | |
| 74 | + throw new \RuntimeException( sprintf( 'Cannot override frozen service "%s".', $id ) ); | |
| 75 | + } | |
| 82 | 76 | |
| 83 | - /** | |
| 84 | - * Gets a parameter or an object. | |
| 85 | - * | |
| 86 | - * @param string $id The unique identifier for the parameter or object | |
| 87 | - * | |
| 88 | - * @return mixed The value of the parameter or an object | |
| 89 | - * | |
| 90 | - * @throws \InvalidArgumentException if the identifier is not defined | |
| 91 | - */ | |
| 92 | - public function offsetGet($id) | |
| 93 | - { | |
| 94 | - if (!isset($this->keys[$id])) { | |
| 95 | - throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); | |
| 96 | - } | |
| 77 | + $this->values[ $id ] = $value; | |
| 78 | + $this->keys[ $id ] = true; | |
| 79 | + } | |
| 97 | 80 | |
| 98 | - if ( | |
| 99 | - isset($this->raw[$id]) | |
| 100 | - || !is_object($this->values[$id]) | |
| 101 | - || isset($this->protected[$this->values[$id]]) | |
| 102 | - || !method_exists($this->values[$id], '__invoke') | |
| 103 | - ) { | |
| 104 | - return $this->values[$id]; | |
| 105 | - } | |
| 81 | + /** | |
| 82 | + * Gets a parameter or an object. | |
| 83 | + * | |
| 84 | + * @param string $id The unique identifier for the parameter or object | |
| 85 | + * | |
| 86 | + * @return mixed The value of the parameter or an object | |
| 87 | + * | |
| 88 | + * @throws \InvalidArgumentException if the identifier is not defined | |
| 89 | + */ | |
| 90 | + public function offsetGet( $id ) { | |
| 91 | + if ( ! isset( $this->keys[ $id ] ) ) { | |
| 92 | + throw new \InvalidArgumentException( sprintf( 'Identifier "%s" is not defined.', $id ) ); | |
| 93 | + } | |
| 106 | 94 | |
| 107 | - if (isset($this->factories[$this->values[$id]])) { | |
| 108 | - return $this->values[$id]($this); | |
| 109 | - } | |
| 95 | + if ( | |
| 96 | + isset( $this->raw[ $id ] ) | |
| 97 | + || ! is_object( $this->values[ $id ] ) | |
| 98 | + || isset( $this->protected[ $this->values[ $id ] ] ) | |
| 99 | + || ! method_exists( $this->values[ $id ], '__invoke' ) | |
| 100 | + ) { | |
| 101 | + return $this->values[ $id ]; | |
| 102 | + } | |
| 110 | 103 | |
| 111 | - $raw = $this->values[$id]; | |
| 112 | - $val = $this->values[$id] = $raw($this); | |
| 113 | - $this->raw[$id] = $raw; | |
| 104 | + if ( isset( $this->factories[ $this->values[ $id ] ] ) ) { | |
| 105 | + return $this->values[ $id ]($this); | |
| 106 | + } | |
| 114 | 107 | |
| 115 | - $this->frozen[$id] = true; | |
| 108 | + $raw = $this->values[ $id ]; | |
| 109 | + $val = $this->values[ $id ] = $raw( $this ); | |
| 110 | + $this->raw[ $id ] = $raw; | |
| 116 | 111 | |
| 117 | - return $val; | |
| 118 | - } | |
| 112 | + $this->frozen[ $id ] = true; | |
| 119 | 113 | |
| 120 | - /** | |
| 121 | - * Checks if a parameter or an object is set. | |
| 122 | - * | |
| 123 | - * @param string $id The unique identifier for the parameter or object | |
| 124 | - * | |
| 125 | - * @return bool | |
| 126 | - */ | |
| 127 | - public function offsetExists($id) | |
| 128 | - { | |
| 129 | - return isset($this->keys[$id]); | |
| 130 | - } | |
| 114 | + return $val; | |
| 115 | + } | |
| 131 | 116 | |
| 132 | - /** | |
| 133 | - * Unsets a parameter or an object. | |
| 134 | - * | |
| 135 | - * @param string $id The unique identifier for the parameter or object | |
| 136 | - */ | |
| 137 | - public function offsetUnset($id) | |
| 138 | - { | |
| 139 | - if (isset($this->keys[$id])) { | |
| 140 | - if (is_object($this->values[$id])) { | |
| 141 | - unset($this->factories[$this->values[$id]], $this->protected[$this->values[$id]]); | |
| 142 | - } | |
| 117 | + /** | |
| 118 | + * Checks if a parameter or an object is set. | |
| 119 | + * | |
| 120 | + * @param string $id The unique identifier for the parameter or object | |
| 121 | + * | |
| 122 | + * @return bool | |
| 123 | + */ | |
| 124 | + public function offsetExists( $id ) { | |
| 125 | + return isset( $this->keys[ $id ] ); | |
| 126 | + } | |
| 143 | 127 | |
| 144 | - unset($this->values[$id], $this->frozen[$id], $this->raw[$id], $this->keys[$id]); | |
| 145 | - } | |
| 146 | - } | |
| 128 | + /** | |
| 129 | + * Unsets a parameter or an object. | |
| 130 | + * | |
| 131 | + * @param string $id The unique identifier for the parameter or object | |
| 132 | + */ | |
| 133 | + public function offsetUnset( $id ) { | |
| 134 | + if ( isset( $this->keys[ $id ] ) ) { | |
| 135 | + if ( is_object( $this->values[ $id ] ) ) { | |
| 136 | + unset( $this->factories[ $this->values[ $id ] ], $this->protected[ $this->values[ $id ] ] ); | |
| 137 | + } | |
| 147 | 138 | |
| 148 | - /** | |
| 149 | - * Marks a callable as being a factory service. | |
| 150 | - * | |
| 151 | - * @param callable $callable A service definition to be used as a factory | |
| 152 | - * | |
| 153 | - * @return callable The passed callable | |
| 154 | - * | |
| 155 | - * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object | |
| 156 | - */ | |
| 157 | - public function factory($callable) | |
| 158 | - { | |
| 159 | - if (!is_object($callable) || !method_exists($callable, '__invoke')) { | |
| 160 | - throw new \InvalidArgumentException('Service definition is not a Closure or invokable object.'); | |
| 161 | - } | |
| 139 | + unset( $this->values[ $id ], $this->frozen[ $id ], $this->raw[ $id ], $this->keys[ $id ] ); | |
| 140 | + } | |
| 141 | + } | |
| 162 | 142 | |
| 163 | - $this->factories->attach($callable); | |
| 143 | + /** | |
| 144 | + * Marks a callable as being a factory service. | |
| 145 | + * | |
| 146 | + * @param callable $callable A service definition to be used as a factory | |
| 147 | + * | |
| 148 | + * @return callable The passed callable | |
| 149 | + * | |
| 150 | + * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object | |
| 151 | + */ | |
| 152 | + public function factory( $callable ) { | |
| 153 | + if ( ! is_object( $callable ) || ! method_exists( $callable, '__invoke' ) ) { | |
| 154 | + throw new \InvalidArgumentException( 'Service definition is not a Closure or invokable object.' ); | |
| 155 | + } | |
| 164 | 156 | |
| 165 | - return $callable; | |
| 166 | - } | |
| 157 | + $this->factories->attach( $callable ); | |
| 167 | 158 | |
| 168 | - /** | |
| 169 | - * Protects a callable from being interpreted as a service. | |
| 170 | - * | |
| 171 | - * This is useful when you want to store a callable as a parameter. | |
| 172 | - * | |
| 173 | - * @param callable $callable A callable to protect from being evaluated | |
| 174 | - * | |
| 175 | - * @return callable The passed callable | |
| 176 | - * | |
| 177 | - * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object | |
| 178 | - */ | |
| 179 | - public function protect($callable) | |
| 180 | - { | |
| 181 | - if (!is_object($callable) || !method_exists($callable, '__invoke')) { | |
| 182 | - throw new \InvalidArgumentException('Callable is not a Closure or invokable object.'); | |
| 183 | - } | |
| 159 | + return $callable; | |
| 160 | + } | |
| 184 | 161 | |
| 185 | - $this->protected->attach($callable); | |
| 162 | + /** | |
| 163 | + * Protects a callable from being interpreted as a service. | |
| 164 | + * | |
| 165 | + * This is useful when you want to store a callable as a parameter. | |
| 166 | + * | |
| 167 | + * @param callable $callable A callable to protect from being evaluated | |
| 168 | + * | |
| 169 | + * @return callable The passed callable | |
| 170 | + * | |
| 171 | + * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object | |
| 172 | + */ | |
| 173 | + public function protect( $callable ) { | |
| 174 | + if ( ! is_object( $callable ) || ! method_exists( $callable, '__invoke' ) ) { | |
| 175 | + throw new \InvalidArgumentException( 'Callable is not a Closure or invokable object.' ); | |
| 176 | + } | |
| 186 | 177 | |
| 187 | - return $callable; | |
| 188 | - } | |
| 178 | + $this->protected->attach( $callable ); | |
| 189 | 179 | |
| 190 | - /** | |
| 191 | - * Gets a parameter or the closure defining an object. | |
| 192 | - * | |
| 193 | - * @param string $id The unique identifier for the parameter or object | |
| 194 | - * | |
| 195 | - * @return mixed The value of the parameter or the closure defining an object | |
| 196 | - * | |
| 197 | - * @throws \InvalidArgumentException if the identifier is not defined | |
| 198 | - */ | |
| 199 | - public function raw($id) | |
| 200 | - { | |
| 201 | - if (!isset($this->keys[$id])) { | |
| 202 | - throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); | |
| 203 | - } | |
| 180 | + return $callable; | |
| 181 | + } | |
| 204 | 182 | |
| 205 | - if (isset($this->raw[$id])) { | |
| 206 | - return $this->raw[$id]; | |
| 207 | - } | |
| 183 | + /** | |
| 184 | + * Gets a parameter or the closure defining an object. | |
| 185 | + * | |
| 186 | + * @param string $id The unique identifier for the parameter or object | |
| 187 | + * | |
| 188 | + * @return mixed The value of the parameter or the closure defining an object | |
| 189 | + * | |
| 190 | + * @throws \InvalidArgumentException if the identifier is not defined | |
| 191 | + */ | |
| 192 | + public function raw( $id ) { | |
| 193 | + if ( ! isset( $this->keys[ $id ] ) ) { | |
| 194 | + throw new \InvalidArgumentException( sprintf( 'Identifier "%s" is not defined.', $id ) ); | |
| 195 | + } | |
| 208 | 196 | |
| 209 | - return $this->values[$id]; | |
| 210 | - } | |
| 197 | + if ( isset( $this->raw[ $id ] ) ) { | |
| 198 | + return $this->raw[ $id ]; | |
| 199 | + } | |
| 211 | 200 | |
| 212 | - /** | |
| 213 | - * Extends an object definition. | |
| 214 | - * | |
| 215 | - * Useful when you want to extend an existing object definition, | |
| 216 | - * without necessarily loading that object. | |
| 217 | - * | |
| 218 | - * @param string $id The unique identifier for the object | |
| 219 | - * @param callable $callable A service definition to extend the original | |
| 220 | - * | |
| 221 | - * @return callable The wrapped callable | |
| 222 | - * | |
| 223 | - * @throws \InvalidArgumentException if the identifier is not defined or not a service definition | |
| 224 | - */ | |
| 225 | - public function extend($id, $callable) | |
| 226 | - { | |
| 227 | - if (!isset($this->keys[$id])) { | |
| 228 | - throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); | |
| 229 | - } | |
| 201 | + return $this->values[ $id ]; | |
| 202 | + } | |
| 230 | 203 | |
| 231 | - if (!is_object($this->values[$id]) || !method_exists($this->values[$id], '__invoke')) { | |
| 232 | - throw new \InvalidArgumentException(sprintf('Identifier "%s" does not contain an object definition.', $id)); | |
| 233 | - } | |
| 204 | + /** | |
| 205 | + * Extends an object definition. | |
| 206 | + * | |
| 207 | + * Useful when you want to extend an existing object definition, | |
| 208 | + * without necessarily loading that object. | |
| 209 | + * | |
| 210 | + * @param string $id The unique identifier for the object | |
| 211 | + * @param callable $callable A service definition to extend the original | |
| 212 | + * | |
| 213 | + * @return callable The wrapped callable | |
| 214 | + * | |
| 215 | + * @throws \InvalidArgumentException if the identifier is not defined or not a service definition | |
| 216 | + */ | |
| 217 | + public function extend( $id, $callable ) { | |
| 218 | + if ( ! isset( $this->keys[ $id ] ) ) { | |
| 219 | + throw new \InvalidArgumentException( sprintf( 'Identifier "%s" is not defined.', $id ) ); | |
| 220 | + } | |
| 234 | 221 | |
| 235 | - if (!is_object($callable) || !method_exists($callable, '__invoke')) { | |
| 236 | - throw new \InvalidArgumentException('Extension service definition is not a Closure or invokable object.'); | |
| 237 | - } | |
| 222 | + if ( ! is_object( $this->values[ $id ] ) || ! method_exists( $this->values[ $id ], '__invoke' ) ) { | |
| 223 | + throw new \InvalidArgumentException( sprintf( 'Identifier "%s" does not contain an object definition.', $id ) ); | |
| 224 | + } | |
| 238 | 225 | |
| 239 | - $factory = $this->values[$id]; | |
| 226 | + if ( ! is_object( $callable ) || ! method_exists( $callable, '__invoke' ) ) { | |
| 227 | + throw new \InvalidArgumentException( 'Extension service definition is not a Closure or invokable object.' ); | |
| 228 | + } | |
| 240 | 229 | |
| 241 | - $extended = function ($c) use ($callable, $factory) { | |
| 242 | - return $callable($factory($c), $c); | |
| 243 | - }; | |
| 230 | + $factory = $this->values[ $id ]; | |
| 244 | 231 | |
| 245 | - if (isset($this->factories[$factory])) { | |
| 246 | - $this->factories->detach($factory); | |
| 247 | - $this->factories->attach($extended); | |
| 248 | - } | |
| 232 | + $extended = function ( $c ) use ( $callable, $factory ) { | |
| 233 | + return $callable( $factory( $c ), $c ); | |
| 234 | + }; | |
| 249 | 235 | |
| 250 | - return $this[$id] = $extended; | |
| 251 | - } | |
| 236 | + if ( isset( $this->factories[ $factory ] ) ) { | |
| 237 | + $this->factories->detach( $factory ); | |
| 238 | + $this->factories->attach( $extended ); | |
| 239 | + } | |
| 252 | 240 | |
| 253 | - /** | |
| 254 | - * Returns all defined value names. | |
| 255 | - * | |
| 256 | - * @return array An array of value names | |
| 257 | - */ | |
| 258 | - public function keys() | |
| 259 | - { | |
| 260 | - return array_keys($this->values); | |
| 261 | - } | |
| 241 | + return $this[ $id ] = $extended; | |
| 242 | + } | |
| 262 | 243 | |
| 263 | - /** | |
| 264 | - * Registers a service provider. | |
| 265 | - * | |
| 266 | - * @param ServiceProviderInterface $provider A ServiceProviderInterface instance | |
| 267 | - * @param array $values An array of values that customizes the provider | |
| 268 | - * | |
| 269 | - * @return static | |
| 270 | - */ | |
| 271 | - public function register(ServiceProviderInterface $provider, array $values = array()) | |
| 272 | - { | |
| 273 | - $provider->register($this); | |
| 244 | + /** | |
| 245 | + * Returns all defined value names. | |
| 246 | + * | |
| 247 | + * @return array An array of value names | |
| 248 | + */ | |
| 249 | + public function keys() { | |
| 250 | + return array_keys( $this->values ); | |
| 251 | + } | |
| 274 | 252 | |
| 275 | - foreach ($values as $key => $value) { | |
| 276 | - $this[$key] = $value; | |
| 277 | - } | |
| 253 | + /** | |
| 254 | + * Registers a service provider. | |
| 255 | + * | |
| 256 | + * @param ServiceProviderInterface $provider A ServiceProviderInterface instance | |
| 257 | + * @param array $values An array of values that customizes the provider | |
| 258 | + * | |
| 259 | + * @return static | |
| 260 | + */ | |
| 261 | + public function register( ServiceProviderInterface $provider, array $values = array() ) { | |
| 262 | + $provider->register( $this ); | |
| 278 | 263 | |
| 279 | - return $this; | |
| 280 | - } | |
| 264 | + foreach ( $values as $key => $value ) { | |
| 265 | + $this[ $key ] = $value; | |
| 266 | + } | |
| 267 | + | |
| 268 | + return $this; | |
| 269 | + } | |
| 281 | 270 | } |