|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* 2016 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\Configuration; |
|
15
|
|
|
|
|
16
|
|
|
use Romm\ConfigurationObject\ConfigurationObjectInstance; |
|
17
|
|
|
use Romm\ConfigurationObject\ConfigurationObjectFactory; |
|
18
|
|
|
use Romm\Formz\Core\Core; |
|
19
|
|
|
use Romm\Formz\Form\FormObject; |
|
20
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* This class is used to build and manage the whole Formz configuration: from a |
|
24
|
|
|
* plain configuration array, it builds an entire tree object which will give |
|
25
|
|
|
* all the features from the `configuration_object` extension (parent |
|
26
|
|
|
* inheritance, array keys save, etc.). |
|
27
|
|
|
*/ |
|
28
|
|
|
class ConfigurationFactory implements SingletonInterface |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ConfigurationObjectInstance[] |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $instances = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $cacheIdentifiers = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns the global Formz configuration. |
|
43
|
|
|
* |
|
44
|
|
|
* Two cache layers are used: |
|
45
|
|
|
* |
|
46
|
|
|
* - A local cache which will avoid fetching the configuration every time |
|
47
|
|
|
* the current script needs it. |
|
48
|
|
|
* - A system cache, which will store the configuration instance when it has |
|
49
|
|
|
* been built, improving performance for next scripts. |
|
50
|
|
|
* |
|
51
|
|
|
* @return ConfigurationObjectInstance |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getFormzConfiguration() |
|
54
|
|
|
{ |
|
55
|
|
|
$cacheIdentifier = $this->getCacheIdentifier(); |
|
56
|
|
|
|
|
57
|
|
|
if (null === $this->instances[$cacheIdentifier]) { |
|
58
|
|
|
$this->instances[$cacheIdentifier] = $this->getFormzConfigurationFromCache($cacheIdentifier); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this->instances[$cacheIdentifier]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Will fetch the configuration from cache, and build it if not found. It |
|
66
|
|
|
* wont be stored in cache if any error is found. This is done this way to |
|
67
|
|
|
* avoid the integrator to be forced to flush caches when errors are found. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $cacheIdentifier |
|
70
|
|
|
* @return ConfigurationObjectInstance |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function getFormzConfigurationFromCache($cacheIdentifier) |
|
73
|
|
|
{ |
|
74
|
|
|
$cacheInstance = Core::get()->getCacheInstance(); |
|
75
|
|
|
|
|
76
|
|
|
if ($cacheInstance->has($cacheIdentifier)) { |
|
77
|
|
|
$instance = $cacheInstance->get($cacheIdentifier); |
|
78
|
|
|
} else { |
|
79
|
|
|
$instance = $this->buildFormzConfiguration(); |
|
80
|
|
|
|
|
81
|
|
|
if (false === $instance->getValidationResult()->hasErrors()) { |
|
82
|
|
|
$cacheInstance->set($cacheIdentifier, $instance); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $instance; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @see getFormzConfiguration() |
|
91
|
|
|
* |
|
92
|
|
|
* @return ConfigurationObjectInstance |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function buildFormzConfiguration() |
|
95
|
|
|
{ |
|
96
|
|
|
$configuration = Core::get()->getTypoScriptUtility()->getFormzConfiguration(); |
|
97
|
|
|
$instance = ConfigurationObjectFactory::getInstance() |
|
98
|
|
|
->get(Configuration::class, $configuration); |
|
99
|
|
|
|
|
100
|
|
|
/** @var Configuration $instanceObject */ |
|
101
|
|
|
$instanceObject = $instance->getObject(true); |
|
102
|
|
|
$instanceObject->calculateHashes(); |
|
103
|
|
|
|
|
104
|
|
|
return $instance; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Will fetch the TypoScript configuration for the given form class name, |
|
109
|
|
|
* convert it to a configuration object, then add it to the list of forms in |
|
110
|
|
|
* the global Formz configuration - which you can access with the function |
|
111
|
|
|
* `getFormzConfiguration()`. |
|
112
|
|
|
* |
|
113
|
|
|
* @param FormObject $form |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
|
|
public function addForm(FormObject $form) |
|
117
|
|
|
{ |
|
118
|
|
|
/** @var Configuration $formzConfigurationObject */ |
|
119
|
|
|
$formzConfigurationObject = $this->getFormzConfiguration()->getObject(true); |
|
120
|
|
|
|
|
121
|
|
|
if (false === $formzConfigurationObject->hasForm($form->getClassName(), $form->getName())) { |
|
122
|
|
|
$formzConfigurationObject->addForm($form); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function getCacheIdentifier() |
|
132
|
|
|
{ |
|
133
|
|
|
$pageUid = Core::get()->getCurrentPageUid(); |
|
134
|
|
|
|
|
135
|
|
|
if (false === array_key_exists($pageUid, $this->cacheIdentifiers)) { |
|
136
|
|
|
$configuration = Core::get()->getTypoScriptUtility()->getFormzConfiguration(); |
|
137
|
|
|
|
|
138
|
|
|
$this->cacheIdentifiers[$pageUid] = 'formz-configuration-' . sha1(serialize($configuration)); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $this->cacheIdentifiers[$pageUid]; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|