vendor/symfony/form/Extension/DependencyInjection/DependencyInjectionExtension.php line 51

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <[email protected]>
  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\FormTypeExtensionInterface;
  15. use Symfony\Component\Form\FormTypeGuesserChain;
  16. use Symfony\Component\Form\FormTypeGuesserInterface;
  17. use Symfony\Component\Form\FormTypeInterface;
  18. class DependencyInjectionExtension implements FormExtensionInterface
  19. {
  20. private ?FormTypeGuesserChain $guesser = null;
  21. private bool $guesserLoaded = false;
  22. private ContainerInterface $typeContainer;
  23. private array $typeExtensionServices;
  24. private iterable $guesserServices;
  25. /**
  26. * @param array<string, iterable<FormTypeExtensionInterface>> $typeExtensionServices
  27. */
  28. public function __construct(ContainerInterface $typeContainer, array $typeExtensionServices, iterable $guesserServices)
  29. {
  30. $this->typeContainer = $typeContainer;
  31. $this->typeExtensionServices = $typeExtensionServices;
  32. $this->guesserServices = $guesserServices;
  33. }
  34. public function getType(string $name): FormTypeInterface
  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. public function hasType(string $name): bool
  42. {
  43. return $this->typeContainer->has($name);
  44. }
  45. public function getTypeExtensions(string $name): array
  46. {
  47. $extensions = [];
  48. if (isset($this->typeExtensionServices[$name])) {
  49. foreach ($this->typeExtensionServices[$name] as $extension) {
  50. $extensions[] = $extension;
  51. $extendedTypes = [];
  52. foreach ($extension::getExtendedTypes() as $extendedType) {
  53. $extendedTypes[] = $extendedType;
  54. }
  55. // validate the result of getExtendedTypes() to ensure it is consistent with the service definition
  56. if (!\in_array($name, $extendedTypes, true)) {
  57. 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, $extension::class, implode('", "', $extendedTypes)));
  58. }
  59. }
  60. }
  61. return $extensions;
  62. }
  63. public function hasTypeExtensions(string $name): bool
  64. {
  65. return isset($this->typeExtensionServices[$name]);
  66. }
  67. public function getTypeGuesser(): ?FormTypeGuesserInterface
  68. {
  69. if (!$this->guesserLoaded) {
  70. $this->guesserLoaded = true;
  71. $guessers = [];
  72. foreach ($this->guesserServices as $serviceId => $service) {
  73. $guessers[] = $service;
  74. }
  75. if ($guessers) {
  76. $this->guesser = new FormTypeGuesserChain($guessers);
  77. }
  78. }
  79. return $this->guesser;
  80. }
  81. }