Completed
Pull Request — master (#14)
by
unknown
02:13
created

Core::getExtensionRelativePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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\ExtensionManagementUtility;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Core\Utility\MathUtility;
27
use TYPO3\CMS\Extbase\Object\ObjectManager;
28
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
29
use TYPO3\CMS\Extbase\Service\EnvironmentService;
30
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
31
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
32
33
/**
34
 * Class containing general functions.
35
 */
36
class Core implements SingletonInterface
37
{
38
39
    const EXTENSION_KEY = 'formz';
40
    const CACHE_IDENTIFIER = 'cache_formz';
41
    const GENERATED_FILES_PATH = 'typo3temp/Formz/';
42
43
    /**
44
     * @var Core
45
     */
46
    protected static $instance;
47
48
    /**
49
     * @var int|null
50
     */
51
    private $currentPageUid = -1;
52
53
    /**
54
     * @var ObjectManagerInterface
55
     */
56
    private $objectManager;
57
58
    /**
59
     * @var TypoScriptUtility
60
     */
61
    private $typoScriptUtility;
62
63
    /**
64
     * @var ConfigurationFactory
65
     */
66
    private $configurationFactory;
67
68
    /**
69
     * @var FormObjectFactory
70
     */
71
    private $formObjectFactory;
72
73
    /**
74
     * Contains the actual language key.
75
     *
76
     * @var string
77
     */
78
    private $languageKey;
79
80
    /**
81
     * @var array
82
     */
83
    private $extensionConfiguration;
84
85
    /**
86
     * @var FrontendInterface
87
     */
88
    protected $cacheInstance;
89
90
    /**
91
     * @return Core
92
     */
93
    public static function get()
94
    {
95
        if (null === self::$instance) {
96
            /** @var ObjectManager $objectManager */
97
            $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
98
99
            self::$instance = $objectManager->get(self::class);
100
        }
101
102
        return self::$instance;
103
    }
104
105
    /**
106
     * Translation handler. Does the same job as Extbase translation tools,
107
     * expect that if the index to the LLL reference is not found, the index is
108
     * returned (Extbase would have returned an empty string).
109
     *
110
     * @param    string $index        The index to the LLL reference.
111
     * @param    string $extensionKey Key of the extension containing the LLL reference.
112
     * @param    array  $arguments    Arguments passed over to vsprintf.
113
     * @return   string               The translated string.
114
     */
115
    public function translate($index, $extensionKey = null, $arguments = null)
116
    {
117
        $extensionKey = ($extensionKey) ?: self::EXTENSION_KEY;
118
        $result = LocalizationUtility::translate($index, $extensionKey, $arguments);
119
        if ($result === '' && $index !== '') {
120
            $result = $index;
121
        }
122
123
        return $result;
124
    }
125
126
    /**
127
     * Converts an array to a clean JSON string which can be used by JavaScript.
128
     *
129
     * @param array $array
130
     * @return string
131
     */
132
    public function arrayToJavaScriptJson(array $array)
133
    {
134
        return json_encode($array, JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_TAG);
135
    }
136
137
    /**
138
     * Returns the current page uid, in a frontend or backend context.
139
     *
140
     * Returns null if the uid can't be found (backend module, ajax call, etc.).
141
     *
142
     * @return int|null
143
     */
144
    public function getCurrentPageUid()
145
    {
146
        if (-1 === $this->currentPageUid) {
147
            /** @var EnvironmentService $environmentService */
148
            $environmentService = GeneralUtility::makeInstance(EnvironmentService::class);
149
150
            $id = ($environmentService->isEnvironmentInFrontendMode())
151
                ? $this->getPageController()->id
152
                : GeneralUtility::_GP('id');
153
154
            if (false === MathUtility::canBeInterpretedAsInteger($id)
155
                || intval($id) < 0
156
            ) {
157
                $id = null;
158
            }
159
160
            $this->currentPageUid = $id;
161
        }
162
163
        return $this->currentPageUid;
164
    }
165
166
    /**
167
     * Allows you to set manually the current page uid. Useful when editing a
168
     * record, for example.
169
     *
170
     * @param int $uid The uid of the page.
171
     */
172
    public function setCurrentPageUid($uid)
173
    {
174
        $this->currentPageUid = intval($uid);
175
    }
176
177
    /**
178
     * Returns the cache instance for this extension.
179
     *
180
     * @return FrontendInterface
181
     */
182
    public function getCacheInstance()
183
    {
184
        if (null === $this->cacheInstance) {
185
            /** @var $cacheManager CacheManager */
186
            $cacheManager = $this->getObjectManager()->get(CacheManager::class);
187
188
            if ($cacheManager->hasCache(self::CACHE_IDENTIFIER)) {
189
                $this->cacheInstance = $cacheManager->getCache(self::CACHE_IDENTIFIER);
190
            }
191
        }
192
193
        return $this->cacheInstance;
194
    }
195
196
    /**
197
     * @param FrontendInterface $cacheInstance
198
     */
199
    public function setCacheInstance(FrontendInterface $cacheInstance)
200
    {
201
        $this->cacheInstance = $cacheInstance;
202
    }
203
204
    /**
205
     * Generic cache identifier creation for usages in the extension.
206
     *
207
     * @param string $string
208
     * @param string $formClassName
209
     * @param int    $maxLength
210
     * @return string
211
     */
212
    public function getCacheIdentifier($string, $formClassName, $maxLength = 55)
213
    {
214
        $explodedClassName = explode('\\', $formClassName);
215
216
        $identifier = strtolower(
217
            $string .
218
            end($explodedClassName) .
219
            '-' .
220
            sha1($formClassName)
221
        );
222
223
        return substr($identifier, 0, $maxLength);
224
    }
225
226
    /**
227
     * Return the extension configuration.
228
     *
229
     * @param string $configurationName If null, returns the whole configuration. Otherwise, returns the asked configuration.
230
     * @return array
231
     */
232
    public function getExtensionConfiguration($configurationName = null)
233
    {
234
        if (null === $this->extensionConfiguration) {
235
            $this->extensionConfiguration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::EXTENSION_KEY]);
236
            if (false === $this->extensionConfiguration) {
237
                $this->extensionConfiguration = [];
238
            }
239
        }
240
241
        $result = null;
242
        if (null === $configurationName) {
243
            $result = $this->extensionConfiguration;
244
        } elseif (ArrayUtility::isValidPath($this->extensionConfiguration, $configurationName, '.')) {
245
            $result = ArrayUtility::getValueByPath($this->extensionConfiguration, $configurationName, '.');
246
        }
247
248
        return $result;
249
    }
250
251
    /**
252
     * Function called when clearing TYPO3 caches. It will remove the temporary
253
     * asset files created by Formz.
254
     *
255
     * @param array $parameters
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
     * Will check if the TypoScript was indeed included, as it contains required
307
     * configuration to make the forms work properly.
308
     *
309
     * @return bool
310
     */
311
    public function isTypoScriptIncluded()
312
    {
313
        return (null !== $this->getTypoScriptUtility()->getExtensionConfigurationFromPath('settings.typoScriptIncluded'));
314
    }
315
316
    /**
317
     * @return bool
318
     */
319
    public function isInDebugMode()
320
    {
321
        return (bool)$this->getExtensionConfiguration('debugMode');
322
    }
323
324
    /**
325
     * @param string|null $path If a string is given, it will be precessed by the extension relative path and returned.
326
     * @return string
327
     */
328
    public function getExtensionRelativePath($path = null)
329
    {
330
        $relativePath = ExtensionManagementUtility::siteRelPath('formz');
331
332
        return (null !== $path)
333
            ? $relativePath . $path
334
            : $relativePath;
335
    }
336
337
    /**
338
     * @return ObjectManagerInterface
339
     */
340
    public function getObjectManager()
341
    {
342
        return $this->objectManager;
343
    }
344
345
    /**
346
     * @param ObjectManagerInterface $objectManager
347
     */
348
    public function injectObjectManager(ObjectManagerInterface $objectManager)
349
    {
350
        $this->objectManager = $objectManager;
351
    }
352
353
    /**
354
     * @return TypoScriptUtility
355
     */
356
    public function getTypoScriptUtility()
357
    {
358
        return $this->typoScriptUtility;
359
    }
360
361
    /**
362
     * @param TypoScriptUtility $typoScriptUtility
363
     */
364
    public function injectTypoScriptUtility(TypoScriptUtility $typoScriptUtility)
365
    {
366
        $this->typoScriptUtility = $typoScriptUtility;
367
    }
368
369
    /**
370
     * @return ConfigurationFactory
371
     */
372
    public function getConfigurationFactory()
373
    {
374
        return $this->configurationFactory;
375
    }
376
377
    /**
378
     * @param ConfigurationFactory $configurationFactory
379
     */
380
    public function injectConfigurationFactory(ConfigurationFactory $configurationFactory)
381
    {
382
        $this->configurationFactory = $configurationFactory;
383
    }
384
385
    /**
386
     * @return FormObjectFactory
387
     */
388
    public function getFormObjectFactory()
389
    {
390
        return $this->formObjectFactory;
391
    }
392
393
    /**
394
     * @param FormObjectFactory $formObjectFactory
395
     */
396
    public function injectFormObjectFactory(FormObjectFactory $formObjectFactory)
397
    {
398
        $this->formObjectFactory = $formObjectFactory;
399
    }
400
401
    /**
402
     * Returns the extension key.
403
     *
404
     * @return string
405
     */
406
    public function getExtensionKey()
407
    {
408
        return self::EXTENSION_KEY;
409
    }
410
411
    /**
412
     * @return TypoScriptFrontendController
413
     */
414
    public function getPageController()
415
    {
416
        return $GLOBALS['TSFE'];
417
    }
418
419
    /**
420
     * @return BackendUserAuthentication
421
     */
422
    public function getBackendUser()
423
    {
424
        return $GLOBALS['BE_USER'];
425
    }
426
}
427