FormFactoryBuilder.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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;
  11. use Symfony\Component\Form\Extension\Core\CoreExtension;
  12. /**
  13. * The default implementation of FormFactoryBuilderInterface.
  14. *
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. class FormFactoryBuilder implements FormFactoryBuilderInterface
  18. {
  19. private $forceCoreExtension;
  20. /**
  21. * @var ResolvedFormTypeFactoryInterface
  22. */
  23. private $resolvedTypeFactory;
  24. /**
  25. * @var FormExtensionInterface[]
  26. */
  27. private $extensions = [];
  28. /**
  29. * @var FormTypeInterface[]
  30. */
  31. private $types = [];
  32. /**
  33. * @var FormTypeExtensionInterface[]
  34. */
  35. private $typeExtensions = [];
  36. /**
  37. * @var FormTypeGuesserInterface[]
  38. */
  39. private $typeGuessers = [];
  40. public function __construct(bool $forceCoreExtension = false)
  41. {
  42. $this->forceCoreExtension = $forceCoreExtension;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function setResolvedTypeFactory(ResolvedFormTypeFactoryInterface $resolvedTypeFactory)
  48. {
  49. $this->resolvedTypeFactory = $resolvedTypeFactory;
  50. return $this;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function addExtension(FormExtensionInterface $extension)
  56. {
  57. $this->extensions[] = $extension;
  58. return $this;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function addExtensions(array $extensions)
  64. {
  65. $this->extensions = array_merge($this->extensions, $extensions);
  66. return $this;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function addType(FormTypeInterface $type)
  72. {
  73. $this->types[] = $type;
  74. return $this;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function addTypes(array $types)
  80. {
  81. foreach ($types as $type) {
  82. $this->types[] = $type;
  83. }
  84. return $this;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function addTypeExtension(FormTypeExtensionInterface $typeExtension)
  90. {
  91. foreach ($typeExtension::getExtendedTypes() as $extendedType) {
  92. $this->typeExtensions[$extendedType][] = $typeExtension;
  93. }
  94. return $this;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function addTypeExtensions(array $typeExtensions)
  100. {
  101. foreach ($typeExtensions as $typeExtension) {
  102. $this->addTypeExtension($typeExtension);
  103. }
  104. return $this;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function addTypeGuesser(FormTypeGuesserInterface $typeGuesser)
  110. {
  111. $this->typeGuessers[] = $typeGuesser;
  112. return $this;
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function addTypeGuessers(array $typeGuessers)
  118. {
  119. $this->typeGuessers = array_merge($this->typeGuessers, $typeGuessers);
  120. return $this;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getFormFactory()
  126. {
  127. $extensions = $this->extensions;
  128. if ($this->forceCoreExtension) {
  129. $hasCoreExtension = false;
  130. foreach ($extensions as $extension) {
  131. if ($extension instanceof CoreExtension) {
  132. $hasCoreExtension = true;
  133. break;
  134. }
  135. }
  136. if (!$hasCoreExtension) {
  137. array_unshift($extensions, new CoreExtension());
  138. }
  139. }
  140. if (\count($this->types) > 0 || \count($this->typeExtensions) > 0 || \count($this->typeGuessers) > 0) {
  141. if (\count($this->typeGuessers) > 1) {
  142. $typeGuesser = new FormTypeGuesserChain($this->typeGuessers);
  143. } else {
  144. $typeGuesser = $this->typeGuessers[0] ?? null;
  145. }
  146. $extensions[] = new PreloadedExtension($this->types, $this->typeExtensions, $typeGuesser);
  147. }
  148. $registry = new FormRegistry($extensions, $this->resolvedTypeFactory ?: new ResolvedFormTypeFactory());
  149. return new FormFactory($registry);
  150. }
  151. }