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 if (0 === strpos($id, '\\') && 1 < substr_count($id, '\\')) {
52 throw new RuntimeException(sprintf(
53 'The definition for "%s" has no class attribute, and appears to reference a class or interface. '
54 .'Please specify the class attribute explicitly or remove the leading backslash by renaming '
55 .'the service to "%s" to get rid of this error.',
60 throw new RuntimeException(sprintf(
61 'The definition for "%s" has no class attribute, and appears to reference a '
62 .'class or interface in the global namespace. Leaving out the "class" attribute '
63 .'is only allowed for namespaced classes. Please specify the class attribute '
64 .'explicitly to get rid of this error.',
69 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));
72 // tag attribute values must be scalars
73 foreach ($definition->getTags() as $name => $tags) {
74 foreach ($tags as $attributes) {
75 foreach ($attributes as $attribute => $value) {
76 if (!is_scalar($value) && null !== $value) {
77 throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));
83 if ($definition->isPublic() && !$definition->isPrivate()) {
84 $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
85 if (null !== $usedEnvs) {
86 throw new EnvParameterException(array($resolvedId), null, 'A service name ("%s") cannot contain dynamic values.');
91 foreach ($container->getAliases() as $id => $alias) {
92 if ($alias->isPublic() && !$alias->isPrivate()) {
93 $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
94 if (null !== $usedEnvs) {
95 throw new EnvParameterException(array($resolvedId), null, 'An alias name ("%s") cannot contain dynamic values.');