Completed
Push — cleanup-service ( 60a2d8...0f35f3 )
by Romain
02:18
created

Core::getExtensionRelativePath()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Core::injectEnvironmentService() 0 4 1
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\Core;
15
16
use Romm\Formz\Service\ExtensionService;
17
use Romm\Formz\Service\TypoScriptService;
18
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19
use TYPO3\CMS\Core\SingletonInterface;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Extbase\Object\ObjectManager;
22
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
23
use TYPO3\CMS\Extbase\Service\EnvironmentService;
24
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
25
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
26
27
/**
28
 * Class containing general functions.
29
 */
30
class Core implements SingletonInterface
31
{
32
    /**
33
     * @var Core
34
     */
35
    protected static $instance;
36
37
    /**
38
     * @var ObjectManagerInterface
39
     */
40
    protected $objectManager;
41
42
    /**
43
     * @var TypoScriptService
44
     */
45
    protected $typoScriptService;
46
47
    /**
48
     * @var EnvironmentService
49
     */
50
    protected $environmentService;
51
52
    /**
53
     * @return Core
54
     */
55
    public static function get()
56
    {
57
        if (null === self::$instance) {
58
            /** @var ObjectManager $objectManager */
59
            $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
60
61
            self::$instance = $objectManager->get(self::class);
62
        }
63
64
        return self::$instance;
65
    }
66
67
    /**
68
     * Translation handler. Does the same job as Extbase translation tools,
69
     * expect that if the index to the LLL reference is not found, the index is
70
     * returned (Extbase would have returned an empty string).
71
     *
72
     * @param    string $index        The index to the LLL reference.
73
     * @param    string $extensionKey Key of the extension containing the LLL reference.
74
     * @param    array  $arguments    Arguments passed over to vsprintf.
75
     * @return   string               The translated string.
76
     */
77
    public function translate($index, $extensionKey = null, $arguments = null)
78
    {
79
        $extensionKey = ($extensionKey) ?: ExtensionService::get()->getExtensionKey();
80
        $result = LocalizationUtility::translate($index, $extensionKey, $arguments);
81
        if ($result === '' && $index !== '') {
82
            $result = $index;
83
        }
84
85
        return $result;
86
    }
87
88
    /**
89
     * Converts an array to a clean JSON string which can be used by JavaScript.
90
     *
91
     * @param array $array
92
     * @return string
93
     */
94
    public function arrayToJavaScriptJson(array $array)
95
    {
96
        return json_encode($array, JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_TAG);
97
    }
98
99
    /**
100
     * Sanitizes a string: lower case with dash separation.
101
     *
102
     * @param string $string
103
     * @return string
104
     */
105
    public function sanitizeString($string)
106
    {
107
        $string = str_replace('_', '-', GeneralUtility::camelCaseToLowerCaseUnderscored($string));
108
109
        while (strpos($string, '--')) {
110
            $string = str_replace('--', '-', $string);
111
        }
112
113
        return $string;
114
    }
115
116
    /**
117
     * Shortcut for object manager `get()` function.
118
     *
119
     * @param string $className
120
     * @return object
121
     */
122
    public static function instantiate($className)
123
    {
124
        $objectManager = self::get()->getObjectManager();
125
126
        return call_user_func_array([$objectManager, 'get'], func_get_args());
127
    }
128
129
    /**
130
     * @return ObjectManagerInterface
131
     */
132
    public function getObjectManager()
133
    {
134
        return $this->objectManager;
135
    }
136
137
    /**
138
     * @param ObjectManagerInterface $objectManager
139
     */
140
    public function injectObjectManager(ObjectManagerInterface $objectManager)
141
    {
142
        $this->objectManager = $objectManager;
143
    }
144
145
    /**
146
     * @param TypoScriptService $typoScriptService
147
     */
148
    public function injectTypoScriptService(TypoScriptService $typoScriptService)
149
    {
150
        $this->typoScriptService = $typoScriptService;
151
    }
152
153
    /**
154
     * @param EnvironmentService $environmentService
155
     */
156
    public function injectEnvironmentService(EnvironmentService $environmentService)
157
    {
158
        $this->environmentService = $environmentService;
159
    }
160
161
    /**
162
     * @return TypoScriptFrontendController
163
     */
164
    public function getPageController()
165
    {
166
        return $GLOBALS['TSFE'];
167
    }
168
169
    /**
170
     * @return BackendUserAuthentication
171
     */
172
    public function getBackendUser()
173
    {
174
        return $GLOBALS['BE_USER'];
175
    }
176
}
177