Completed
Push — revert-41-development ( 361644 )
by Romain
03:56 queued 01:50
created

TypoScriptUtility::getBackendTypoScriptConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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\Utility;
15
16
use Romm\Formz\Core\Core;
17
use TYPO3\CMS\Core\SingletonInterface;
18
use TYPO3\CMS\Core\Utility\ArrayUtility;
19
use TYPO3\CMS\Extbase\Service\EnvironmentService;
20
use TYPO3\CMS\Extbase\Service\TypoScriptService;
21
22
/**
23
 * Handles the TypoScript configuration of the extension.
24
 */
25
class TypoScriptUtility implements SingletonInterface
26
{
27
    const EXTENSION_CONFIGURATION_PATH = 'config.tx_formz';
28
29
    const PAGES_CONFIGURATION_HASHES_CACHE_IDENTIFIER = 'ts-conf-hash-pages';
30
31
    /**
32
     * @var EnvironmentService
33
     */
34
    protected $environmentService;
35
36
    /**
37
     * @var TypoScriptService
38
     */
39
    protected $typoScriptService;
40
41
    /**
42
     * Storage for the pages configuration.
43
     *
44
     * @var array
45
     */
46
    protected $pageConfiguration = [];
47
48
    /**
49
     * @var array
50
     */
51
    protected $pagesConfigurationHashes;
52
53
    /**
54
     * Calls the function `getConfigurationFromPath`, but uses the extension
55
     * configuration path as root path.
56
     *
57
     * @param string        $path      The path to the configuration value. If null is given, the whole extension configuration is returned.
58
     * @param int|null|bool $pageUid   The uid of the page you want the TypoScript configuration from. If `null` is given, the current page uid is used.
59
     * @param string        $delimiter The delimiter for the path. Default is ".".
60
     * @return mixed|null
61
     */
62
    public function getExtensionConfigurationFromPath($path = null, $pageUid = null, $delimiter = '.')
63
    {
64
        $extensionConfiguration = $this->getFullExtensionConfiguration($pageUid);
65
66
        if (null === $path) {
67
            $result = $extensionConfiguration;
68
        } else {
69
            $result = (ArrayUtility::isValidPath($extensionConfiguration, $path, $delimiter))
70
                ? ArrayUtility::getValueByPath($extensionConfiguration, $path, $delimiter)
71
                : null;
72
        }
73
74
        return $result;
75
    }
76
77
    /**
78
     * Returns the TypoScript configuration for the given form class name.
79
     *
80
     * @param string $formClassName
81
     * @return array
82
     */
83
    public function getFormConfiguration($formClassName)
84
    {
85
        $formzConfiguration = $this->getExtensionConfigurationFromPath();
86
87
        return (isset($formzConfiguration['forms'][$formClassName]))
88
            ? $formzConfiguration['forms'][$formClassName]
89
            : [];
90
    }
91
92
    /**
93
     * Returns the full Formz TypoScript configuration, but without the `forms`
94
     * key.
95
     *
96
     * @return array
97
     */
98
    public function getFormzConfiguration()
99
    {
100
        $configuration = $this->getExtensionConfigurationFromPath();
101
        unset($configuration['forms']);
102
103
        return $configuration;
104
    }
105
106
    /**
107
     * This function will fetch the extension TypoScript configuration. There
108
     * are two levels of cache: one cache entry is used to store identifiers for
109
     * the second level of configuration caches: for every page id on which
110
     * there is a need to access the Formz configuration.
111
     *
112
     * @param int|null $pageUid The uid of the page you want the TypoScript configuration from. If `null` is given, the current page uid is used.
113
     * @return array
114
     */
115
    protected function getFullExtensionConfiguration($pageUid = null)
116
    {
117
        $result = null;
118
        $pageUid = $this->getRealPageUid($pageUid);
119
        $cacheInstance = Core::get()->getCacheInstance();
120
121
        if (null === $this->pagesConfigurationHashes) {
122
            $this->pagesConfigurationHashes = ($cacheInstance->has(self::PAGES_CONFIGURATION_HASHES_CACHE_IDENTIFIER))
123
                ? $cacheInstance->get(self::PAGES_CONFIGURATION_HASHES_CACHE_IDENTIFIER)
124
                : [];
125
        }
126
127
        if (true === isset($this->pagesConfigurationHashes[$pageUid])) {
128
            $hash = $this->pagesConfigurationHashes[$pageUid];
129
130
            if ($cacheInstance->has($hash)) {
131
                $result = $cacheInstance->get($hash);
132
            }
133
        }
134
135
        if (null === $result) {
136
            $result = $this->getConfiguration($pageUid);
137
138
            $result = (ArrayUtility::isValidPath($result, self::EXTENSION_CONFIGURATION_PATH, '.'))
139
                ? ArrayUtility::getValueByPath($result, self::EXTENSION_CONFIGURATION_PATH, '.')
140
                : [];
141
142
            $hash = 'ts-conf-page-' . sha1(serialize($result));
143
144
            $cacheInstance->set($hash, $result);
145
146
            $this->pagesConfigurationHashes[$pageUid] = $hash;
147
            $cacheInstance->set(self::PAGES_CONFIGURATION_HASHES_CACHE_IDENTIFIER, $this->pagesConfigurationHashes);
148
        }
149
150
        return $result;
151
    }
152
153
    /**
154
     * Returns the TypoScript configuration, including the static configuration
155
     * from files (see function `getExtensionConfiguration()`).
156
     *
157
     * As this function does not save the configuration in cache, we advise not
158
     * to call it, and prefer using the function `getConfigurationFromPath()`
159
     * instead, which has its own caching system.
160
     *
161
     * It can still be useful to get the whole TypoScript configuration, so the
162
     * function remains public, but use with caution!
163
     *
164
     * @param int|null $pageUid The uid of the page you want the TypoScript configuration from. If `null` is given, the current page uid is used.
165
     * @return array The configuration.
166
     */
167
    public function getConfiguration($pageUid = null)
168
    {
169
        $pageUid = $this->getRealPageUid($pageUid);
170
171
        if (!array_key_exists($pageUid, $this->pageConfiguration)) {
172
            if ($this->environmentService->isEnvironmentInFrontendMode()) {
173
                $typoScriptArray = Core::get()->getPageController()->tmpl->setup;
174
            } else {
175
                // @todo: backend context
176
                $typoScriptArray = [];
177
            }
178
179
            $this->pageConfiguration[$pageUid] = $this->typoScriptService->convertTypoScriptArrayToPlainArray($typoScriptArray);
180
        }
181
182
        return $this->pageConfiguration[$pageUid];
183
    }
184
185
    /**
186
     * Determines the real page uid, depending on the type of the parameter.
187
     *
188
     * @param  int|null $pageUid The page uid.
189
     * @return int|null The real page uid.
190
     */
191
    private function getRealPageUid($pageUid)
192
    {
193
        return ($pageUid === null)
194
            ? Core::get()->getCurrentPageUid()
195
            : $pageUid;
196
    }
197
198
    /**
199
     * @param EnvironmentService $environmentService
200
     */
201
    public function injectEnvironmentService(EnvironmentService $environmentService)
202
    {
203
        $this->environmentService = $environmentService;
204
    }
205
206
    /**
207
     * @param TypoScriptService $typoScriptService
208
     */
209
    public function injectTypoScriptService(TypoScriptService $typoScriptService)
210
    {
211
        $this->typoScriptService = $typoScriptService;
212
    }
213
}
214