Drupal investigation

CheckDefinitionValidityPass.php 4.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\ContainerInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  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. * - synthetic services are always of non-prototype scope
  24. * - shared services are always of non-prototype scope
  25. *
  26. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  27. */
  28. class CheckDefinitionValidityPass implements CompilerPassInterface
  29. {
  30. /**
  31. * Processes the ContainerBuilder to validate the Definition.
  32. *
  33. * @param ContainerBuilder $container
  34. *
  35. * @throws RuntimeException When the Definition is invalid
  36. */
  37. public function process(ContainerBuilder $container)
  38. {
  39. foreach ($container->getDefinitions() as $id => $definition) {
  40. // synthetic service is public
  41. if ($definition->isSynthetic() && !$definition->isPublic()) {
  42. throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
  43. }
  44. // synthetic service has non-prototype scope
  45. if ($definition->isSynthetic() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
  46. throw new RuntimeException(sprintf('A synthetic service ("%s") cannot be of scope "prototype".', $id));
  47. }
  48. // shared service has non-prototype scope
  49. if ($definition->isShared() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
  50. throw new RuntimeException(sprintf('A shared service ("%s") cannot be of scope "prototype".', $id));
  51. }
  52. if ($definition->getFactory() && ($definition->getFactoryClass(false) || $definition->getFactoryService(false) || $definition->getFactoryMethod(false))) {
  53. throw new RuntimeException(sprintf('A service ("%s") can use either the old or the new factory syntax, not both.', $id));
  54. }
  55. // non-synthetic, non-abstract service has class
  56. if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) {
  57. if ($definition->getFactory() || $definition->getFactoryClass(false) || $definition->getFactoryService(false)) {
  58. 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));
  59. }
  60. throw new RuntimeException(sprintf(
  61. 'The definition for "%s" has no class. If you intend to inject '
  62. .'this service dynamically at runtime, please mark it as synthetic=true. '
  63. .'If this is an abstract definition solely used by child definitions, '
  64. .'please add abstract=true, otherwise specify a class to get rid of this error.',
  65. $id
  66. ));
  67. }
  68. // tag attribute values must be scalars
  69. foreach ($definition->getTags() as $name => $tags) {
  70. foreach ($tags as $attributes) {
  71. foreach ($attributes as $attribute => $value) {
  72. if (!is_scalar($value) && null !== $value) {
  73. throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }