DependencyInjectionExtension.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Form\Extension\DependencyInjection;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\Form\Exception\InvalidArgumentException;
  13. use Symfony\Component\Form\FormExtensionInterface;
  14. use Symfony\Component\Form\FormTypeGuesserChain;
  15. class DependencyInjectionExtension implements FormExtensionInterface
  16. {
  17. private $guesser;
  18. private $guesserLoaded = false;
  19. private $typeContainer;
  20. private $typeExtensionServices;
  21. private $guesserServices;
  22. /**
  23. * @param iterable[] $typeExtensionServices
  24. */
  25. public function __construct(ContainerInterface $typeContainer, array $typeExtensionServices, iterable $guesserServices)
  26. {
  27. $this->typeContainer = $typeContainer;
  28. $this->typeExtensionServices = $typeExtensionServices;
  29. $this->guesserServices = $guesserServices;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getType(string $name)
  35. {
  36. if (!$this->typeContainer->has($name)) {
  37. throw new InvalidArgumentException(sprintf('The field type "%s" is not registered in the service container.', $name));
  38. }
  39. return $this->typeContainer->get($name);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function hasType(string $name)
  45. {
  46. return $this->typeContainer->has($name);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function getTypeExtensions(string $name)
  52. {
  53. $extensions = [];
  54. if (isset($this->typeExtensionServices[$name])) {
  55. foreach ($this->typeExtensionServices[$name] as $extension) {
  56. $extensions[] = $extension;
  57. $extendedTypes = [];
  58. foreach ($extension::getExtendedTypes() as $extendedType) {
  59. $extendedTypes[] = $extendedType;
  60. }
  61. // validate the result of getExtendedTypes() to ensure it is consistent with the service definition
  62. if (!\in_array($name, $extendedTypes, true)) {
  63. throw new InvalidArgumentException(sprintf('The extended type "%s" specified for the type extension class "%s" does not match any of the actual extended types (["%s"]).', $name, \get_class($extension), implode('", "', $extendedTypes)));
  64. }
  65. }
  66. }
  67. return $extensions;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function hasTypeExtensions(string $name)
  73. {
  74. return isset($this->typeExtensionServices[$name]);
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function getTypeGuesser()
  80. {
  81. if (!$this->guesserLoaded) {
  82. $this->guesserLoaded = true;
  83. $guessers = [];
  84. foreach ($this->guesserServices as $serviceId => $service) {
  85. $guessers[] = $service;
  86. }
  87. if ($guessers) {
  88. $this->guesser = new FormTypeGuesserChain($guessers);
  89. }
  90. }
  91. return $this->guesser;
  92. }
  93. }