4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
16 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
19 * This pass validates each definition individually only taking the information
20 * into account which is contained in the definition itself.
22 * Later passes can rely on the following, and specifically do not need to
23 * perform these checks themselves:
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
30 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
32 class CheckDefinitionValidityPass implements CompilerPassInterface
35 * Processes the ContainerBuilder to validate the Definition.
37 * @param ContainerBuilder $container
39 * @throws RuntimeException When the Definition is invalid
41 public function process(ContainerBuilder $container)
43 foreach ($container->getDefinitions() as $id => $definition) {
44 // synthetic service is public
45 if ($definition->isSynthetic() && !$definition->isPublic()) {
46 throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
49 // synthetic service has non-prototype scope
50 if ($definition->isSynthetic() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
51 throw new RuntimeException(sprintf('A synthetic service ("%s") cannot be of scope "prototype".', $id));
54 // shared service has non-prototype scope
55 if ($definition->isShared() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
56 throw new RuntimeException(sprintf('A shared service ("%s") cannot be of scope "prototype".', $id));
59 if ($definition->getFactory() && ($definition->getFactoryClass(false) || $definition->getFactoryService(false) || $definition->getFactoryMethod(false))) {
60 throw new RuntimeException(sprintf('A service ("%s") can use either the old or the new factory syntax, not both.', $id));
63 // non-synthetic, non-abstract service has class
64 if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) {
65 if ($definition->getFactory() || $definition->getFactoryClass(false) || $definition->getFactoryService(false)) {
66 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));
69 throw new RuntimeException(sprintf(
70 'The definition for "%s" has no class. If you intend to inject '
71 .'this service dynamically at runtime, please mark it as synthetic=true. '
72 .'If this is an abstract definition solely used by child definitions, '
73 .'please add abstract=true, otherwise specify a class to get rid of this error.',
78 // tag attribute values must be scalars
79 foreach ($definition->getTags() as $name => $tags) {
80 foreach ($tags as $attributes) {
81 foreach ($attributes as $attribute => $value) {
82 if (!is_scalar($value) && null !== $value) {
83 throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));