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\Configuration; |
15
|
|
|
|
16
|
|
|
use Romm\ConfigurationObject\ConfigurationObjectFactory; |
17
|
|
|
use Romm\ConfigurationObject\ConfigurationObjectInstance; |
18
|
|
|
use Romm\Formz\Core\Core; |
19
|
|
|
use Romm\Formz\Service\TypoScriptService; |
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
|
|
|
* @var TypoScriptService |
32
|
|
|
*/ |
33
|
|
|
protected $typoScriptService; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ConfigurationObjectInstance[] |
37
|
|
|
*/ |
38
|
|
|
protected $instances = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $cacheIdentifiers = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns the global Formz configuration. |
47
|
|
|
* |
48
|
|
|
* Two cache layers are used: |
49
|
|
|
* |
50
|
|
|
* - A local cache which will avoid fetching the configuration every time |
51
|
|
|
* the current script needs it. |
52
|
|
|
* - A system cache, which will store the configuration instance when it has |
53
|
|
|
* been built, improving performance for next scripts. |
54
|
|
|
* |
55
|
|
|
* @return ConfigurationObjectInstance |
56
|
|
|
*/ |
57
|
|
|
public function getFormzConfiguration() |
58
|
|
|
{ |
59
|
|
|
$cacheIdentifier = $this->getCacheIdentifier(); |
60
|
|
|
|
61
|
|
|
if (false === array_key_exists($cacheIdentifier, $this->instances)) { |
62
|
|
|
$this->instances[$cacheIdentifier] = $this->getFormzConfigurationFromCache($cacheIdentifier); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->instances[$cacheIdentifier]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Will fetch the configuration from cache, and build it if not found. It |
70
|
|
|
* wont be stored in cache if any error is found. This is done this way to |
71
|
|
|
* avoid the integrator to be forced to flush caches when errors are found. |
72
|
|
|
* |
73
|
|
|
* @param string $cacheIdentifier |
74
|
|
|
* @return ConfigurationObjectInstance |
75
|
|
|
*/ |
76
|
|
|
protected function getFormzConfigurationFromCache($cacheIdentifier) |
77
|
|
|
{ |
78
|
|
|
$cacheInstance = Core::get()->getCacheInstance(); |
79
|
|
|
|
80
|
|
|
if ($cacheInstance->has($cacheIdentifier)) { |
81
|
|
|
$instance = $cacheInstance->get($cacheIdentifier); |
82
|
|
|
} else { |
83
|
|
|
$instance = $this->buildFormzConfiguration(); |
84
|
|
|
|
85
|
|
|
if (false === $instance->getValidationResult()->hasErrors()) { |
86
|
|
|
$cacheInstance->set($cacheIdentifier, $instance); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $instance; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @see getFormzConfiguration() |
95
|
|
|
* |
96
|
|
|
* @return ConfigurationObjectInstance |
97
|
|
|
*/ |
98
|
|
|
protected function buildFormzConfiguration() |
99
|
|
|
{ |
100
|
|
|
$configuration = $this->typoScriptService->getFormzConfiguration(); |
101
|
|
|
$instance = ConfigurationObjectFactory::getInstance() |
102
|
|
|
->get(Configuration::class, $configuration); |
103
|
|
|
|
104
|
|
|
/** @var Configuration $instanceObject */ |
105
|
|
|
$instanceObject = $instance->getObject(true); |
106
|
|
|
$instanceObject->calculateHash(); |
107
|
|
|
|
108
|
|
|
return $instance; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
protected function getCacheIdentifier() |
115
|
|
|
{ |
116
|
|
|
$contextHash = Core::get()->getContextHash(); |
117
|
|
|
|
118
|
|
|
if (false === array_key_exists($contextHash, $this->cacheIdentifiers)) { |
119
|
|
|
$configuration = $this->typoScriptService->getFormzConfiguration(); |
120
|
|
|
|
121
|
|
|
$this->cacheIdentifiers[$contextHash] = 'formz-configuration-' . sha1(serialize($configuration)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->cacheIdentifiers[$contextHash]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param TypoScriptService $typoScriptService |
129
|
|
|
*/ |
130
|
|
|
public function injectTypoScriptService(TypoScriptService $typoScriptService) |
131
|
|
|
{ |
132
|
|
|
$this->typoScriptService = $typoScriptService; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|