|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Container package. |
| 5 | + * |
| 6 | + * (c) Elliot Wright <elliot@elliotwright.co> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Container; |
| 13 | + |
| 14 | +use ArrayAccess; |
| 15 | +use Closure; |
| 16 | +use InvalidArgumentException; |
| 17 | +use SplObjectStorage; |
| 18 | + |
| 19 | +/** |
| 20 | + * Container |
| 21 | + * |
| 22 | + * @author Elliot Wright <elliot@elliotwright.co> |
| 23 | + */ |
| 24 | +class Container implements ArrayAccess, ContainerInterface |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var SplObjectStorage |
| 28 | + */ |
| 29 | + protected $factories; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var LockBox |
| 33 | + */ |
| 34 | + protected $parameters; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var LockBox |
| 38 | + */ |
| 39 | + protected $services; |
| 40 | + |
| 41 | + |
| 42 | + /** |
| 43 | + * Constructor |
| 44 | + */ |
| 45 | + public function __construct() |
| 46 | + { |
| 47 | + $this->factories = new SplObjectStorage(); |
| 48 | + $this->parameters = new LockBox(); |
| 49 | + $this->services = new LockBox(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Array access for setting parameters |
| 54 | + * |
| 55 | + * @param string $name |
| 56 | + * @param mixed $value |
| 57 | + */ |
| 58 | + public function offsetSet($name, $value) |
| 59 | + { |
| 60 | + $this->setParameter($name, $value); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Array access for getting parameters |
| 65 | + * |
| 66 | + * @param string $name |
| 67 | + * |
| 68 | + * @return mixed |
| 69 | + */ |
| 70 | + public function offsetGet($name) |
| 71 | + { |
| 72 | + return $this->getParameter($name); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Array item exists |
| 77 | + * |
| 78 | + * @param string $name |
| 79 | + * |
| 80 | + * @return boolean |
| 81 | + */ |
| 82 | + public function offsetExists($name) |
| 83 | + { |
| 84 | + return $this->hasParameter($name); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Array item unset |
| 89 | + * |
| 90 | + * @param string $name |
| 91 | + * |
| 92 | + * @return boolean |
| 93 | + */ |
| 94 | + public function offsetUnset($name) |
| 95 | + { |
| 96 | + return $this->removeParameter($name); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * {@inheritDoc} |
| 101 | + */ |
| 102 | + public function set($name, $value) |
| 103 | + { |
| 104 | + $this->services->set($name, $value); |
| 105 | + |
| 106 | + return $this; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * {@inheritDoc} |
| 111 | + */ |
| 112 | + public function get($name) |
| 113 | + { |
| 114 | + if (!$this->services->has($name)) { |
| 115 | + throw new InvalidArgumentException(sprintf('Service "%s" is not defined.', $name)); |
| 116 | + } |
| 117 | + |
| 118 | + /** @var Closure $service */ |
| 119 | + $service = $this->services->get($name); |
| 120 | + |
| 121 | + if ($this->services->isLocked($name) |
| 122 | + || !method_exists($this->services->get($name), '__invoke')) { |
| 123 | + return $service; |
| 124 | + } |
| 125 | + |
| 126 | + if ($this->factories->contains($service)) { |
| 127 | + return $service($this); |
| 128 | + } |
| 129 | + |
| 130 | + $this->services->set($name, $service($this)); |
| 131 | + $this->services->lock($name); |
| 132 | + |
| 133 | + return $this->services->get($name); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * {@inheritDoc} |
| 138 | + */ |
| 139 | + public function has($name) |
| 140 | + { |
| 141 | + return $this->services->has($name); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * {@inheritDoc} |
| 146 | + */ |
| 147 | + public function remove($name) |
| 148 | + { |
| 149 | + if ($this->services->has($name)) { |
| 150 | + if (is_object($this->services->get($name))) { |
| 151 | + unset($this->factories[$this->services->get($name)]); |
| 152 | + } |
| 153 | + |
| 154 | + $this->services->remove($name); |
| 155 | + } |
| 156 | + |
| 157 | + return $this; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * {@inheritDoc} |
| 162 | + */ |
| 163 | + public function setParameter($name, $value) |
| 164 | + { |
| 165 | + $this->parameters->set($name, $value); |
| 166 | + |
| 167 | + return $this; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * {@inheritDoc} |
| 172 | + */ |
| 173 | + public function getParameter($name) |
| 174 | + { |
| 175 | + if (!$this->parameters->has($name)) { |
| 176 | + throw new InvalidArgumentException(sprintf('Parameter "%s" is not defined.', $name)); |
| 177 | + } |
| 178 | + |
| 179 | + return $this->parameters->get($name); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * {@inheritDoc} |
| 184 | + */ |
| 185 | + public function hasParameter($name) |
| 186 | + { |
| 187 | + return $this->parameters->has($name); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * {@inheritDoc} |
| 192 | + */ |
| 193 | + public function removeParameter($name) |
| 194 | + { |
| 195 | + return $this->parameters->remove($name); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * All array access keys |
| 200 | + * |
| 201 | + * @return array |
| 202 | + */ |
| 203 | + public function keys() |
| 204 | + { |
| 205 | + return $this->services->contents(); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Marks a callable as being a factory service. |
| 210 | + * |
| 211 | + * @param callable $callable |
| 212 | + * |
| 213 | + * @return callable |
| 214 | + * |
| 215 | + * @throws InvalidArgumentException |
| 216 | + */ |
| 217 | + public function factory($callable) |
| 218 | + { |
| 219 | + if (!is_object($callable) || !method_exists($callable, '__invoke')) { |
| 220 | + throw new InvalidArgumentException('Service definition is not a Closure or invokable object.'); |
| 221 | + } |
| 222 | + |
| 223 | + $this->factories->attach($callable); |
| 224 | + |
| 225 | + return $callable; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Extends a service definition. |
| 230 | + * |
| 231 | + * @param string $name |
| 232 | + * @param callable $callable |
| 233 | + * @param boolean $strict |
| 234 | + * |
| 235 | + * @return callable |
| 236 | + * |
| 237 | + * @throws InvalidArgumentException |
| 238 | + */ |
| 239 | + public function extend($name, $callable, $strict = true) |
| 240 | + { |
| 241 | + if (!$this->services->has($name)) { |
| 242 | + if ($strict) { |
| 243 | + throw new InvalidArgumentException(sprintf('Service "%s" is not defined.', $name)); |
| 244 | + } else { |
| 245 | + return false; |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + $factory = $this->services->get($name); |
| 250 | + |
| 251 | + if (!is_object($factory) || !method_exists($factory, '__invoke')) { |
| 252 | + throw new InvalidArgumentException(sprintf('Service "%s" does not contain an object definition.', $name)); |
| 253 | + } |
| 254 | + |
| 255 | + if (!is_object($callable) || !method_exists($callable, '__invoke')) { |
| 256 | + throw new InvalidArgumentException('Extension service definition is not a Closure or invokable object.'); |
| 257 | + } |
| 258 | + |
| 259 | + $extended = function($c) use($callable, $factory) { |
| 260 | + /** @var Closure $factory */ |
| 261 | + return $callable($factory($c), $c); |
| 262 | + }; |
| 263 | + |
| 264 | + if ($this->factories->contains($factory)) { |
| 265 | + $this->factories->detach($factory); |
| 266 | + $this->factories->attach($extended); |
| 267 | + } |
| 268 | + |
| 269 | + $this->services->unlock($name); |
| 270 | + $this->services->set($name, $extended); |
| 271 | + |
| 272 | + return $this->services->get($name); |
| 273 | + } |
| 274 | +} |
0 commit comments