FormFactoryBuilder.php 3.6 KB

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