Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\DependencyInjection\Compiler; Chris@0: Chris@0: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@14: use Symfony\Component\DependencyInjection\Exception\EnvParameterException; Chris@0: use Symfony\Component\DependencyInjection\Exception\RuntimeException; Chris@0: Chris@0: /** Chris@0: * This pass validates each definition individually only taking the information Chris@0: * into account which is contained in the definition itself. Chris@0: * Chris@0: * Later passes can rely on the following, and specifically do not need to Chris@0: * perform these checks themselves: Chris@0: * Chris@0: * - non synthetic, non abstract services always have a class set Chris@0: * - synthetic services are always public Chris@0: * Chris@0: * @author Johannes M. Schmitt Chris@0: */ Chris@0: class CheckDefinitionValidityPass implements CompilerPassInterface Chris@0: { Chris@0: /** Chris@0: * Processes the ContainerBuilder to validate the Definition. Chris@0: * Chris@0: * @throws RuntimeException When the Definition is invalid Chris@0: */ Chris@0: public function process(ContainerBuilder $container) Chris@0: { Chris@0: foreach ($container->getDefinitions() as $id => $definition) { Chris@0: // synthetic service is public Chris@14: if ($definition->isSynthetic() && !($definition->isPublic() || $definition->isPrivate())) { Chris@0: throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id)); Chris@0: } Chris@0: Chris@0: // non-synthetic, non-abstract service has class Chris@0: if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) { Chris@0: if ($definition->getFactory()) { Chris@0: 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)); Chris@0: } Chris@14: if (class_exists($id) || interface_exists($id, false)) { Chris@17: if (0 === strpos($id, '\\') && 1 < substr_count($id, '\\')) { Chris@17: throw new RuntimeException(sprintf( Chris@17: 'The definition for "%s" has no class attribute, and appears to reference a class or interface. ' Chris@17: .'Please specify the class attribute explicitly or remove the leading backslash by renaming ' Chris@17: .'the service to "%s" to get rid of this error.', Chris@17: $id, substr($id, 1) Chris@17: )); Chris@17: } Chris@17: Chris@14: throw new RuntimeException(sprintf( Chris@14: 'The definition for "%s" has no class attribute, and appears to reference a ' Chris@14: .'class or interface in the global namespace. Leaving out the "class" attribute ' Chris@14: .'is only allowed for namespaced classes. Please specify the class attribute ' Chris@14: .'explicitly to get rid of this error.', Chris@14: $id Chris@14: )); Chris@14: } Chris@0: Chris@17: 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)); Chris@0: } Chris@0: Chris@0: // tag attribute values must be scalars Chris@0: foreach ($definition->getTags() as $name => $tags) { Chris@0: foreach ($tags as $attributes) { Chris@0: foreach ($attributes as $attribute => $value) { Chris@0: if (!is_scalar($value) && null !== $value) { Chris@0: throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute)); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@14: Chris@14: if ($definition->isPublic() && !$definition->isPrivate()) { Chris@14: $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs); Chris@14: if (null !== $usedEnvs) { Chris@17: throw new EnvParameterException([$resolvedId], null, 'A service name ("%s") cannot contain dynamic values.'); Chris@14: } Chris@14: } Chris@14: } Chris@14: Chris@14: foreach ($container->getAliases() as $id => $alias) { Chris@14: if ($alias->isPublic() && !$alias->isPrivate()) { Chris@14: $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs); Chris@14: if (null !== $usedEnvs) { Chris@17: throw new EnvParameterException([$resolvedId], null, 'An alias name ("%s") cannot contain dynamic values.'); Chris@14: } Chris@14: } Chris@0: } Chris@0: } Chris@0: }