|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Component\Form; |
|
13 |
|
14 use Symfony\Component\Form\Exception\FormException; |
|
15 use Symfony\Component\Form\Exception\UnexpectedTypeException; |
|
16 |
|
17 /** |
|
18 * @author Bernhard Schussek <bernhard.schussek@symfony.com> |
|
19 */ |
|
20 abstract class AbstractExtension implements FormExtensionInterface |
|
21 { |
|
22 /** |
|
23 * The types provided by this extension |
|
24 * @var array An array of FormTypeInterface |
|
25 */ |
|
26 private $types; |
|
27 |
|
28 /** |
|
29 * The type extensions provided by this extension |
|
30 * @var array An array of FormTypeExtensionInterface |
|
31 */ |
|
32 private $typeExtensions; |
|
33 |
|
34 /** |
|
35 * The type guesser provided by this extension |
|
36 * @var FormTypeGuesserInterface |
|
37 */ |
|
38 private $typeGuesser; |
|
39 |
|
40 /** |
|
41 * Whether the type guesser has been loaded |
|
42 * @var Boolean |
|
43 */ |
|
44 private $typeGuesserLoaded = false; |
|
45 |
|
46 /** |
|
47 * Returns a type by name. |
|
48 * |
|
49 * @param string $name The name of the type |
|
50 * |
|
51 * @return FormTypeInterface The type |
|
52 * |
|
53 * @throws FormException if the given type is not supported by this extension |
|
54 */ |
|
55 public function getType($name) |
|
56 { |
|
57 if (null === $this->types) { |
|
58 $this->initTypes(); |
|
59 } |
|
60 |
|
61 if (!isset($this->types[$name])) { |
|
62 throw new FormException(sprintf('The type "%s" can not be loaded by this extension', $name)); |
|
63 } |
|
64 |
|
65 return $this->types[$name]; |
|
66 } |
|
67 |
|
68 /** |
|
69 * Returns whether the given type is supported. |
|
70 * |
|
71 * @param string $name The name of the type |
|
72 * |
|
73 * @return Boolean Whether the type is supported by this extension |
|
74 */ |
|
75 public function hasType($name) |
|
76 { |
|
77 if (null === $this->types) { |
|
78 $this->initTypes(); |
|
79 } |
|
80 |
|
81 return isset($this->types[$name]); |
|
82 } |
|
83 |
|
84 /** |
|
85 * Returns the extensions for the given type. |
|
86 * |
|
87 * @param string $name The name of the type |
|
88 * |
|
89 * @return array An array of extensions as FormTypeExtensionInterface instances |
|
90 */ |
|
91 public function getTypeExtensions($name) |
|
92 { |
|
93 if (null === $this->typeExtensions) { |
|
94 $this->initTypeExtensions(); |
|
95 } |
|
96 |
|
97 return isset($this->typeExtensions[$name]) |
|
98 ? $this->typeExtensions[$name] |
|
99 : array(); |
|
100 } |
|
101 |
|
102 /** |
|
103 * Returns whether this extension provides type extensions for the given type. |
|
104 * |
|
105 * @param string $name The name of the type |
|
106 * |
|
107 * @return Boolean Whether the given type has extensions |
|
108 */ |
|
109 public function hasTypeExtensions($name) |
|
110 { |
|
111 if (null === $this->typeExtensions) { |
|
112 $this->initTypeExtensions(); |
|
113 } |
|
114 |
|
115 return isset($this->typeExtensions[$name]) && count($this->typeExtensions[$name]) > 0; |
|
116 } |
|
117 |
|
118 /** |
|
119 * Returns the type guesser provided by this extension. |
|
120 * |
|
121 * @return FormTypeGuesserInterface|null The type guesser |
|
122 */ |
|
123 public function getTypeGuesser() |
|
124 { |
|
125 if (!$this->typeGuesserLoaded) { |
|
126 $this->initTypeGuesser(); |
|
127 } |
|
128 |
|
129 return $this->typeGuesser; |
|
130 } |
|
131 |
|
132 /** |
|
133 * Registers the types. |
|
134 * |
|
135 * @return array An array of FormTypeInterface instances |
|
136 */ |
|
137 protected function loadTypes() |
|
138 { |
|
139 return array(); |
|
140 } |
|
141 |
|
142 /** |
|
143 * Registers the type extensions. |
|
144 * |
|
145 * @return array An array of FormTypeExtensionInterface instances |
|
146 */ |
|
147 protected function loadTypeExtensions() |
|
148 { |
|
149 return array(); |
|
150 } |
|
151 |
|
152 /** |
|
153 * Registers the type guesser. |
|
154 * |
|
155 * @return FormTypeGuesserInterface|null A type guesser |
|
156 */ |
|
157 protected function loadTypeGuesser() |
|
158 { |
|
159 return null; |
|
160 } |
|
161 |
|
162 /** |
|
163 * Initializes the types. |
|
164 * |
|
165 * @throws UnexpectedTypeException if any registered type is not an instance of FormTypeInterface |
|
166 */ |
|
167 private function initTypes() |
|
168 { |
|
169 $this->types = array(); |
|
170 |
|
171 foreach ($this->loadTypes() as $type) { |
|
172 if (!$type instanceof FormTypeInterface) { |
|
173 throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface'); |
|
174 } |
|
175 |
|
176 $this->types[$type->getName()] = $type; |
|
177 } |
|
178 } |
|
179 |
|
180 /** |
|
181 * Initializes the type extensions. |
|
182 * |
|
183 * @throws UnexpectedTypeException if any registered type extension is not |
|
184 * an instance of FormTypeExtensionInterface |
|
185 */ |
|
186 private function initTypeExtensions() |
|
187 { |
|
188 $this->typeExtensions = array(); |
|
189 |
|
190 foreach ($this->loadTypeExtensions() as $extension) { |
|
191 if (!$extension instanceof FormTypeExtensionInterface) { |
|
192 throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface'); |
|
193 } |
|
194 |
|
195 $type = $extension->getExtendedType(); |
|
196 |
|
197 $this->typeExtensions[$type][] = $extension; |
|
198 } |
|
199 } |
|
200 |
|
201 /** |
|
202 * Initializes the type guesser. |
|
203 * |
|
204 * @throws UnexpectedTypeException if the type guesser is not an instance of FormTypeGuesserInterface |
|
205 */ |
|
206 private function initTypeGuesser() |
|
207 { |
|
208 $this->typeGuesserLoaded = true; |
|
209 |
|
210 $this->typeGuesser = $this->loadTypeGuesser(); |
|
211 if (null !== $this->typeGuesser && !$this->typeGuesser instanceof FormTypeGuesserInterface) { |
|
212 throw new UnexpectedTypeException($this->typeGuesser, 'Symfony\Component\Form\FormTypeGuesserInterface'); |
|
213 } |
|
214 } |
|
215 } |