]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/FormRegistry.php
0267a5657b3989ea724f4a23530986cf0b4dce83
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / FormRegistry.php
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\ExceptionInterface;
15 use Symfony\Component\Form\Exception\UnexpectedTypeException;
16 use Symfony\Component\Form\Exception\InvalidArgumentException;
17
18 /**
19 * The central registry of the Form component.
20 *
21 * @author Bernhard Schussek <bschussek@gmail.com>
22 */
23 class FormRegistry implements FormRegistryInterface
24 {
25 /**
26 * Extensions
27 *
28 * @var FormExtensionInterface[] An array of FormExtensionInterface
29 */
30 private $extensions = array();
31
32 /**
33 * @var array
34 */
35 private $types = array();
36
37 /**
38 * @var FormTypeGuesserInterface|false|null
39 */
40 private $guesser = false;
41
42 /**
43 * @var ResolvedFormTypeFactoryInterface
44 */
45 private $resolvedTypeFactory;
46
47 /**
48 * Constructor.
49 *
50 * @param FormExtensionInterface[] $extensions An array of FormExtensionInterface
51 * @param ResolvedFormTypeFactoryInterface $resolvedTypeFactory The factory for resolved form types.
52 *
53 * @throws UnexpectedTypeException if any extension does not implement FormExtensionInterface
54 */
55 public function __construct(array $extensions, ResolvedFormTypeFactoryInterface $resolvedTypeFactory)
56 {
57 foreach ($extensions as $extension) {
58 if (!$extension instanceof FormExtensionInterface) {
59 throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormExtensionInterface');
60 }
61 }
62
63 $this->extensions = $extensions;
64 $this->resolvedTypeFactory = $resolvedTypeFactory;
65 }
66
67 /**
68 * {@inheritdoc}
69 */
70 public function getType($name)
71 {
72 if (!is_string($name)) {
73 throw new UnexpectedTypeException($name, 'string');
74 }
75
76 if (!isset($this->types[$name])) {
77 /** @var FormTypeInterface $type */
78 $type = null;
79
80 foreach ($this->extensions as $extension) {
81 /* @var FormExtensionInterface $extension */
82 if ($extension->hasType($name)) {
83 $type = $extension->getType($name);
84 break;
85 }
86 }
87
88 if (!$type) {
89 throw new InvalidArgumentException(sprintf('Could not load type "%s"', $name));
90 }
91
92 $this->resolveAndAddType($type);
93 }
94
95 return $this->types[$name];
96 }
97
98 /**
99 * Wraps a type into a ResolvedFormTypeInterface implementation and connects
100 * it with its parent type.
101 *
102 * @param FormTypeInterface $type The type to resolve.
103 *
104 * @return ResolvedFormTypeInterface The resolved type.
105 */
106 private function resolveAndAddType(FormTypeInterface $type)
107 {
108 $parentType = $type->getParent();
109
110 if ($parentType instanceof FormTypeInterface) {
111 $this->resolveAndAddType($parentType);
112 $parentType = $parentType->getName();
113 }
114
115 $typeExtensions = array();
116
117 foreach ($this->extensions as $extension) {
118 /* @var FormExtensionInterface $extension */
119 $typeExtensions = array_merge(
120 $typeExtensions,
121 $extension->getTypeExtensions($type->getName())
122 );
123 }
124
125 $this->types[$type->getName()] = $this->resolvedTypeFactory->createResolvedType(
126 $type,
127 $typeExtensions,
128 $parentType ? $this->getType($parentType) : null
129 );
130 }
131
132 /**
133 * {@inheritdoc}
134 */
135 public function hasType($name)
136 {
137 if (isset($this->types[$name])) {
138 return true;
139 }
140
141 try {
142 $this->getType($name);
143 } catch (ExceptionInterface $e) {
144 return false;
145 }
146
147 return true;
148 }
149
150 /**
151 * {@inheritdoc}
152 */
153 public function getTypeGuesser()
154 {
155 if (false === $this->guesser) {
156 $guessers = array();
157
158 foreach ($this->extensions as $extension) {
159 /* @var FormExtensionInterface $extension */
160 $guesser = $extension->getTypeGuesser();
161
162 if ($guesser) {
163 $guessers[] = $guesser;
164 }
165 }
166
167 $this->guesser = !empty($guessers) ? new FormTypeGuesserChain($guessers) : null;
168 }
169
170 return $this->guesser;
171 }
172
173 /**
174 * {@inheritdoc}
175 */
176 public function getExtensions()
177 {
178 return $this->extensions;
179 }
180 }