AbstractExtension.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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\Exception\InvalidArgumentException;
  12. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  13. /**
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. abstract class AbstractExtension implements FormExtensionInterface
  17. {
  18. /**
  19. * The types provided by this extension.
  20. *
  21. * @var FormTypeInterface[] An array of FormTypeInterface
  22. */
  23. private $types;
  24. /**
  25. * The type extensions provided by this extension.
  26. *
  27. * @var FormTypeExtensionInterface[] An array of FormTypeExtensionInterface
  28. */
  29. private $typeExtensions;
  30. /**
  31. * The type guesser provided by this extension.
  32. *
  33. * @var FormTypeGuesserInterface|null
  34. */
  35. private $typeGuesser;
  36. /**
  37. * Whether the type guesser has been loaded.
  38. *
  39. * @var bool
  40. */
  41. private $typeGuesserLoaded = false;
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getType(string $name)
  46. {
  47. if (null === $this->types) {
  48. $this->initTypes();
  49. }
  50. if (!isset($this->types[$name])) {
  51. throw new InvalidArgumentException(sprintf('The type "%s" can not be loaded by this extension.', $name));
  52. }
  53. return $this->types[$name];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function hasType(string $name)
  59. {
  60. if (null === $this->types) {
  61. $this->initTypes();
  62. }
  63. return isset($this->types[$name]);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function getTypeExtensions(string $name)
  69. {
  70. if (null === $this->typeExtensions) {
  71. $this->initTypeExtensions();
  72. }
  73. return $this->typeExtensions[$name]
  74. ?? [];
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function hasTypeExtensions(string $name)
  80. {
  81. if (null === $this->typeExtensions) {
  82. $this->initTypeExtensions();
  83. }
  84. return isset($this->typeExtensions[$name]) && \count($this->typeExtensions[$name]) > 0;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function getTypeGuesser()
  90. {
  91. if (!$this->typeGuesserLoaded) {
  92. $this->initTypeGuesser();
  93. }
  94. return $this->typeGuesser;
  95. }
  96. /**
  97. * Registers the types.
  98. *
  99. * @return FormTypeInterface[] An array of FormTypeInterface instances
  100. */
  101. protected function loadTypes()
  102. {
  103. return [];
  104. }
  105. /**
  106. * Registers the type extensions.
  107. *
  108. * @return FormTypeExtensionInterface[] An array of FormTypeExtensionInterface instances
  109. */
  110. protected function loadTypeExtensions()
  111. {
  112. return [];
  113. }
  114. /**
  115. * Registers the type guesser.
  116. *
  117. * @return FormTypeGuesserInterface|null
  118. */
  119. protected function loadTypeGuesser()
  120. {
  121. return null;
  122. }
  123. /**
  124. * Initializes the types.
  125. *
  126. * @throws UnexpectedTypeException if any registered type is not an instance of FormTypeInterface
  127. */
  128. private function initTypes()
  129. {
  130. $this->types = [];
  131. foreach ($this->loadTypes() as $type) {
  132. if (!$type instanceof FormTypeInterface) {
  133. throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface');
  134. }
  135. $this->types[\get_class($type)] = $type;
  136. }
  137. }
  138. /**
  139. * Initializes the type extensions.
  140. *
  141. * @throws UnexpectedTypeException if any registered type extension is not
  142. * an instance of FormTypeExtensionInterface
  143. */
  144. private function initTypeExtensions()
  145. {
  146. $this->typeExtensions = [];
  147. foreach ($this->loadTypeExtensions() as $extension) {
  148. if (!$extension instanceof FormTypeExtensionInterface) {
  149. throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface');
  150. }
  151. foreach ($extension::getExtendedTypes() as $extendedType) {
  152. $this->typeExtensions[$extendedType][] = $extension;
  153. }
  154. }
  155. }
  156. /**
  157. * Initializes the type guesser.
  158. *
  159. * @throws UnexpectedTypeException if the type guesser is not an instance of FormTypeGuesserInterface
  160. */
  161. private function initTypeGuesser()
  162. {
  163. $this->typeGuesserLoaded = true;
  164. $this->typeGuesser = $this->loadTypeGuesser();
  165. if (null !== $this->typeGuesser && !$this->typeGuesser instanceof FormTypeGuesserInterface) {
  166. throw new UnexpectedTypeException($this->typeGuesser, 'Symfony\Component\Form\FormTypeGuesserInterface');
  167. }
  168. }
  169. }