This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Compiler/CheckDefinitionValidityPass.php

83 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2011-05-31 09:57:06 +01:00
/*
2012-03-31 22:00:32 +01:00
* This file is part of the Symfony package.
2011-05-31 09:57:06 +01:00
*
* (c) Fabien Potencier <fabien@symfony.com>
*
2012-03-31 22:00:32 +01:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2011-05-31 09:57:06 +01:00
*/
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
/**
* This pass validates each definition individually only taking the information
* into account which is contained in the definition itself.
*
* Later passes can rely on the following, and specifically do not need to
2011-04-30 20:40:15 +01:00
* perform these checks themselves:
*
2011-01-26 23:14:31 +00:00
* - non synthetic, non abstract services always have a class set
* - synthetic services are always public
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class CheckDefinitionValidityPass implements CompilerPassInterface
{
2011-02-13 18:06:41 +00:00
/**
* Processes the ContainerBuilder to validate the Definition.
*
2011-04-15 20:12:02 +01:00
* @param ContainerBuilder $container
2011-12-13 07:50:54 +00:00
*
* @throws RuntimeException When the Definition is invalid
2011-02-13 18:06:41 +00:00
*/
public function process(ContainerBuilder $container)
{
foreach ($container->getDefinitions() as $id => $definition) {
// synthetic service is public
if ($definition->isSynthetic() && (!$definition->isPublic() || $definition->isPrivate())) {
2014-09-24 07:35:53 +01:00
throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
}
2011-01-26 23:14:31 +00:00
// non-synthetic, non-abstract service has class
if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) {
if ($definition->getFactory()) {
2014-09-24 07:35:53 +01:00
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));
}
if (class_exists($id) || interface_exists($id, false)) {
throw new RuntimeException(sprintf(
'The definition for "%s" has no class attribute, and appears to reference a '
.'class or interface in the global namespace. Leaving out the "class" attribute '
.'is only allowed for namespaced classes. Please specify the class attribute '
.'explicitly to get rid of this error.',
$id
));
}
throw new RuntimeException(sprintf(
'The definition for "%s" has no class. If you intend to inject '
2011-01-26 23:14:31 +00:00
.'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
));
}
// tag attribute values must be scalars
foreach ($definition->getTags() as $name => $tags) {
foreach ($tags as $attributes) {
foreach ($attributes as $attribute => $value) {
2013-09-15 19:46:31 +01:00
if (!is_scalar($value) && null !== $value) {
throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));
}
}
}
}
}
}
}