[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/Compiler/ -> CheckDefinitionValidityPass.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\DependencyInjection\Compiler;
  13  
  14  use Symfony\Component\DependencyInjection\ContainerBuilder;
  15  use Symfony\Component\DependencyInjection\ContainerInterface;
  16  use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  17  
  18  /**
  19   * This pass validates each definition individually only taking the information
  20   * into account which is contained in the definition itself.
  21   *
  22   * Later passes can rely on the following, and specifically do not need to
  23   * perform these checks themselves:
  24   *
  25   * - non synthetic, non abstract services always have a class set
  26   * - synthetic services are always public
  27   * - synthetic services are always of non-prototype scope
  28   * - shared services are always of non-prototype scope
  29   *
  30   * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  31   */
  32  class CheckDefinitionValidityPass implements CompilerPassInterface
  33  {
  34      /**
  35       * Processes the ContainerBuilder to validate the Definition.
  36       *
  37       * @throws RuntimeException When the Definition is invalid
  38       */
  39      public function process(ContainerBuilder $container)
  40      {
  41          foreach ($container->getDefinitions() as $id => $definition) {
  42              // synthetic service is public
  43              if ($definition->isSynthetic() && !$definition->isPublic()) {
  44                  throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
  45              }
  46  
  47              // synthetic service has non-prototype scope
  48              if ($definition->isSynthetic() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
  49                  throw new RuntimeException(sprintf('A synthetic service ("%s") cannot be of scope "prototype".', $id));
  50              }
  51  
  52              // shared service has non-prototype scope
  53              if ($definition->isShared() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
  54                  throw new RuntimeException(sprintf('A shared service ("%s") cannot be of scope "prototype".', $id));
  55              }
  56  
  57              if ($definition->getFactory() && ($definition->getFactoryClass(false) || $definition->getFactoryService(false) || $definition->getFactoryMethod(false))) {
  58                  throw new RuntimeException(sprintf('A service ("%s") can use either the old or the new factory syntax, not both.', $id));
  59              }
  60  
  61              // non-synthetic, non-abstract service has class
  62              if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) {
  63                  if ($definition->getFactory() || $definition->getFactoryClass(false) || $definition->getFactoryService(false)) {
  64                      throw new RuntimeException(sprintf('Please add the class to service "%s" even if it is constructed by a factory since we might need to add method calls based on compile-time checks.', $id));
  65                  }
  66  
  67                  throw new RuntimeException(sprintf('The definition for "%s" has no class. If you intend to inject this service dynamically at runtime, please mark it as synthetic=true. If this is an abstract definition solely used by child definitions, please add abstract=true, otherwise specify a class to get rid of this error.', $id));
  68              }
  69  
  70              // tag attribute values must be scalars
  71              foreach ($definition->getTags() as $name => $tags) {
  72                  foreach ($tags as $attributes) {
  73                      foreach ($attributes as $attribute => $value) {
  74                          if (!is_scalar($value) && null !== $value) {
  75                              throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));
  76                          }
  77                      }
  78                  }
  79              }
  80          }
  81      }
  82  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1