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; |
15
|
|
|
|
16
|
|
|
use Romm\ConfigurationObject\ConfigurationObjectFactory; |
17
|
|
|
use Romm\ConfigurationObject\ConfigurationObjectInstance; |
18
|
|
|
use Romm\Formz\Configuration\ConfigurationFactory; |
19
|
|
|
use Romm\Formz\Configuration\Form\Form; |
20
|
|
|
use Romm\Formz\Core\Core; |
21
|
|
|
use Romm\Formz\Service\CacheService; |
22
|
|
|
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; |
23
|
|
|
use TYPO3\CMS\Extbase\Error\Result; |
24
|
|
|
|
25
|
|
|
class FormObjectConfiguration |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var FormObject |
29
|
|
|
*/ |
30
|
|
|
protected $formObject; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Contains the form configuration. |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $configurationArray = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Contains the form configuration object, which was created from the |
41
|
|
|
* configuration array. |
42
|
|
|
* |
43
|
|
|
* @var ConfigurationObjectInstance |
44
|
|
|
*/ |
45
|
|
|
protected $configurationObject; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Result |
49
|
|
|
*/ |
50
|
|
|
protected $configurationValidationResult; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $lastConfigurationHash; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var ConfigurationFactory |
59
|
|
|
*/ |
60
|
|
|
protected $configurationFactory; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param FormObject $formObject |
64
|
|
|
* @param array $configurationArray |
65
|
|
|
*/ |
66
|
|
|
public function __construct(FormObject $formObject, array $configurationArray) |
67
|
|
|
{ |
68
|
|
|
$this->formObject = $formObject; |
69
|
|
|
$this->configurationArray = $configurationArray; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns an instance of configuration object. Checks if it was previously |
74
|
|
|
* stored in cache, otherwise it is created from scratch. |
75
|
|
|
* |
76
|
|
|
* @return ConfigurationObjectInstance |
77
|
|
|
*/ |
78
|
|
|
public function getConfigurationObject() |
79
|
|
|
{ |
80
|
|
|
if (null === $this->configurationObject |
81
|
|
|
|| $this->lastConfigurationHash !== $this->formObject->getHash() |
82
|
|
|
) { |
83
|
|
|
$this->lastConfigurationHash = $this->formObject->getHash(); |
84
|
|
|
$this->configurationObject = $this->getConfigurationObjectFromCache(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->configurationObject; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* This function will merge and return the validation results of both the |
92
|
|
|
* global FormZ configuration object, and this form configuration object. |
93
|
|
|
* |
94
|
|
|
* @return Result |
95
|
|
|
*/ |
96
|
|
|
public function getConfigurationValidationResult() |
97
|
|
|
{ |
98
|
|
|
if (null === $this->configurationValidationResult |
99
|
|
|
|| $this->lastConfigurationHash !== $this->formObject->getHash() |
100
|
|
|
) { |
101
|
|
|
$configurationObject = $this->getConfigurationObject(); |
102
|
|
|
$this->configurationValidationResult = $this->refreshConfigurationValidationResult($configurationObject); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->configurationValidationResult; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Resets the validation result and merges it with the global FormZ |
110
|
|
|
* configuration. |
111
|
|
|
* |
112
|
|
|
* @param ConfigurationObjectInstance $configurationObject |
113
|
|
|
* @return Result |
114
|
|
|
*/ |
115
|
|
|
protected function refreshConfigurationValidationResult(ConfigurationObjectInstance $configurationObject) |
116
|
|
|
{ |
117
|
|
|
$result = new Result; |
118
|
|
|
$formzConfigurationValidationResult = $this->configurationFactory |
119
|
|
|
->getFormzConfiguration() |
120
|
|
|
->getValidationResult(); |
121
|
|
|
|
122
|
|
|
$result->merge($formzConfigurationValidationResult); |
123
|
|
|
|
124
|
|
|
$result->forProperty('forms.' . $this->formObject->getClassName()) |
125
|
|
|
->merge($configurationObject->getValidationResult()); |
126
|
|
|
|
127
|
|
|
return $result; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return ConfigurationObjectInstance |
132
|
|
|
*/ |
133
|
|
|
protected function getConfigurationObjectFromCache() |
134
|
|
|
{ |
135
|
|
|
$cacheInstance = $this->getCacheInstance(); |
136
|
|
|
$cacheIdentifier = 'configuration-' . $this->formObject->getHash(); |
137
|
|
|
|
138
|
|
|
if ($cacheInstance->has($cacheIdentifier)) { |
139
|
|
|
$configurationObject = $cacheInstance->get($cacheIdentifier); |
140
|
|
|
} else { |
141
|
|
|
$configurationObject = $this->buildConfigurationObject(); |
142
|
|
|
|
143
|
|
|
if (false === $configurationObject->getValidationResult()->hasErrors()) { |
144
|
|
|
$cacheInstance->set($cacheIdentifier, $configurationObject); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $configurationObject; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return ConfigurationObjectInstance |
153
|
|
|
*/ |
154
|
|
|
protected function buildConfigurationObject() |
155
|
|
|
{ |
156
|
|
|
return $this->getConfigurationObjectInstance($this->sanitizeConfiguration($this->configurationArray)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* This function will clean the configuration array by removing useless data |
161
|
|
|
* and updating needed ones. |
162
|
|
|
* |
163
|
|
|
* @param array $configuration |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
protected function sanitizeConfiguration(array $configuration) |
167
|
|
|
{ |
168
|
|
|
// Removing configuration of fields which do not exist for this form. |
169
|
|
|
$sanitizedFieldsConfiguration = []; |
170
|
|
|
$fieldsConfiguration = (isset($configuration['fields'])) |
171
|
|
|
? $configuration['fields'] |
172
|
|
|
: []; |
173
|
|
|
|
174
|
|
|
foreach ($this->formObject->getProperties() as $property) { |
175
|
|
|
$sanitizedFieldsConfiguration[$property] = (isset($fieldsConfiguration[$property])) |
176
|
|
|
? $fieldsConfiguration[$property] |
177
|
|
|
: []; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$configuration['fields'] = $sanitizedFieldsConfiguration; |
181
|
|
|
|
182
|
|
|
return $configuration; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param array $configuration |
187
|
|
|
* @return ConfigurationObjectInstance |
188
|
|
|
*/ |
189
|
|
|
protected function getConfigurationObjectInstance(array $configuration) |
190
|
|
|
{ |
191
|
|
|
return ConfigurationObjectFactory::getInstance()->get(Form::class, $configuration); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return FrontendInterface |
196
|
|
|
*/ |
197
|
|
|
protected function getCacheInstance() |
198
|
|
|
{ |
199
|
|
|
return CacheService::get()->getCacheInstance(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param ConfigurationFactory $configurationFactory |
204
|
|
|
*/ |
205
|
|
|
public function injectConfigurationFactory(ConfigurationFactory $configurationFactory) |
206
|
|
|
{ |
207
|
|
|
$this->configurationFactory = $configurationFactory; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function __sleep() |
211
|
|
|
{ |
212
|
|
|
return ['formObject', 'configurationArray']; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* When this class is unserialized, the dependencies are injected. |
217
|
|
|
*/ |
218
|
|
|
public function __wakeup() |
219
|
|
|
{ |
220
|
|
|
/** @var ConfigurationFactory $configurationFactory */ |
221
|
|
|
$configurationFactory = Core::instantiate(ConfigurationFactory::class); |
222
|
|
|
|
223
|
|
|
$this->injectConfigurationFactory($configurationFactory); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|