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