annotate vendor/symfony/dependency-injection/Container.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\DependencyInjection;
Chris@0 13
Chris@0 14 use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
Chris@0 15 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
Chris@0 16 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
Chris@0 17 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
Chris@0 18 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
Chris@0 19 use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
Chris@0 20 use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
Chris@0 21
Chris@0 22 /**
Chris@0 23 * Container is a dependency injection container.
Chris@0 24 *
Chris@0 25 * It gives access to object instances (services).
Chris@0 26 *
Chris@0 27 * Services and parameters are simple key/pair stores.
Chris@0 28 *
Chris@0 29 * Parameter and service keys are case insensitive.
Chris@0 30 *
Chris@0 31 * A service id can contain lowercased letters, digits, underscores, and dots.
Chris@0 32 * Underscores are used to separate words, and dots to group services
Chris@0 33 * under namespaces:
Chris@0 34 *
Chris@0 35 * <ul>
Chris@0 36 * <li>request</li>
Chris@0 37 * <li>mysql_session_storage</li>
Chris@0 38 * <li>symfony.mysql_session_storage</li>
Chris@0 39 * </ul>
Chris@0 40 *
Chris@0 41 * A service can also be defined by creating a method named
Chris@0 42 * getXXXService(), where XXX is the camelized version of the id:
Chris@0 43 *
Chris@0 44 * <ul>
Chris@0 45 * <li>request -> getRequestService()</li>
Chris@0 46 * <li>mysql_session_storage -> getMysqlSessionStorageService()</li>
Chris@0 47 * <li>symfony.mysql_session_storage -> getSymfony_MysqlSessionStorageService()</li>
Chris@0 48 * </ul>
Chris@0 49 *
Chris@0 50 * The container can have three possible behaviors when a service does not exist:
Chris@0 51 *
Chris@0 52 * * EXCEPTION_ON_INVALID_REFERENCE: Throws an exception (the default)
Chris@0 53 * * NULL_ON_INVALID_REFERENCE: Returns null
Chris@0 54 * * IGNORE_ON_INVALID_REFERENCE: Ignores the wrapping command asking for the reference
Chris@0 55 * (for instance, ignore a setter if the service does not exist)
Chris@0 56 *
Chris@0 57 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 58 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
Chris@0 59 */
Chris@0 60 class Container implements ResettableContainerInterface
Chris@0 61 {
Chris@0 62 /**
Chris@0 63 * @var ParameterBagInterface
Chris@0 64 */
Chris@0 65 protected $parameterBag;
Chris@0 66
Chris@0 67 protected $services = array();
Chris@0 68 protected $methodMap = array();
Chris@0 69 protected $privates = array();
Chris@0 70 protected $aliases = array();
Chris@0 71 protected $loading = array();
Chris@0 72
Chris@0 73 private $underscoreMap = array('_' => '', '.' => '_', '\\' => '_');
Chris@0 74 private $envCache = array();
Chris@0 75
Chris@0 76 /**
Chris@0 77 * @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
Chris@0 78 */
Chris@0 79 public function __construct(ParameterBagInterface $parameterBag = null)
Chris@0 80 {
Chris@0 81 $this->parameterBag = $parameterBag ?: new EnvPlaceholderParameterBag();
Chris@0 82 }
Chris@0 83
Chris@0 84 /**
Chris@0 85 * Compiles the container.
Chris@0 86 *
Chris@0 87 * This method does two things:
Chris@0 88 *
Chris@0 89 * * Parameter values are resolved;
Chris@0 90 * * The parameter bag is frozen.
Chris@0 91 */
Chris@0 92 public function compile()
Chris@0 93 {
Chris@0 94 $this->parameterBag->resolve();
Chris@0 95
Chris@0 96 $this->parameterBag = new FrozenParameterBag($this->parameterBag->all());
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * Returns true if the container parameter bag are frozen.
Chris@0 101 *
Chris@0 102 * @return bool true if the container parameter bag are frozen, false otherwise
Chris@0 103 */
Chris@0 104 public function isFrozen()
Chris@0 105 {
Chris@0 106 return $this->parameterBag instanceof FrozenParameterBag;
Chris@0 107 }
Chris@0 108
Chris@0 109 /**
Chris@0 110 * Gets the service container parameter bag.
Chris@0 111 *
Chris@0 112 * @return ParameterBagInterface A ParameterBagInterface instance
Chris@0 113 */
Chris@0 114 public function getParameterBag()
Chris@0 115 {
Chris@0 116 return $this->parameterBag;
Chris@0 117 }
Chris@0 118
Chris@0 119 /**
Chris@0 120 * Gets a parameter.
Chris@0 121 *
Chris@0 122 * @param string $name The parameter name
Chris@0 123 *
Chris@0 124 * @return mixed The parameter value
Chris@0 125 *
Chris@0 126 * @throws InvalidArgumentException if the parameter is not defined
Chris@0 127 */
Chris@0 128 public function getParameter($name)
Chris@0 129 {
Chris@0 130 return $this->parameterBag->get($name);
Chris@0 131 }
Chris@0 132
Chris@0 133 /**
Chris@0 134 * Checks if a parameter exists.
Chris@0 135 *
Chris@0 136 * @param string $name The parameter name
Chris@0 137 *
Chris@0 138 * @return bool The presence of parameter in container
Chris@0 139 */
Chris@0 140 public function hasParameter($name)
Chris@0 141 {
Chris@0 142 return $this->parameterBag->has($name);
Chris@0 143 }
Chris@0 144
Chris@0 145 /**
Chris@0 146 * Sets a parameter.
Chris@0 147 *
Chris@0 148 * @param string $name The parameter name
Chris@0 149 * @param mixed $value The parameter value
Chris@0 150 */
Chris@0 151 public function setParameter($name, $value)
Chris@0 152 {
Chris@0 153 $this->parameterBag->set($name, $value);
Chris@0 154 }
Chris@0 155
Chris@0 156 /**
Chris@0 157 * Sets a service.
Chris@0 158 *
Chris@0 159 * Setting a service to null resets the service: has() returns false and get()
Chris@0 160 * behaves in the same way as if the service was never created.
Chris@0 161 *
Chris@0 162 * @param string $id The service identifier
Chris@0 163 * @param object $service The service instance
Chris@0 164 */
Chris@0 165 public function set($id, $service)
Chris@0 166 {
Chris@0 167 $id = strtolower($id);
Chris@0 168
Chris@0 169 if ('service_container' === $id) {
Chris@0 170 throw new InvalidArgumentException('You cannot set service "service_container".');
Chris@0 171 }
Chris@0 172
Chris@0 173 if (isset($this->aliases[$id])) {
Chris@0 174 unset($this->aliases[$id]);
Chris@0 175 }
Chris@0 176
Chris@0 177 $this->services[$id] = $service;
Chris@0 178
Chris@0 179 if (null === $service) {
Chris@0 180 unset($this->services[$id]);
Chris@0 181 }
Chris@0 182
Chris@0 183 if (isset($this->privates[$id])) {
Chris@0 184 if (null === $service) {
Chris@0 185 @trigger_error(sprintf('Unsetting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
Chris@0 186 unset($this->privates[$id]);
Chris@0 187 } else {
Chris@0 188 @trigger_error(sprintf('Setting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0. A new public service will be created instead.', $id), E_USER_DEPRECATED);
Chris@0 189 }
Chris@0 190 }
Chris@0 191 }
Chris@0 192
Chris@0 193 /**
Chris@0 194 * Returns true if the given service is defined.
Chris@0 195 *
Chris@0 196 * @param string $id The service identifier
Chris@0 197 *
Chris@0 198 * @return bool true if the service is defined, false otherwise
Chris@0 199 */
Chris@0 200 public function has($id)
Chris@0 201 {
Chris@0 202 for ($i = 2;;) {
Chris@0 203 if ('service_container' === $id
Chris@0 204 || isset($this->aliases[$id])
Chris@0 205 || isset($this->services[$id])
Chris@0 206 ) {
Chris@0 207 return true;
Chris@0 208 }
Chris@0 209
Chris@0 210 if (isset($this->privates[$id])) {
Chris@0 211 @trigger_error(sprintf('Checking for the existence of the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
Chris@0 212 }
Chris@0 213
Chris@0 214 if (isset($this->methodMap[$id])) {
Chris@0 215 return true;
Chris@0 216 }
Chris@0 217
Chris@0 218 if (--$i && $id !== $lcId = strtolower($id)) {
Chris@0 219 $id = $lcId;
Chris@0 220 continue;
Chris@0 221 }
Chris@0 222
Chris@0 223 // We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder,
Chris@0 224 // and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper)
Chris@0 225 if (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class && method_exists($this, 'get'.strtr($id, $this->underscoreMap).'Service')) {
Chris@0 226 @trigger_error('Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.', E_USER_DEPRECATED);
Chris@0 227
Chris@0 228 return true;
Chris@0 229 }
Chris@0 230
Chris@0 231 return false;
Chris@0 232 }
Chris@0 233 }
Chris@0 234
Chris@0 235 /**
Chris@0 236 * Gets a service.
Chris@0 237 *
Chris@0 238 * If a service is defined both through a set() method and
Chris@0 239 * with a get{$id}Service() method, the former has always precedence.
Chris@0 240 *
Chris@0 241 * @param string $id The service identifier
Chris@0 242 * @param int $invalidBehavior The behavior when the service does not exist
Chris@0 243 *
Chris@0 244 * @return object The associated service
Chris@0 245 *
Chris@0 246 * @throws ServiceCircularReferenceException When a circular reference is detected
Chris@0 247 * @throws ServiceNotFoundException When the service is not defined
Chris@0 248 * @throws \Exception if an exception has been thrown when the service has been resolved
Chris@0 249 *
Chris@0 250 * @see Reference
Chris@0 251 */
Chris@0 252 public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
Chris@0 253 {
Chris@0 254 // Attempt to retrieve the service by checking first aliases then
Chris@0 255 // available services. Service IDs are case insensitive, however since
Chris@0 256 // this method can be called thousands of times during a request, avoid
Chris@0 257 // calling strtolower() unless necessary.
Chris@0 258 for ($i = 2;;) {
Chris@0 259 if ('service_container' === $id) {
Chris@0 260 return $this;
Chris@0 261 }
Chris@0 262 if (isset($this->aliases[$id])) {
Chris@0 263 $id = $this->aliases[$id];
Chris@0 264 }
Chris@0 265 // Re-use shared service instance if it exists.
Chris@0 266 if (isset($this->services[$id])) {
Chris@0 267 return $this->services[$id];
Chris@0 268 }
Chris@0 269
Chris@0 270 if (isset($this->loading[$id])) {
Chris@0 271 throw new ServiceCircularReferenceException($id, array_keys($this->loading));
Chris@0 272 }
Chris@0 273
Chris@0 274 if (isset($this->methodMap[$id])) {
Chris@0 275 $method = $this->methodMap[$id];
Chris@0 276 } elseif (--$i && $id !== $lcId = strtolower($id)) {
Chris@0 277 $id = $lcId;
Chris@0 278 continue;
Chris@0 279 } elseif (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class && method_exists($this, $method = 'get'.strtr($id, $this->underscoreMap).'Service')) {
Chris@0 280 // We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder,
Chris@0 281 // and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper)
Chris@0 282 @trigger_error('Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.', E_USER_DEPRECATED);
Chris@0 283 // $method is set to the right value, proceed
Chris@0 284 } else {
Chris@0 285 if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
Chris@0 286 if (!$id) {
Chris@0 287 throw new ServiceNotFoundException($id);
Chris@0 288 }
Chris@0 289
Chris@0 290 $alternatives = array();
Chris@0 291 foreach ($this->getServiceIds() as $knownId) {
Chris@0 292 $lev = levenshtein($id, $knownId);
Chris@0 293 if ($lev <= strlen($id) / 3 || false !== strpos($knownId, $id)) {
Chris@0 294 $alternatives[] = $knownId;
Chris@0 295 }
Chris@0 296 }
Chris@0 297
Chris@0 298 throw new ServiceNotFoundException($id, null, null, $alternatives);
Chris@0 299 }
Chris@0 300
Chris@0 301 return;
Chris@0 302 }
Chris@0 303 if (isset($this->privates[$id])) {
Chris@0 304 @trigger_error(sprintf('Requesting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
Chris@0 305 }
Chris@0 306
Chris@0 307 $this->loading[$id] = true;
Chris@0 308
Chris@0 309 try {
Chris@0 310 $service = $this->$method();
Chris@0 311 } catch (\Exception $e) {
Chris@0 312 unset($this->services[$id]);
Chris@0 313
Chris@0 314 throw $e;
Chris@0 315 } finally {
Chris@0 316 unset($this->loading[$id]);
Chris@0 317 }
Chris@0 318
Chris@0 319 return $service;
Chris@0 320 }
Chris@0 321 }
Chris@0 322
Chris@0 323 /**
Chris@0 324 * Returns true if the given service has actually been initialized.
Chris@0 325 *
Chris@0 326 * @param string $id The service identifier
Chris@0 327 *
Chris@0 328 * @return bool true if service has already been initialized, false otherwise
Chris@0 329 */
Chris@0 330 public function initialized($id)
Chris@0 331 {
Chris@0 332 $id = strtolower($id);
Chris@0 333
Chris@0 334 if ('service_container' === $id) {
Chris@0 335 return false;
Chris@0 336 }
Chris@0 337
Chris@0 338 if (isset($this->aliases[$id])) {
Chris@0 339 $id = $this->aliases[$id];
Chris@0 340 }
Chris@0 341
Chris@0 342 return isset($this->services[$id]);
Chris@0 343 }
Chris@0 344
Chris@0 345 /**
Chris@0 346 * {@inheritdoc}
Chris@0 347 */
Chris@0 348 public function reset()
Chris@0 349 {
Chris@0 350 $this->services = array();
Chris@0 351 }
Chris@0 352
Chris@0 353 /**
Chris@0 354 * Gets all service ids.
Chris@0 355 *
Chris@0 356 * @return array An array of all defined service ids
Chris@0 357 */
Chris@0 358 public function getServiceIds()
Chris@0 359 {
Chris@0 360 $ids = array();
Chris@0 361
Chris@0 362 if (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class) {
Chris@0 363 // We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder,
Chris@0 364 // and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper)
Chris@0 365 @trigger_error('Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.', E_USER_DEPRECATED);
Chris@0 366
Chris@0 367 foreach (get_class_methods($this) as $method) {
Chris@0 368 if (preg_match('/^get(.+)Service$/', $method, $match)) {
Chris@0 369 $ids[] = self::underscore($match[1]);
Chris@0 370 }
Chris@0 371 }
Chris@0 372 }
Chris@0 373 $ids[] = 'service_container';
Chris@0 374
Chris@0 375 return array_unique(array_merge($ids, array_keys($this->methodMap), array_keys($this->services)));
Chris@0 376 }
Chris@0 377
Chris@0 378 /**
Chris@0 379 * Camelizes a string.
Chris@0 380 *
Chris@0 381 * @param string $id A string to camelize
Chris@0 382 *
Chris@0 383 * @return string The camelized string
Chris@0 384 */
Chris@0 385 public static function camelize($id)
Chris@0 386 {
Chris@0 387 return strtr(ucwords(strtr($id, array('_' => ' ', '.' => '_ ', '\\' => '_ '))), array(' ' => ''));
Chris@0 388 }
Chris@0 389
Chris@0 390 /**
Chris@0 391 * A string to underscore.
Chris@0 392 *
Chris@0 393 * @param string $id The string to underscore
Chris@0 394 *
Chris@0 395 * @return string The underscored string
Chris@0 396 */
Chris@0 397 public static function underscore($id)
Chris@0 398 {
Chris@0 399 return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), str_replace('_', '.', $id)));
Chris@0 400 }
Chris@0 401
Chris@0 402 /**
Chris@0 403 * Fetches a variable from the environment.
Chris@0 404 *
Chris@0 405 * @param string The name of the environment variable
Chris@0 406 *
Chris@0 407 * @return scalar The value to use for the provided environment variable name
Chris@0 408 *
Chris@0 409 * @throws EnvNotFoundException When the environment variable is not found and has no default value
Chris@0 410 */
Chris@0 411 protected function getEnv($name)
Chris@0 412 {
Chris@0 413 if (isset($this->envCache[$name]) || array_key_exists($name, $this->envCache)) {
Chris@0 414 return $this->envCache[$name];
Chris@0 415 }
Chris@0 416 if (isset($_ENV[$name])) {
Chris@0 417 return $this->envCache[$name] = $_ENV[$name];
Chris@0 418 }
Chris@0 419 if (false !== $env = getenv($name)) {
Chris@0 420 return $this->envCache[$name] = $env;
Chris@0 421 }
Chris@0 422 if (!$this->hasParameter("env($name)")) {
Chris@0 423 throw new EnvNotFoundException($name);
Chris@0 424 }
Chris@0 425
Chris@0 426 return $this->envCache[$name] = $this->getParameter("env($name)");
Chris@0 427 }
Chris@0 428
Chris@0 429 private function __clone()
Chris@0 430 {
Chris@0 431 }
Chris@0 432 }