Completed
Push — backend-compatibility ( c3c5b5...b6bc18 )
by Romain
02:26
created

TypoScriptUtility::getFrontendTypoScriptConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
21
use TYPO3\CMS\Extbase\Service\TypoScriptService;
22
23
/**
24
 * Handles the TypoScript configuration of the extension.
25
 */
26
class TypoScriptUtility implements SingletonInterface
27
{
28
    const EXTENSION_CONFIGURATION_PATH = 'config.tx_formz';
29
30
    /**
31
     * @var TypoScriptService
32
     */
33
    protected $typoScriptService;
34
35
    /**
36
     * Storage for the pages configuration.
37
     *
38
     * @var array
39
     */
40
    protected $configuration = [];
41
42
    /**
43
     * Returns the TypoScript configuration at the given path (starting from
44
     * Formz configuration root).
45
     *
46
     * @param string $path
47
     * @return mixed
48
     */
49
    public function getExtensionConfigurationFromPath($path)
50
    {
51
        $extensionConfiguration = $this->getExtensionConfiguration();
52
53
        return (ArrayUtility::isValidPath($extensionConfiguration, $path, '.'))
54
            ? ArrayUtility::getValueByPath($extensionConfiguration, $path, '.')
55
            : null;
56
    }
57
58
    /**
59
     * Returns the TypoScript configuration for the given form class name.
60
     *
61
     * @param string $formClassName
62
     * @return array
63
     */
64
    public function getFormConfiguration($formClassName)
65
    {
66
        $formzConfiguration = $this->getExtensionConfiguration();
67
68
        return (isset($formzConfiguration['forms'][$formClassName]))
69
            ? $formzConfiguration['forms'][$formClassName]
70
            : [];
71
    }
72
73
    /**
74
     * Returns the full Formz TypoScript configuration, but without the `forms`
75
     * key.
76
     *
77
     * @return array
78
     */
79
    public function getFormzConfiguration()
80
    {
81
        $configuration = $this->getExtensionConfiguration();
82
        unset($configuration['forms']);
83
84
        return $configuration;
85
    }
86
87
    /**
88
     * This function will fetch the extension TypoScript configuration, and
89
     * store it in cache for further usage.
90
     *
91
     * The configuration array is not stored in cache if the configuration
92
     * property `settings.typoScriptIncluded` is not found.
93
     *
94
     * @return array
95
     */
96
    protected function getExtensionConfiguration()
97
    {
98
        $cacheInstance = Core::get()->getCacheInstance();
99
        $hash = $this->getContextHash();
100
101
        if ($cacheInstance->has($hash)) {
102
            $result = $cacheInstance->get($hash);
103
        } else {
104
            $result = $this->getFullConfiguration();
105
            $result = (ArrayUtility::isValidPath($result, self::EXTENSION_CONFIGURATION_PATH, '.'))
106
                ? ArrayUtility::getValueByPath($result, self::EXTENSION_CONFIGURATION_PATH, '.')
107
                : [];
108
109
            if (ArrayUtility::isValidPath($result, 'settings.typoScriptIncluded', '.')) {
110
                $cacheInstance->set($hash, $result);
111
            }
112
        }
113
114
        return $result;
115
    }
116
117
    /**
118
     * Returns the full TypoScript configuration, based on the context of the
119
     * current request.
120
     *
121
     * @return array
122
     */
123
    protected function getFullConfiguration()
124
    {
125
        $contextHash = $this->getContextHash();
126
127
        if (false === array_key_exists($contextHash, $this->configuration)) {
128
            if (Core::get()->getEnvironmentService()->isEnvironmentInFrontendMode()) {
129
                $typoScriptArray = $this->getFrontendTypoScriptConfiguration();
130
            } else {
131
                $typoScriptArray = $this->getBackendTypoScriptConfiguration();
132
            }
133
134
            $this->configuration[$contextHash] = $this->typoScriptService->convertTypoScriptArrayToPlainArray($typoScriptArray);
135
        }
136
137
        return $this->configuration[$contextHash];
138
    }
139
140
    /**
141
     * @return array
142
     */
143
    protected function getFrontendTypoScriptConfiguration()
144
    {
145
        return Core::get()->getPageController()->tmpl->setup;
146
    }
147
148
    /**
149
     * @return array
150
     */
151
    protected function getBackendTypoScriptConfiguration()
152
    {
153
        /** @var ConfigurationManager $configurationManager */
154
        $configurationManager = Core::get()->getObjectManager()->get(ConfigurationManager::class);
155
156
        return $configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
157
    }
158
159
    /**
160
     * Returns a unique hash for the context of the current request, depending
161
     * on wether the request comes from frontend or backend.
162
     *
163
     * @return string
164
     */
165
    protected function getContextHash()
166
    {
167
        $hash = (Core::get()->getEnvironmentService()->isEnvironmentInFrontendMode())
168
            ? 'fe-' . Core::get()->getCurrentPageUid()
169
            : 'be-' . Core::get()->sanitizeString(GeneralUtility::_GET('M'));
170
171
        return 'ts-conf-' . $hash;
172
    }
173
174
    /**
175
     * @param TypoScriptService $typoScriptService
176
     */
177
    public function injectTypoScriptService(TypoScriptService $typoScriptService)
178
    {
179
        $this->typoScriptService = $typoScriptService;
180
    }
181
}
182