CheckDefinitionValidityPass.php 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\ContainerInterface;
  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. * @throws RuntimeException When the Definition is invalid
  34. */
  35. public function process(ContainerBuilder $container)
  36. {
  37. foreach ($container->getDefinitions() as $id => $definition) {
  38. // synthetic service is public
  39. if ($definition->isSynthetic() && !$definition->isPublic()) {
  40. throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
  41. }
  42. // synthetic service has non-prototype scope
  43. if ($definition->isSynthetic() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
  44. throw new RuntimeException(sprintf('A synthetic service ("%s") cannot be of scope "prototype".', $id));
  45. }
  46. // shared service has non-prototype scope
  47. if ($definition->isShared() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
  48. throw new RuntimeException(sprintf('A shared service ("%s") cannot be of scope "prototype".', $id));
  49. }
  50. if ($definition->getFactory() && ($definition->getFactoryClass(false) || $definition->getFactoryService(false) || $definition->getFactoryMethod(false))) {
  51. throw new RuntimeException(sprintf('A service ("%s") can use either the old or the new factory syntax, not both.', $id));
  52. }
  53. // non-synthetic, non-abstract service has class
  54. if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) {
  55. if ($definition->getFactory() || $definition->getFactoryClass(false) || $definition->getFactoryService(false)) {
  56. 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));
  57. }
  58. 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));
  59. }
  60. // tag attribute values must be scalars
  61. foreach ($definition->getTags() as $name => $tags) {
  62. foreach ($tags as $attributes) {
  63. foreach ($attributes as $attribute => $value) {
  64. if (!is_scalar($value) && null !== $value) {
  65. throw new RuntimeException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $id, $name, $attribute));
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }