Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 159 lines (123 sloc) 3.663 kB
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
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 * @var array
24 */
25 private $types;
26
27 /**
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
28 * @var array
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
29 */
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
30 private $typeExtensions;
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
31
32 /**
33 * @var FormTypeGuesserInterface
34 */
35 private $typeGuesser;
36
37 /**
38 * @var Boolean
39 */
40 private $typeGuesserLoaded = false;
41
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
42 protected function loadTypes()
43 {
44 return array();
45 }
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
46
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
47 protected function loadTypeExtensions()
48 {
49 return array();
50 }
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
51
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
52 protected function loadTypeGuesser()
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
53 {
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
54 return null;
55 }
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
56
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
57 private function initTypes()
58 {
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
59 $types = $this->loadTypes();
60 $typesByName = array();
61
62 foreach ($types as $type) {
63 if (!$type instanceof FormTypeInterface) {
64 throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface');
65 }
66
67 $typesByName[$type->getName()] = $type;
68 }
69
70 $this->types = $typesByName;
71 }
72
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
73 private function initTypeExtensions()
74 {
75 $extensions = $this->loadTypeExtensions();
76 $extensionsByType = array();
77
78 foreach ($extensions as $extension) {
79 if (!$extension instanceof FormTypeExtensionInterface) {
80 throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface');
81 }
82
83 $type = $extension->getExtendedType();
84
85 if (!isset($extensionsByType[$type])) {
86 $extensionsByType[$type] = array();
87 }
88
89 $extensionsByType[$type][] = $extension;
90 }
91
92 $this->typeExtensions = $extensionsByType;
93 }
94
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
95 private function initTypeGuesser()
96 {
97 $this->typeGuesserLoaded = true;
98
99 $guesser = $this->loadTypeGuesser();
100
101 if (!$guesser instanceof FormTypeGuesserInterface) {
102 throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeGuesserInterface');
103 }
104
105 $this->guesser = $guesser;
106 }
107
108 public function getType($name)
109 {
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
110 if (null === $this->types) {
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
111 $this->initTypes();
112 }
113
114 if (!isset($this->types[$name])) {
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
115 throw new FormException(sprintf('The type "%s" can not be loaded by this extension', $name));
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
116 }
117
118 return $this->types[$name];
119 }
120
121 public function hasType($name)
122 {
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
123 if (null === $this->types) {
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
124 $this->initTypes();
125 }
126
127 return isset($this->types[$name]);
128 }
129
3e5fc07 @webmozart [Form] Added FormTypeExtensionInterface
webmozart authored
130 function getTypeExtensions($name)
131 {
132 if (null === $this->typeExtensions) {
133 $this->initTypeExtensions();
134 }
135
136 return isset($this->typeExtensions[$name])
137 ? $this->typeExtensions[$name]
138 : array();
139 }
140
141 function hasTypeExtensions($name)
142 {
143 if (null === $this->typeExtensions) {
144 $this->initTypeExtensions();
145 }
146
147 return isset($this->typeExtensions[$name]) && count($this->typeExtensions[$name]) > 0;
148 }
149
fc5c541 @webmozart [Form] Reorganized code into "form extensions"
webmozart authored
150 public function getTypeGuesser()
151 {
152 if (!$this->typeGuesserLoaded) {
153 $this->initTypeGuesser();
154 }
155
156 return $this->typeGuesser;
157 }
158 }
Something went wrong with that request. Please try again.