deployphp /
deployer
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | /* (c) Anton Medvedev <[email protected]> |
||||
| 6 | * |
||||
| 7 | * For the full copyright and license information, please view the LICENSE |
||||
| 8 | * file that was distributed with this source code. |
||||
| 9 | */ |
||||
| 10 | |||||
| 11 | namespace Deployer\Component\Pimple; |
||||
| 12 | |||||
| 13 | use Deployer\Component\Pimple\Exception\ExpectedInvokableException; |
||||
| 14 | use Deployer\Component\Pimple\Exception\FrozenServiceException; |
||||
| 15 | use Deployer\Component\Pimple\Exception\InvalidServiceIdentifierException; |
||||
| 16 | use Deployer\Component\Pimple\Exception\UnknownIdentifierException; |
||||
| 17 | |||||
| 18 | /** |
||||
| 19 | * Container main class. |
||||
| 20 | * |
||||
| 21 | * @author Fabien Potencier |
||||
| 22 | */ |
||||
| 23 | class Container implements \ArrayAccess |
||||
| 24 | { |
||||
| 25 | /** |
||||
| 26 | * @var array |
||||
| 27 | */ |
||||
| 28 | private $values = []; |
||||
| 29 | /** |
||||
| 30 | * @var \SplObjectStorage |
||||
| 31 | */ |
||||
| 32 | private $factories; |
||||
| 33 | /** |
||||
| 34 | * @var \SplObjectStorage |
||||
| 35 | */ |
||||
| 36 | private $protected; |
||||
| 37 | /** |
||||
| 38 | * @var array |
||||
| 39 | */ |
||||
| 40 | private $frozen = []; |
||||
| 41 | /** |
||||
| 42 | * @var array |
||||
| 43 | */ |
||||
| 44 | private $raw = []; |
||||
| 45 | /** |
||||
| 46 | * @var array |
||||
| 47 | */ |
||||
| 48 | private $keys = []; |
||||
| 49 | |||||
| 50 | /** |
||||
| 51 | * Instantiates the container. |
||||
| 52 | * |
||||
| 53 | * Objects and parameters can be passed as argument to the constructor. |
||||
| 54 | * |
||||
| 55 | * @param array $values The parameters or objects |
||||
| 56 | */ |
||||
| 57 | public function __construct(array $values = []) |
||||
| 58 | { |
||||
| 59 | $this->factories = new \SplObjectStorage(); |
||||
| 60 | $this->protected = new \SplObjectStorage(); |
||||
| 61 | |||||
| 62 | foreach ($values as $key => $value) { |
||||
| 63 | $this->offsetSet($key, $value); |
||||
| 64 | } |
||||
| 65 | } |
||||
| 66 | |||||
| 67 | /** |
||||
| 68 | * Sets a parameter or an object. |
||||
| 69 | * |
||||
| 70 | * Objects must be defined as Closures. |
||||
| 71 | * |
||||
| 72 | * Allowing any PHP callable leads to difficult to debug problems |
||||
| 73 | * as function names (strings) are callable (creating a function with |
||||
| 74 | * the same name as an existing parameter would break your container). |
||||
| 75 | * |
||||
| 76 | * @param string $id The unique identifier for the parameter or object |
||||
| 77 | * @param mixed $value The value of the parameter or a closure to define an object |
||||
| 78 | * |
||||
| 79 | * @throws FrozenServiceException Prevent override of a frozen service |
||||
| 80 | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint |
||||
| 81 | */ |
||||
| 82 | #[\ReturnTypeWillChange] |
||||
| 83 | public function offsetSet($id, $value) |
||||
| 84 | { |
||||
| 85 | if (isset($this->frozen[$id])) { |
||||
| 86 | throw new FrozenServiceException($id); |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | $this->values[$id] = $value; |
||||
| 90 | $this->keys[$id] = true; |
||||
| 91 | } |
||||
| 92 | |||||
| 93 | /** |
||||
| 94 | * Gets a parameter or an object. |
||||
| 95 | * |
||||
| 96 | * @param string $id The unique identifier for the parameter or object |
||||
| 97 | * |
||||
| 98 | * @return mixed The value of the parameter or an object |
||||
| 99 | * |
||||
| 100 | * @throws UnknownIdentifierException If the identifier is not defined |
||||
| 101 | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint |
||||
| 102 | */ |
||||
| 103 | #[\ReturnTypeWillChange] |
||||
| 104 | public function offsetGet($id) |
||||
| 105 | { |
||||
| 106 | if (!isset($this->keys[$id])) { |
||||
| 107 | throw new UnknownIdentifierException($id); |
||||
| 108 | } |
||||
| 109 | |||||
| 110 | if ( |
||||
| 111 | isset($this->raw[$id]) |
||||
| 112 | || !\is_object($this->values[$id]) |
||||
| 113 | || isset($this->protected[$this->values[$id]]) |
||||
| 114 | || !\method_exists($this->values[$id], '__invoke') |
||||
| 115 | ) { |
||||
| 116 | return $this->values[$id]; |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | if (isset($this->factories[$this->values[$id]])) { |
||||
| 120 | return $this->values[$id]($this); |
||||
| 121 | } |
||||
| 122 | |||||
| 123 | $raw = $this->values[$id]; |
||||
| 124 | $val = $this->values[$id] = $raw($this); |
||||
| 125 | $this->raw[$id] = $raw; |
||||
| 126 | |||||
| 127 | $this->frozen[$id] = true; |
||||
| 128 | |||||
| 129 | return $val; |
||||
| 130 | } |
||||
| 131 | |||||
| 132 | /** |
||||
| 133 | * Checks if a parameter or an object is set. |
||||
| 134 | * |
||||
| 135 | * @param string $id The unique identifier for the parameter or object |
||||
| 136 | * |
||||
| 137 | * @return bool |
||||
| 138 | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint |
||||
| 139 | */ |
||||
| 140 | #[\ReturnTypeWillChange] |
||||
| 141 | public function offsetExists($id) |
||||
| 142 | { |
||||
| 143 | return isset($this->keys[$id]); |
||||
| 144 | } |
||||
| 145 | |||||
| 146 | /** |
||||
| 147 | * Unsets a parameter or an object. |
||||
| 148 | * |
||||
| 149 | * @param string $id The unique identifier for the parameter or object |
||||
| 150 | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint |
||||
| 151 | */ |
||||
| 152 | #[\ReturnTypeWillChange] |
||||
| 153 | public function offsetUnset($id) |
||||
| 154 | { |
||||
| 155 | if (isset($this->keys[$id])) { |
||||
| 156 | if (\is_object($this->values[$id])) { |
||||
| 157 | unset($this->factories[$this->values[$id]], $this->protected[$this->values[$id]]); |
||||
| 158 | } |
||||
| 159 | |||||
| 160 | unset($this->values[$id], $this->frozen[$id], $this->raw[$id], $this->keys[$id]); |
||||
| 161 | } |
||||
| 162 | } |
||||
| 163 | |||||
| 164 | /** |
||||
| 165 | * Marks a callable as being a factory service. |
||||
| 166 | * |
||||
| 167 | * @param callable $callable A service definition to be used as a factory |
||||
| 168 | * |
||||
| 169 | * @return callable The passed callable |
||||
| 170 | * |
||||
| 171 | * @throws ExpectedInvokableException Service definition has to be a closure or an invokable object |
||||
| 172 | */ |
||||
| 173 | public function factory(callable $callable) |
||||
| 174 | { |
||||
| 175 | if (!\method_exists($callable, '__invoke')) { |
||||
| 176 | throw new ExpectedInvokableException('Service definition is not a Closure or invokable object.'); |
||||
| 177 | } |
||||
| 178 | |||||
| 179 | $this->factories->attach($callable); |
||||
| 180 | |||||
| 181 | return $callable; |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 182 | } |
||||
| 183 | |||||
| 184 | /** |
||||
| 185 | * Protects a callable from being interpreted as a service. |
||||
| 186 | * |
||||
| 187 | * This is useful when you want to store a callable as a parameter. |
||||
| 188 | * |
||||
| 189 | * @param callable $callable A callable to protect from being evaluated |
||||
| 190 | * |
||||
| 191 | * @return callable The passed callable |
||||
| 192 | * |
||||
| 193 | * @throws ExpectedInvokableException Service definition has to be a closure or an invokable object |
||||
| 194 | */ |
||||
| 195 | public function protect(callable $callable) |
||||
| 196 | { |
||||
| 197 | if (!\method_exists($callable, '__invoke')) { |
||||
| 198 | throw new ExpectedInvokableException('Callable is not a Closure or invokable object.'); |
||||
| 199 | } |
||||
| 200 | |||||
| 201 | $this->protected->attach($callable); |
||||
| 202 | |||||
| 203 | return $callable; |
||||
|
0 ignored issues
–
show
|
|||||
| 204 | } |
||||
| 205 | |||||
| 206 | /** |
||||
| 207 | * Gets a parameter or the closure defining an object. |
||||
| 208 | * |
||||
| 209 | * @param string $id The unique identifier for the parameter or object |
||||
| 210 | * |
||||
| 211 | * @return mixed The value of the parameter or the closure defining an object |
||||
| 212 | * |
||||
| 213 | * @throws UnknownIdentifierException If the identifier is not defined |
||||
| 214 | */ |
||||
| 215 | public function raw(string $id) |
||||
| 216 | { |
||||
| 217 | if (!isset($this->keys[$id])) { |
||||
| 218 | throw new UnknownIdentifierException($id); |
||||
| 219 | } |
||||
| 220 | |||||
| 221 | if (isset($this->raw[$id])) { |
||||
| 222 | return $this->raw[$id]; |
||||
| 223 | } |
||||
| 224 | |||||
| 225 | return $this->values[$id]; |
||||
| 226 | } |
||||
| 227 | |||||
| 228 | /** |
||||
| 229 | * Extends an object definition. |
||||
| 230 | * |
||||
| 231 | * Useful when you want to extend an existing object definition, |
||||
| 232 | * without necessarily loading that object. |
||||
| 233 | * |
||||
| 234 | * @param string $id The unique identifier for the object |
||||
| 235 | * @param callable $callable A service definition to extend the original |
||||
| 236 | * |
||||
| 237 | * @return callable The wrapped callable |
||||
| 238 | * |
||||
| 239 | * @throws UnknownIdentifierException If the identifier is not defined |
||||
| 240 | * @throws FrozenServiceException If the service is frozen |
||||
| 241 | * @throws InvalidServiceIdentifierException If the identifier belongs to a parameter |
||||
| 242 | * @throws ExpectedInvokableException If the extension callable is not a closure or an invokable object |
||||
| 243 | */ |
||||
| 244 | public function extend(string $id, callable $callable) |
||||
| 245 | { |
||||
| 246 | if (!isset($this->keys[$id])) { |
||||
| 247 | throw new UnknownIdentifierException($id); |
||||
| 248 | } |
||||
| 249 | |||||
| 250 | if (isset($this->frozen[$id])) { |
||||
| 251 | throw new FrozenServiceException($id); |
||||
| 252 | } |
||||
| 253 | |||||
| 254 | if (!\is_object($this->values[$id]) || !\method_exists($this->values[$id], '__invoke')) { |
||||
| 255 | throw new InvalidServiceIdentifierException($id); |
||||
| 256 | } |
||||
| 257 | |||||
| 258 | if (isset($this->protected[$this->values[$id]])) { |
||||
| 259 | @\trigger_error(\sprintf('How Pimple behaves when extending protected closures will be fixed in Pimple 4. Are you sure "%s" should be protected?', $id), E_USER_DEPRECATED); |
||||
| 260 | } |
||||
| 261 | |||||
| 262 | if (!\is_object($callable) || !\method_exists($callable, '__invoke')) { |
||||
| 263 | throw new ExpectedInvokableException('Extension service definition is not a Closure or invokable object.'); |
||||
| 264 | } |
||||
| 265 | |||||
| 266 | $factory = $this->values[$id]; |
||||
| 267 | |||||
| 268 | $extended = function ($c) use ($callable, $factory) { |
||||
| 269 | return $callable($factory($c), $c); |
||||
| 270 | }; |
||||
| 271 | |||||
| 272 | if (isset($this->factories[$factory])) { |
||||
| 273 | $this->factories->detach($factory); |
||||
| 274 | $this->factories->attach($extended); |
||||
|
0 ignored issues
–
show
$extended of type callable is incompatible with the type object expected by parameter $object of SplObjectStorage::attach().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 275 | } |
||||
| 276 | |||||
| 277 | return $this[$id] = $extended; |
||||
| 278 | } |
||||
| 279 | |||||
| 280 | /** |
||||
| 281 | * Returns all defined value names. |
||||
| 282 | * |
||||
| 283 | * @return array An array of value names |
||||
| 284 | */ |
||||
| 285 | public function keys() |
||||
| 286 | { |
||||
| 287 | return \array_keys($this->values); |
||||
| 288 | } |
||||
| 289 | } |
||||
| 290 |