CheckDefinitionValidityPass.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Exception\EnvParameterException;
  13. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  14. use Symfony\Component\DependencyInjection\Loader\FileLoader;
  15. /**
  16. * This pass validates each definition individually only taking the information
  17. * into account which is contained in the definition itself.
  18. *
  19. * Later passes can rely on the following, and specifically do not need to
  20. * perform these checks themselves:
  21. *
  22. * - non synthetic, non abstract services always have a class set
  23. * - synthetic services are always public
  24. *
  25. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  26. */
  27. class CheckDefinitionValidityPass implements CompilerPassInterface
  28. {
  29. /**
  30. * Processes the ContainerBuilder to validate the Definition.
  31. *
  32. * @throws RuntimeException When the Definition is invalid
  33. */
  34. public function process(ContainerBuilder $container)
  35. {
  36. foreach ($container->getDefinitions() as $id => $definition) {
  37. // synthetic service is public
  38. if ($definition->isSynthetic() && !$definition->isPublic()) {
  39. throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
  40. }
  41. // non-synthetic, non-abstract service has class
  42. if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass() && (!$definition->getFactory() || !preg_match(FileLoader::ANONYMOUS_ID_REGEXP, $id))) {
  43. if ($definition->getFactory()) {
  44. 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));
  45. }
  46. if (class_exists($id) || interface_exists($id, false)) {
  47. if (0 === strpos($id, '\\') && 1 < substr_count($id, '\\')) {
  48. throw new RuntimeException(sprintf('The definition for "%s" has no class attribute, and appears to reference a class or interface. Please specify the class attribute explicitly or remove the leading backslash by renaming the service to "%s" to get rid of this error.', $id, substr($id, 1)));
  49. }
  50. 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));
  51. }
  52. 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));
  53. }
  54. // tag attribute values must be scalars
  55. foreach ($definition->getTags() as $name => $tags) {
  56. foreach ($tags as $attributes) {
  57. foreach ($attributes as $attribute => $value) {
  58. if (!is_scalar($value) && null !== $value) {
  59. throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));
  60. }
  61. }
  62. }
  63. }
  64. if ($definition->isPublic() && !$definition->isPrivate()) {
  65. $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
  66. if (null !== $usedEnvs) {
  67. throw new EnvParameterException([$resolvedId], null, 'A service name ("%s") cannot contain dynamic values.');
  68. }
  69. }
  70. }
  71. foreach ($container->getAliases() as $id => $alias) {
  72. if ($alias->isPublic() && !$alias->isPrivate()) {
  73. $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
  74. if (null !== $usedEnvs) {
  75. throw new EnvParameterException([$resolvedId], null, 'An alias name ("%s") cannot contain dynamic values.');
  76. }
  77. }
  78. }
  79. }
  80. }