AbstractExtension.php 4.8 KB

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