Completed
Push — master ( 405deb...3788b5 )
by
unknown
03:31
created

Core::injectObjectManager()   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 1
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\Core;
15
16
use Romm\Formz\Configuration\ConfigurationFactory;
17
use Romm\Formz\Form\FormObjectFactory;
18
use Romm\Formz\Utility\TypoScriptUtility;
19
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20
use TYPO3\CMS\Core\Cache\CacheManager;
21
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
22
use TYPO3\CMS\Core\SingletonInterface;
23
use TYPO3\CMS\Core\Utility\ArrayUtility;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Core\Utility\MathUtility;
26
use TYPO3\CMS\Extbase\Object\ObjectManager;
27
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
28
use TYPO3\CMS\Extbase\Service\EnvironmentService;
29
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
30
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
31
32
/**
33
 * Class containing general functions.
34
 */
35
class Core implements SingletonInterface
36
{
37
38
    const EXTENSION_KEY = 'formz';
39
    const CACHE_IDENTIFIER = 'cache_formz';
40
    const GENERATED_FILES_PATH = 'typo3temp/Formz/';
41
42
    /**
43
     * @var Core
44
     */
45
    protected static $instance;
46
47
    /**
48
     * @var int|null
49
     */
50
    private $currentPageUid = -1;
51
52
    /**
53
     * @var ObjectManagerInterface
54
     */
55
    private $objectManager;
56
57
    /**
58
     * @var TypoScriptUtility
59
     */
60
    private $typoScriptUtility;
61
62
    /**
63
     * @var ConfigurationFactory
64
     */
65
    private $configurationFactory;
66
67
    /**
68
     * @var FormObjectFactory
69
     */
70
    private $formObjectFactory;
71
72
    /**
73
     * Contains the actual language key.
74
     *
75
     * @var string
76
     */
77
    private $languageKey;
78
79
    /**
80
     * @var array
81
     */
82
    private $extensionConfiguration;
83
84
    /**
85
     * @var FrontendInterface
86
     */
87
    protected $cacheInstance;
88
89
    /**
90
     * @return Core
91
     */
92
    public static function get()
93
    {
94
        if (null === self::$instance) {
95
            /** @var ObjectManager $objectManager */
96
            $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
97
98
            self::$instance = $objectManager->get(self::class);
99
        }
100
101
        return self::$instance;
102
    }
103
104
    /**
105
     * Translation handler. Does the same job as Extbase translation tools,
106
     * expect that if the index to the LLL reference is not found, the index is
107
     * returned (Extbase would have returned an empty string).
108
     *
109
     * @param    string $index        The index to the LLL reference.
110
     * @param    string $extensionKey Key of the extension containing the LLL reference.
111
     * @param    array  $arguments    Arguments passed over to vsprintf.
112
     * @return   string               The translated string.
113
     */
114
    public function translate($index, $extensionKey = null, $arguments = null)
115
    {
116
        $extensionKey = ($extensionKey) ?: self::EXTENSION_KEY;
117
        $result = LocalizationUtility::translate($index, $extensionKey, $arguments);
118
        if ($result === '' && $index !== '') {
119
            $result = $index;
120
        }
121
122
        return $result;
123
    }
124
125
    /**
126
     * Converts an array to a clean JSON string which can be used by JavaScript.
127
     *
128
     * @param array $array
129
     * @return string
130
     */
131
    public function arrayToJavaScriptJson(array $array)
132
    {
133
        return json_encode($array, JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_TAG);
134
    }
135
136
    /**
137
     * Returns the current page uid, in a frontend or backend context.
138
     *
139
     * Returns null if the uid can't be found (backend module, ajax call, etc.).
140
     *
141
     * @return int|null
142
     */
143
    public function getCurrentPageUid()
144
    {
145
        if (-1 === $this->currentPageUid) {
146
            /** @var EnvironmentService $environmentService */
147
            $environmentService = GeneralUtility::makeInstance(EnvironmentService::class);
148
149
            $id = ($environmentService->isEnvironmentInFrontendMode())
150
                ? $this->getPageController()->id
151
                : GeneralUtility::_GP('id');
152
153
            if (false === MathUtility::canBeInterpretedAsInteger($id)
154
                || intval($id) < 0
155
            ) {
156
                $id = null;
157
            }
158
159
            $this->currentPageUid = $id;
160
        }
161
162
        return $this->currentPageUid;
163
    }
164
165
    /**
166
     * Allows you to set manually the current page uid. Useful when editing a
167
     * record, for example.
168
     *
169
     * @param int $uid The uid of the page.
170
     */
171
    public function setCurrentPageUid($uid)
172
    {
173
        $this->currentPageUid = intval($uid);
174
    }
175
176
    /**
177
     * Returns the cache instance for this extension.
178
     *
179
     * @return FrontendInterface
180
     */
181
    public function getCacheInstance()
182
    {
183
        if (null === $this->cacheInstance) {
184
            /** @var $cacheManager CacheManager */
185
            $cacheManager = $this->getObjectManager()->get(CacheManager::class);
186
187
            if ($cacheManager->hasCache(self::CACHE_IDENTIFIER)) {
188
                $this->cacheInstance = $cacheManager->getCache(self::CACHE_IDENTIFIER);
189
            }
190
        }
191
192
        return $this->cacheInstance;
193
    }
194
195
    /**
196
     * @param FrontendInterface $cacheInstance
197
     */
198
    public function setCacheInstance(FrontendInterface $cacheInstance)
199
    {
200
        $this->cacheInstance = $cacheInstance;
201
    }
202
203
    /**
204
     * Generic cache identifier creation for usages in the extension.
205
     *
206
     * @param string $string
207
     * @param string $formClassName
208
     * @param int    $maxLength
209
     * @return string
210
     */
211
    public function getCacheIdentifier($string, $formClassName, $maxLength = 55)
212
    {
213
        $explodedClassName = explode('\\', $formClassName);
214
215
        $identifier = strtolower(
216
            $string .
217
            end($explodedClassName) .
218
            '-' .
219
            sha1($formClassName)
220
        );
221
222
        return substr($identifier, 0, $maxLength);
223
    }
224
225
    /**
226
     * Return the extension configuration.
227
     *
228
     * @param string $configurationName If null, returns the whole configuration. Otherwise, returns the asked configuration.
229
     * @return array
230
     */
231
    public function getExtensionConfiguration($configurationName = null)
232
    {
233
        if (null === $this->extensionConfiguration) {
234
            $this->extensionConfiguration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::EXTENSION_KEY]);
235
            if (false === $this->extensionConfiguration) {
236
                $this->extensionConfiguration = [];
237
            }
238
        }
239
240
        $result = null;
241
        if (null === $configurationName) {
242
            $result = $this->extensionConfiguration;
243
        } elseif (ArrayUtility::isValidPath($this->extensionConfiguration, $configurationName, '.')) {
244
            $result = ArrayUtility::getValueByPath($this->extensionConfiguration, $configurationName, '.');
245
        }
246
247
        return $result;
248
    }
249
250
    /**
251
     * Function called when clearing TYPO3 caches. It will remove the temporary
252
     * asset files created by Formz.
253
     *
254
     * @param array $parameters
255
     * @return void
256
     */
257
    public function clearCacheCommand($parameters)
258
    {
259
        if (false === in_array($parameters['cacheCmd'], ['all', 'system'])) {
260
            return;
261
        }
262
263
        $files = glob(GeneralUtility::getFileAbsFileName(self::GENERATED_FILES_PATH . '*'));
264
265
        if (false === $files) {
266
            return;
267
        }
268
269
        foreach ($files as $assetCacheFile) {
270
            unlink($assetCacheFile);
271
        }
272
    }
273
274
    /**
275
     * Returns the current language key.
276
     *
277
     * @return string
278
     */
279
    public function getLanguageKey()
280
    {
281
        if (null === $this->languageKey) {
282
            $this->languageKey = 'default';
283
284
            /** @var EnvironmentService $environmentService */
285
            $environmentService = GeneralUtility::makeInstance(EnvironmentService::class);
286
287
            if ($environmentService->isEnvironmentInFrontendMode()) {
288
                $pageController = $this->getPageController();
289
290
                if (isset($pageController->config['config']['language'])) {
291
                    $this->languageKey = $pageController->config['config']['language'];
292
                }
293
            } else {
294
                $backendUser = $this->getBackendUser();
295
296
                if (strlen($backendUser->uc['lang']) > 0) {
297
                    $this->languageKey = $backendUser->uc['lang'];
298
                }
299
            }
300
        }
301
302
        return $this->languageKey;
303
    }
304
305
    /**
306
     * @return bool
307
     */
308
    public function isInDebugMode()
309
    {
310
        return (bool)$this->getExtensionConfiguration('debugMode');
311
    }
312
313
    /**
314
     * @return ObjectManagerInterface
315
     */
316
    public function getObjectManager()
317
    {
318
        return $this->objectManager;
319
    }
320
321
    /**
322
     * @param ObjectManagerInterface $objectManager
323
     */
324
    public function injectObjectManager(ObjectManagerInterface $objectManager)
325
    {
326
        $this->objectManager = $objectManager;
327
    }
328
329
    /**
330
     * @return TypoScriptUtility
331
     */
332
    public function getTypoScriptUtility()
333
    {
334
        return $this->typoScriptUtility;
335
    }
336
337
    /**
338
     * @param TypoScriptUtility $typoScriptUtility
339
     */
340
    public function injectTypoScriptUtility(TypoScriptUtility $typoScriptUtility)
341
    {
342
        $this->typoScriptUtility = $typoScriptUtility;
343
    }
344
345
    /**
346
     * @return ConfigurationFactory
347
     */
348
    public function getConfigurationFactory()
349
    {
350
        return $this->configurationFactory;
351
    }
352
353
    /**
354
     * @param ConfigurationFactory $configurationFactory
355
     */
356
    public function injectConfigurationFactory(ConfigurationFactory $configurationFactory)
357
    {
358
        $this->configurationFactory = $configurationFactory;
359
    }
360
361
    /**
362
     * @return FormObjectFactory
363
     */
364
    public function getFormObjectFactory()
365
    {
366
        return $this->formObjectFactory;
367
    }
368
369
    /**
370
     * @param FormObjectFactory $formObjectFactory
371
     */
372
    public function injectFormObjectFactory(FormObjectFactory $formObjectFactory)
373
    {
374
        $this->formObjectFactory = $formObjectFactory;
375
    }
376
377
    /**
378
     * Returns the extension key.
379
     *
380
     * @return string
381
     */
382
    public function getExtensionKey()
383
    {
384
        return self::EXTENSION_KEY;
385
    }
386
387
    /**
388
     * @return TypoScriptFrontendController
389
     */
390
    public function getPageController()
391
    {
392
        return $GLOBALS['TSFE'];
393
    }
394
395
    /**
396
     * @return BackendUserAuthentication
397
     */
398
    public function getBackendUser()
399
    {
400
        return $GLOBALS['BE_USER'];
401
    }
402
}
403