DependencyInjectionExtension.php 3.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\Form\Extension\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\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 $container;
  18. private $typeServiceIds;
  19. private $typeExtensionServiceIds;
  20. private $guesserServiceIds;
  21. private $guesser;
  22. private $guesserLoaded = false;
  23. public function __construct(ContainerInterface $container, array $typeServiceIds, array $typeExtensionServiceIds, array $guesserServiceIds)
  24. {
  25. $this->container = $container;
  26. $this->typeServiceIds = $typeServiceIds;
  27. $this->typeExtensionServiceIds = $typeExtensionServiceIds;
  28. $this->guesserServiceIds = $guesserServiceIds;
  29. }
  30. public function getType($name)
  31. {
  32. if (!isset($this->typeServiceIds[$name])) {
  33. throw new InvalidArgumentException(sprintf('The field type "%s" is not registered with the service container.', $name));
  34. }
  35. $type = $this->container->get($this->typeServiceIds[$name]);
  36. // BC: validate result of getName() for legacy names (non-FQCN)
  37. if ($name !== \get_class($type) && $type->getName() !== $name) {
  38. throw new InvalidArgumentException(sprintf('The type name specified for the service "%s" does not match the actual name. Expected "%s", given "%s"', $this->typeServiceIds[$name], $name, $type->getName()));
  39. }
  40. return $type;
  41. }
  42. public function hasType($name)
  43. {
  44. return isset($this->typeServiceIds[$name]);
  45. }
  46. public function getTypeExtensions($name)
  47. {
  48. $extensions = array();
  49. if (isset($this->typeExtensionServiceIds[$name])) {
  50. foreach ($this->typeExtensionServiceIds[$name] as $serviceId) {
  51. $extensions[] = $extension = $this->container->get($serviceId);
  52. // validate result of getExtendedType() to ensure it is consistent with the service definition
  53. if ($extension->getExtendedType() !== $name) {
  54. throw new InvalidArgumentException(sprintf('The extended type specified for the service "%s" does not match the actual extended type. Expected "%s", given "%s".', $serviceId, $name, $extension->getExtendedType()));
  55. }
  56. }
  57. }
  58. return $extensions;
  59. }
  60. public function hasTypeExtensions($name)
  61. {
  62. return isset($this->typeExtensionServiceIds[$name]);
  63. }
  64. public function getTypeGuesser()
  65. {
  66. if (!$this->guesserLoaded) {
  67. $this->guesserLoaded = true;
  68. $guessers = array();
  69. foreach ($this->guesserServiceIds as $serviceId) {
  70. $guessers[] = $this->container->get($serviceId);
  71. }
  72. if (\count($guessers) > 0) {
  73. $this->guesser = new FormTypeGuesserChain($guessers);
  74. }
  75. }
  76. return $this->guesser;
  77. }
  78. }