CheckDefinitionValidityPass.php 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /**
  15. * This pass validates each definition individually only taking the information
  16. * into account which is contained in the definition itself.
  17. *
  18. * Later passes can rely on the following, and specifically do not need to
  19. * perform these checks themselves:
  20. *
  21. * - non synthetic, non abstract services always have a class set
  22. * - synthetic services are always public
  23. *
  24. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  25. */
  26. class CheckDefinitionValidityPass implements CompilerPassInterface
  27. {
  28. /**
  29. * Processes the ContainerBuilder to validate the Definition.
  30. *
  31. * @throws RuntimeException When the Definition is invalid
  32. */
  33. public function process(ContainerBuilder $container)
  34. {
  35. foreach ($container->getDefinitions() as $id => $definition) {
  36. // synthetic service is public
  37. if ($definition->isSynthetic() && !$definition->isPublic()) {
  38. throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
  39. }
  40. // non-synthetic, non-abstract service has class
  41. if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) {
  42. if ($definition->getFactory()) {
  43. 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));
  44. }
  45. if (class_exists($id) || interface_exists($id, false)) {
  46. throw new RuntimeException(sprintf(
  47. 'The definition for "%s" has no class attribute, and appears to reference a '
  48. .'class or interface in the global namespace. Leaving out the "class" attribute '
  49. .'is only allowed for namespaced classes. Please specify the class attribute '
  50. .'explicitly to get rid of this error.',
  51. $id
  52. ));
  53. }
  54. throw new RuntimeException(sprintf(
  55. 'The definition for "%s" has no class. If you intend to inject '
  56. .'this service dynamically at runtime, please mark it as synthetic=true. '
  57. .'If this is an abstract definition solely used by child definitions, '
  58. .'please add abstract=true, otherwise specify a class to get rid of this error.',
  59. $id
  60. ));
  61. }
  62. // tag attribute values must be scalars
  63. foreach ($definition->getTags() as $name => $tags) {
  64. foreach ($tags as $attributes) {
  65. foreach ($attributes as $attribute => $value) {
  66. if (!is_scalar($value) && null !== $value) {
  67. throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));
  68. }
  69. }
  70. }
  71. }
  72. if ($definition->isPublic()) {
  73. $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
  74. if (null !== $usedEnvs) {
  75. throw new EnvParameterException(array($resolvedId), null, 'A service name ("%s") cannot contain dynamic values.');
  76. }
  77. }
  78. }
  79. foreach ($container->getAliases() as $id => $alias) {
  80. if ($alias->isPublic()) {
  81. $resolvedId = $container->resolveEnvPlaceholders($id, null, $usedEnvs);
  82. if (null !== $usedEnvs) {
  83. throw new EnvParameterException(array($resolvedId), null, 'An alias name ("%s") cannot contain dynamic values.');
  84. }
  85. }
  86. }
  87. }
  88. }