4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form
;
14 use Symfony\Component\Form\Exception\InvalidArgumentException
;
15 use Symfony\Component\Form\Exception\UnexpectedTypeException
;
18 * @author Bernhard Schussek <bschussek@gmail.com>
20 abstract class AbstractExtension
implements FormExtensionInterface
23 * The types provided by this extension
24 * @var FormTypeInterface[] An array of FormTypeInterface
29 * The type extensions provided by this extension
30 * @var FormTypeExtensionInterface[] An array of FormTypeExtensionInterface
32 private $typeExtensions;
35 * The type guesser provided by this extension
36 * @var FormTypeGuesserInterface
41 * Whether the type guesser has been loaded
44 private $typeGuesserLoaded = false;
49 public function getType($name)
51 if (null === $this->types
) {
55 if (!isset($this->types
[$name])) {
56 throw new InvalidArgumentException(sprintf('The type "%s" can not be loaded by this extension', $name));
59 return $this->types
[$name];
65 public function hasType($name)
67 if (null === $this->types
) {
71 return isset($this->types
[$name]);
77 public function getTypeExtensions($name)
79 if (null === $this->typeExtensions
) {
80 $this->initTypeExtensions();
83 return isset($this->typeExtensions
[$name])
84 ? $this->typeExtensions
[$name]
91 public function hasTypeExtensions($name)
93 if (null === $this->typeExtensions
) {
94 $this->initTypeExtensions();
97 return isset($this->typeExtensions
[$name]) && count($this->typeExtensions
[$name]) > 0;
103 public function getTypeGuesser()
105 if (!$this->typeGuesserLoaded
) {
106 $this->initTypeGuesser();
109 return $this->typeGuesser
;
113 * Registers the types.
115 * @return FormTypeInterface[] An array of FormTypeInterface instances
117 protected function loadTypes()
123 * Registers the type extensions.
125 * @return FormTypeExtensionInterface[] An array of FormTypeExtensionInterface instances
127 protected function loadTypeExtensions()
133 * Registers the type guesser.
135 * @return FormTypeGuesserInterface|null A type guesser
137 protected function loadTypeGuesser()
143 * Initializes the types.
145 * @throws UnexpectedTypeException if any registered type is not an instance of FormTypeInterface
147 private function initTypes()
149 $this->types
= array();
151 foreach ($this->loadTypes() as $type) {
152 if (!$type instanceof FormTypeInterface
) {
153 throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface');
156 $this->types
[$type->getName()] = $type;
161 * Initializes the type extensions.
163 * @throws UnexpectedTypeException if any registered type extension is not
164 * an instance of FormTypeExtensionInterface
166 private function initTypeExtensions()
168 $this->typeExtensions
= array();
170 foreach ($this->loadTypeExtensions() as $extension) {
171 if (!$extension instanceof FormTypeExtensionInterface
) {
172 throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface');
175 $type = $extension->getExtendedType();
177 $this->typeExtensions
[$type][] = $extension;
182 * Initializes the type guesser.
184 * @throws UnexpectedTypeException if the type guesser is not an instance of FormTypeGuesserInterface
186 private function initTypeGuesser()
188 $this->typeGuesserLoaded
= true;
190 $this->typeGuesser
= $this->loadTypeGuesser();
191 if (null !== $this->typeGuesser
&& !$this->typeGuesser
instanceof FormTypeGuesserInterface
) {
192 throw new UnexpectedTypeException($this->typeGuesser
, 'Symfony\Component\Form\FormTypeGuesserInterface');