Completed
Push — backend-compatibility ( 20ef2e...c3c5b5 )
by Romain
04:38 queued 01:14
created

TypoScriptUtility::getFullConfiguration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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