You are here

public function CheckDefinitionValidityPass::process in Service Container 7

Same name and namespace in other branches
  1. 7.2 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Compiler/CheckDefinitionValidityPass.php \Symfony\Component\DependencyInjection\Compiler\CheckDefinitionValidityPass::process()

Processes the ContainerBuilder to validate the Definition.

Parameters

ContainerBuilder $container:

Throws

RuntimeException When the Definition is invalid

Overrides CompilerPassInterface::process

File

modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Compiler/CheckDefinitionValidityPass.php, line 40

Class

CheckDefinitionValidityPass
This pass validates each definition individually only taking the information into account which is contained in the definition itself.

Namespace

Symfony\Component\DependencyInjection\Compiler

Code

public function process(ContainerBuilder $container) {
  foreach ($container
    ->getDefinitions() as $id => $definition) {

    // synthetic service is public
    if ($definition
      ->isSynthetic() && !$definition
      ->isPublic()) {
      throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
    }

    // synthetic service has non-prototype scope
    if ($definition
      ->isSynthetic() && ContainerInterface::SCOPE_PROTOTYPE === $definition
      ->getScope()) {
      throw new RuntimeException(sprintf('A synthetic service ("%s") cannot be of scope "prototype".', $id));
    }
    if ($definition
      ->getFactory() && ($definition
      ->getFactoryClass(false) || $definition
      ->getFactoryService(false) || $definition
      ->getFactoryMethod(false))) {
      throw new RuntimeException(sprintf('A service ("%s") can use either the old or the new factory syntax, not both.', $id));
    }

    // non-synthetic, non-abstract service has class
    if (!$definition
      ->isAbstract() && !$definition
      ->isSynthetic() && !$definition
      ->getClass()) {
      if ($definition
        ->getFactory() || $definition
        ->getFactoryClass(false) || $definition
        ->getFactoryService(false)) {
        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));
      }
      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));
    }

    // tag attribute values must be scalars
    foreach ($definition
      ->getTags() as $name => $tags) {
      foreach ($tags as $attributes) {
        foreach ($attributes as $attribute => $value) {
          if (!is_scalar($value) && null !== $value) {
            throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));
          }
        }
      }
    }
  }
}