|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the PHPBench package |
| 5 | + * |
| 6 | + * (c) Daniel Leech <daniel@dantleech.com> |
| 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 PhpBench\DependencyInjection; |
| 13 | + |
| 14 | +use Interop\Container\ContainerInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * PHPBench Container. |
| 18 | + * |
| 19 | + * This is a simple, extendable, closure based dependency injection container. |
| 20 | + */ |
| 21 | +class Container implements ContainerInterface |
| 22 | +{ |
| 23 | + private $instantiators = []; |
| 24 | + private $services = []; |
| 25 | + private $tags = []; |
| 26 | + private $config = []; |
| 27 | + private $userConfig = []; |
| 28 | + |
| 29 | + private $extensionClasses = []; |
| 30 | + |
| 31 | + public function __construct(array $extensionClasses = [], array $userConfig = []) |
| 32 | + { |
| 33 | + $this->extensionClasses = $extensionClasses; |
| 34 | + $this->userConfig = $userConfig; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Configure the container. This method will call the `configure()` method |
| 39 | + * on each extension. Extensions must use this opportunity to register their |
| 40 | + * services and define any default config. |
| 41 | + * |
| 42 | + * This method must be called before `build()`. |
| 43 | + */ |
| 44 | + public function init() |
| 45 | + { |
| 46 | + $extensions = []; |
| 47 | + |
| 48 | + if (empty($this->extensionClasses) && empty($this->userConfig)) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + foreach ($this->extensionClasses as $extensionClass) { |
| 53 | + if (!class_exists($extensionClass)) { |
| 54 | + throw new \InvalidArgumentException(sprintf( |
| 55 | + 'Extension class "%s" does not exist', |
| 56 | + $extensionClass |
| 57 | + )); |
| 58 | + } |
| 59 | + |
| 60 | + $extension = new $extensionClass(); |
| 61 | + |
| 62 | + if (!$extension instanceof ExtensionInterface) { |
| 63 | + throw new \InvalidArgumentException(sprintf( |
| 64 | + // add any manually specified extensions |
| 65 | + 'Extension "%s" must implement the PhpBench\\Extension interface', |
| 66 | + get_class($extension) |
| 67 | + )); |
| 68 | + } |
| 69 | + |
| 70 | + $extensions[] = $extension; |
| 71 | + |
| 72 | + $this->config = array_merge( |
| 73 | + $this->config, |
| 74 | + $extension->getDefaultConfig() |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + $diff = array_diff(array_keys($this->userConfig), array_keys($this->config)); |
| 79 | + |
| 80 | + if ($diff) { |
| 81 | + throw new \InvalidArgumentException(sprintf( |
| 82 | + 'Unknown configuration keys: "%s". Permitted keys: "%s"', |
| 83 | + implode('", "', $diff), implode('", "', array_keys($this->config)) |
| 84 | + )); |
| 85 | + } |
| 86 | + |
| 87 | + $this->config = array_merge( |
| 88 | + $this->config, |
| 89 | + $this->userConfig |
| 90 | + ); |
| 91 | + |
| 92 | + foreach ($extensions as $extension) { |
| 93 | + $extension->load($this); |
| 94 | + } |
| 95 | + |
| 96 | + foreach ($extensions as $extension) { |
| 97 | + $extension->build($this); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Instantiate and return the service with the given ID. |
| 103 | + * Note that this method will return the same instance on subsequent calls. |
| 104 | + * |
| 105 | + * @param string $serviceId |
| 106 | + * |
| 107 | + * @return mixed |
| 108 | + */ |
| 109 | + public function get($serviceId) |
| 110 | + { |
| 111 | + if (isset($this->services[$serviceId])) { |
| 112 | + return $this->services[$serviceId]; |
| 113 | + } |
| 114 | + |
| 115 | + if (!isset($this->instantiators[$serviceId])) { |
| 116 | + throw new \InvalidArgumentException(sprintf( |
| 117 | + 'No instantiator has been registered for requested service "%s"', |
| 118 | + $serviceId |
| 119 | + )); |
| 120 | + } |
| 121 | + |
| 122 | + $this->services[$serviceId] = $this->instantiators[$serviceId]($this); |
| 123 | + |
| 124 | + return $this->services[$serviceId]; |
| 125 | + } |
| 126 | + |
| 127 | + public function has($serviceId) |
| 128 | + { |
| 129 | + return isset($this->instantiators[$serviceId]); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Set a service instance. |
| 134 | + * |
| 135 | + * @param string $serviceId |
| 136 | + * @param mixed $instance |
| 137 | + */ |
| 138 | + public function set($serviceId, $instance) |
| 139 | + { |
| 140 | + $this->services[$serviceId] = $instance; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Return services IDs for the given tag. |
| 145 | + * |
| 146 | + * @param string $tag |
| 147 | + * |
| 148 | + * @return string[] |
| 149 | + */ |
| 150 | + public function getServiceIdsForTag($tag) |
| 151 | + { |
| 152 | + $serviceIds = []; |
| 153 | + foreach ($this->tags as $serviceId => $tags) { |
| 154 | + if (isset($tags[$tag])) { |
| 155 | + $serviceIds[$serviceId] = $tags[$tag]; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return $serviceIds; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Register a service with the given ID and instantiator. |
| 164 | + * |
| 165 | + * The instantiator is a closure which accepts an instance of this container and |
| 166 | + * returns a new instance of the service class. |
| 167 | + * |
| 168 | + * @param string $serviceId |
| 169 | + * @param \Closure $instantiator |
| 170 | + * @param string[] $tags |
| 171 | + */ |
| 172 | + public function register($serviceId, \Closure $instantiator, array $tags = []) |
| 173 | + { |
| 174 | + if (isset($this->instantiators[$serviceId])) { |
| 175 | + throw new \InvalidArgumentException(sprintf( |
| 176 | + 'Service with ID "%s" has already been registered', $serviceId)); |
| 177 | + } |
| 178 | + |
| 179 | + $this->instantiators[$serviceId] = $instantiator; |
| 180 | + $this->tags[$serviceId] = $tags; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Set the value of the parameter with the given name. |
| 185 | + * |
| 186 | + * @param string $name |
| 187 | + * @param mixed $value |
| 188 | + */ |
| 189 | + public function setParameter($name, $value) |
| 190 | + { |
| 191 | + $this->config[$name] = $value; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Return the parameter with the given name. |
| 196 | + * |
| 197 | + * @param string $name |
| 198 | + * |
| 199 | + * @throws \InvalidArgumentException |
| 200 | + * |
| 201 | + * @return mixed |
| 202 | + */ |
| 203 | + public function getParameter($name) |
| 204 | + { |
| 205 | + if (!array_key_exists($name, $this->config)) { |
| 206 | + throw new \InvalidArgumentException(sprintf( |
| 207 | + 'Parameter "%s" has not been registered', |
| 208 | + $name |
| 209 | + )); |
| 210 | + } |
| 211 | + |
| 212 | + return $this->config[$name]; |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Return true if the named parameter exists. |
| 217 | + * |
| 218 | + * @param string $name |
| 219 | + * |
| 220 | + * @return bool |
| 221 | + */ |
| 222 | + public function hasParameter($name) |
| 223 | + { |
| 224 | + return array_key_exists($name, $this->config); |
| 225 | + } |
| 226 | +} |
0 commit comments