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\ContainerBuilder;
15 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
18 * This pass validates each definition individually only taking the information
19 * into account which is contained in the definition itself.
21 * Later passes can rely on the following, and specifically do not need to
22 * perform these checks themselves:
24 * - non synthetic, non abstract services always have a class set
25 * - synthetic services are always public
27 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
29 class CheckDefinitionValidityPass implements CompilerPassInterface
32 * Processes the ContainerBuilder to validate the Definition.
34 * @param ContainerBuilder $container
36 * @throws RuntimeException When the Definition is invalid
38 public function process(ContainerBuilder $container)
40 foreach ($container->getDefinitions() as $id => $definition) {
41 // synthetic service is public
42 if ($definition->isSynthetic() && !$definition->isPublic()) {
43 throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
46 // non-synthetic, non-abstract service has class
47 if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) {
48 if ($definition->getFactory()) {
49 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));
52 throw new RuntimeException(sprintf(
53 'The definition for "%s" has no class. If you intend to inject '
54 .'this service dynamically at runtime, please mark it as synthetic=true. '
55 .'If this is an abstract definition solely used by child definitions, '
56 .'please add abstract=true, otherwise specify a class to get rid of this error.',
61 // tag attribute values must be scalars
62 foreach ($definition->getTags() as $name => $tags) {
63 foreach ($tags as $attributes) {
64 foreach ($attributes as $attribute => $value) {
65 if (!is_scalar($value) && null !== $value) {
66 throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));