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\EnvParameterException;
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
28 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
30 class CheckDefinitionValidityPass implements CompilerPassInterface
33 * Processes the ContainerBuilder to validate the Definition.
35 * @throws RuntimeException When the Definition is invalid
37 public function process(ContainerBuilder $container)
39 foreach ($container->getDefinitions() as $id => $definition) {
40 // synthetic service is public
41 if ($definition->isSynthetic() && !($definition->isPublic() || $definition->isPrivate())) {
42 throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
45 // non-synthetic, non-abstract service has class
46 if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) {
47 if ($definition->getFactory()) {
48 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));
50 if (class_exists($id) || interface_exists($id, false)) {
51 throw new RuntimeException(sprintf(
52 'The definition for "%s" has no class attribute, and appears to reference a '
53 .'class or interface in the global namespace. Leaving out the "class" attribute '
54 .'is only allowed for namespaced classes. Please specify the class attribute '
55 .'explicitly to get rid of this error.',
60 throw new RuntimeException(sprintf(
61 'The definition for "%s" has no class. If you intend to inject '
62 .'this service dynamically at runtime, please mark it as synthetic=true. '
63 .'If this is an abstract definition solely used by child definitions, '
64 .'please add abstract=true, otherwise specify a class to get rid of this error.',
69 // tag attribute values must be scalars
70 foreach ($definition->getTags() as $name => $tags) {
71 foreach ($tags as $attributes) {
72 foreach ($attributes as $attribute => $value) {
73 if (!is_scalar($value) && null !== $value) {
74 throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));
80 if ($definition->isPublic() && !$definition->isPrivate()) {
81 $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
82 if (null !== $usedEnvs) {
83 throw new EnvParameterException(array($resolvedId), null, 'A service name ("%s") cannot contain dynamic values.');
88 foreach ($container->getAliases() as $id => $alias) {
89 if ($alias->isPublic() && !$alias->isPrivate()) {
90 $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
91 if (null !== $usedEnvs) {
92 throw new EnvParameterException(array($resolvedId), null, 'An alias name ("%s") cannot contain dynamic values.');