1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Form\FormObject; |
15
|
|
|
|
16
|
|
|
use Romm\ConfigurationObject\ConfigurationObjectInterface; |
17
|
|
|
use Romm\Formz\Configuration\Configuration; |
18
|
|
|
use Romm\Formz\Configuration\ConfigurationFactory; |
19
|
|
|
use Romm\Formz\Core\Core; |
20
|
|
|
use Romm\Formz\Exceptions\ClassNotFoundException; |
21
|
|
|
use Romm\Formz\Exceptions\InvalidArgumentTypeException; |
22
|
|
|
use Romm\Formz\Form\FormInterface; |
23
|
|
|
use Romm\Formz\Form\FormObject\Builder\DefaultFormObjectBuilder; |
24
|
|
|
use Romm\Formz\Form\FormObject\Builder\FormObjectBuilderInterface; |
25
|
|
|
use Romm\Formz\Service\CacheService; |
26
|
|
|
use Romm\Formz\Service\StringService; |
27
|
|
|
use Romm\Formz\Service\Traits\ExtendedSelfInstantiateTrait; |
28
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Factory class that will manage form object instances. |
32
|
|
|
* |
33
|
|
|
* You can fetch a new instance by calling one of the following methods: |
34
|
|
|
* `getInstanceFromFormInstance()` or `getInstanceFromClassName()` |
35
|
|
|
* |
36
|
|
|
* @see \Romm\Formz\Form\FormObject\FormObject |
37
|
|
|
*/ |
38
|
|
|
class FormObjectFactory implements SingletonInterface |
39
|
|
|
{ |
40
|
|
|
use ExtendedSelfInstantiateTrait; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var FormObject[] |
44
|
|
|
*/ |
45
|
|
|
protected $instances = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var FormObjectStatic[] |
49
|
|
|
*/ |
50
|
|
|
protected $static = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var FormObjectProxy[] |
54
|
|
|
*/ |
55
|
|
|
protected $proxy = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param FormInterface $form |
59
|
|
|
* @param string $name |
60
|
|
|
* @return FormObject |
61
|
|
|
*/ |
62
|
|
|
public function getInstanceWithFormInstance(FormInterface $form, $name = 'defaultName') |
63
|
|
|
{ |
64
|
|
|
$hash = $name . '-' . spl_object_hash($form); |
65
|
|
|
|
66
|
|
|
if (false === isset($this->instances[$hash])) { |
67
|
|
|
$this->instances[$hash] = $this->getInstanceWithClassName(get_class($form), $name); |
68
|
|
|
$this->instances[$hash]->setForm($form); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->instances[$hash]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Will create an instance of `FormObject` based on a class that implements |
76
|
|
|
* the interface `FormInterface`. |
77
|
|
|
* |
78
|
|
|
* @param string $className |
79
|
|
|
* @param string $name |
80
|
|
|
* @return FormObject |
81
|
|
|
*/ |
82
|
|
|
public function getInstanceWithClassName($className, $name) |
83
|
|
|
{ |
84
|
|
|
/** @var FormObject $formObject */ |
85
|
|
|
$formObject = Core::instantiate(FormObject::class, $name, $this->getStaticInstance($className)); |
86
|
|
|
|
87
|
|
|
return $formObject; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the proxy object for the given form object and form instance. |
92
|
|
|
* |
93
|
|
|
* Please use with caution, as this is a very low level function! |
94
|
|
|
* |
95
|
|
|
* @param FormInterface $form |
96
|
|
|
* @return FormObjectProxy |
97
|
|
|
*/ |
98
|
|
|
public function getProxy(FormInterface $form) |
99
|
|
|
{ |
100
|
|
|
$hash = spl_object_hash($form); |
101
|
|
|
|
102
|
|
|
if (false === isset($this->proxy[$hash])) { |
103
|
|
|
$this->proxy[$hash] = $this->getNewProxyInstance($form); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->proxy[$hash]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $className |
111
|
|
|
* @return FormObjectStatic |
112
|
|
|
* @throws ClassNotFoundException |
113
|
|
|
* @throws InvalidArgumentTypeException |
114
|
|
|
*/ |
115
|
|
|
protected function getStaticInstance($className) |
116
|
|
|
{ |
117
|
|
|
if (false === class_exists($className)) { |
118
|
|
|
throw ClassNotFoundException::wrongFormClassName($className); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (false === in_array(FormInterface::class, class_implements($className))) { |
122
|
|
|
throw InvalidArgumentTypeException::wrongFormType($className); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$cacheIdentifier = $this->getCacheIdentifier($className); |
126
|
|
|
|
127
|
|
|
if (false === isset($this->static[$cacheIdentifier])) { |
128
|
|
|
$cacheInstance = CacheService::get()->getCacheInstance(); |
129
|
|
|
|
130
|
|
|
if ($cacheInstance->has($cacheIdentifier)) { |
131
|
|
|
$static = $cacheInstance->get($cacheIdentifier); |
132
|
|
|
} else { |
133
|
|
|
$static = $this->buildStaticInstance($className); |
134
|
|
|
$static->getObjectHash(); |
135
|
|
|
|
136
|
|
|
$cacheInstance->set($cacheIdentifier, $static); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->addToGlobalConfiguration($static); |
140
|
|
|
|
141
|
|
|
$this->static[$cacheIdentifier] = $static; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->static[$cacheIdentifier]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Adds the given form configuration to the global FormZ configuration |
149
|
|
|
* object. |
150
|
|
|
* |
151
|
|
|
* @param FormObjectStatic $static |
152
|
|
|
*/ |
153
|
|
|
public function addToGlobalConfiguration(FormObjectStatic $static) |
154
|
|
|
{ |
155
|
|
|
if (false === $this->getGlobalConfiguration()->hasForm($static->getClassName())) { |
|
|
|
|
156
|
|
|
$this->getGlobalConfiguration()->addForm($static); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $className |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
protected function getCacheIdentifier($className) |
165
|
|
|
{ |
166
|
|
|
$sanitizedClassName = StringService::get()->sanitizeString(str_replace('\\', '-', $className)); |
167
|
|
|
|
168
|
|
|
return 'form-object-' . $sanitizedClassName; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Wrapper for unit tests. |
173
|
|
|
* |
174
|
|
|
* @param string $className |
175
|
|
|
* @return FormObjectStatic |
176
|
|
|
*/ |
177
|
|
|
protected function buildStaticInstance($className) |
178
|
|
|
{ |
179
|
|
|
/** @var FormObjectBuilderInterface $builder */ |
180
|
|
|
$builder = Core::instantiate(DefaultFormObjectBuilder::class); |
181
|
|
|
|
182
|
|
|
return $builder->getStaticInstance($className); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Wrapper for unit tests. |
187
|
|
|
* |
188
|
|
|
* @param FormInterface $form |
189
|
|
|
* @return FormObjectProxy |
190
|
|
|
*/ |
191
|
|
|
protected function getNewProxyInstance(FormInterface $form) |
192
|
|
|
{ |
193
|
|
|
$formObject = $this->getInstanceWithFormInstance($form); |
194
|
|
|
|
195
|
|
|
/** @var FormObjectProxy $formObjectProxy */ |
196
|
|
|
$formObjectProxy = Core::instantiate(FormObjectProxy::class, $formObject, $form); |
197
|
|
|
|
198
|
|
|
return $formObjectProxy; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return Configuration|ConfigurationObjectInterface |
203
|
|
|
*/ |
204
|
|
|
protected function getGlobalConfiguration() |
205
|
|
|
{ |
206
|
|
|
return ConfigurationFactory::get()->getFormzConfiguration()->getObject(true); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: