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