AbstractExtension.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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
  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($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($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($name)
  69. {
  70. if (null === $this->typeExtensions) {
  71. $this->initTypeExtensions();
  72. }
  73. return isset($this->typeExtensions[$name])
  74. ? $this->typeExtensions[$name]
  75. : array();
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function hasTypeExtensions($name)
  81. {
  82. if (null === $this->typeExtensions) {
  83. $this->initTypeExtensions();
  84. }
  85. return isset($this->typeExtensions[$name]) && \count($this->typeExtensions[$name]) > 0;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getTypeGuesser()
  91. {
  92. if (!$this->typeGuesserLoaded) {
  93. $this->initTypeGuesser();
  94. }
  95. return $this->typeGuesser;
  96. }
  97. /**
  98. * Registers the types.
  99. *
  100. * @return FormTypeInterface[] An array of FormTypeInterface instances
  101. */
  102. protected function loadTypes()
  103. {
  104. return array();
  105. }
  106. /**
  107. * Registers the type extensions.
  108. *
  109. * @return FormTypeExtensionInterface[] An array of FormTypeExtensionInterface instances
  110. */
  111. protected function loadTypeExtensions()
  112. {
  113. return array();
  114. }
  115. /**
  116. * Registers the type guesser.
  117. *
  118. * @return FormTypeGuesserInterface|null A type guesser
  119. */
  120. protected function loadTypeGuesser()
  121. {
  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 = array();
  131. foreach ($this->loadTypes() as $type) {
  132. if (!$type instanceof FormTypeInterface) {
  133. throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface');
  134. }
  135. // Since Symfony 3.0 types are identified by their FQCN
  136. $fqcn = \get_class($type);
  137. $legacyName = $type->getName();
  138. $this->types[$fqcn] = $type;
  139. if ($legacyName) {
  140. $this->types[$legacyName] = $type;
  141. }
  142. }
  143. }
  144. /**
  145. * Initializes the type extensions.
  146. *
  147. * @throws UnexpectedTypeException if any registered type extension is not
  148. * an instance of FormTypeExtensionInterface
  149. */
  150. private function initTypeExtensions()
  151. {
  152. $this->typeExtensions = array();
  153. foreach ($this->loadTypeExtensions() as $extension) {
  154. if (!$extension instanceof FormTypeExtensionInterface) {
  155. throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface');
  156. }
  157. $type = $extension->getExtendedType();
  158. $this->typeExtensions[$type][] = $extension;
  159. }
  160. }
  161. /**
  162. * Initializes the type guesser.
  163. *
  164. * @throws UnexpectedTypeException if the type guesser is not an instance of FormTypeGuesserInterface
  165. */
  166. private function initTypeGuesser()
  167. {
  168. $this->typeGuesserLoaded = true;
  169. $this->typeGuesser = $this->loadTypeGuesser();
  170. if (null !== $this->typeGuesser && !$this->typeGuesser instanceof FormTypeGuesserInterface) {
  171. throw new UnexpectedTypeException($this->typeGuesser, 'Symfony\Component\Form\FormTypeGuesserInterface');
  172. }
  173. }
  174. }