Completed
Push — master ( a29f3c...3272d9 )
by
unknown
06:21
created

Loader::initializeTypo3()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 53
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 53
ccs 0
cts 33
cp 0
rs 8.7155
cc 6
eloc 28
nc 10
nop 0
crap 42

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Aoe\Restler\System\TYPO3;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2015 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use TYPO3\CMS\Core\Core\Bootstrap;
29
use TYPO3\CMS\Core\SingletonInterface;
30
use TYPO3\CMS\Core\TimeTracker\NullTimeTracker;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
33
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
34
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
35
use TYPO3\CMS\Frontend\Utility\EidUtility;
36
use LogicException;
37
use RuntimeException;
38
39
// @codingStandardsIgnoreStart
40
// we must load some PHP/TYPO3-classes manually, because at this point, TYPO3 (and it's auto-loading) is not initialized
41
require_once __DIR__ . '/../../../../../../typo3/sysext/core/Classes/Core/ApplicationContext.php';
42
require_once __DIR__ . '/../../../../../../typo3/sysext/core/Classes/Core/Bootstrap.php';
43
require_once __DIR__ . '/../../../../../../typo3/sysext/core/Classes/SingletonInterface.php';
44
// @codingStandardsIgnoreEnd
45
46
/**
47
 * @package Restler
48
 */
49
class Loader implements SingletonInterface
50
{
51
    const TYPO3_VERSION_6LTS = 'TYPO3_VERSION_6LTS';
52
    const TYPO3_VERSION_7LTS = 'TYPO3_VERSION_7LTS';
53
    const TYPO3_VERSION_8LTS = 'TYPO3_VERSION_8LTS';
54
55
    /**
56
     * defines, if usage of backend-user is enabled
57
     *
58
     * @var boolean
59
     */
60
    private $isBackEndUserInitialized = false;
61
    /**
62
     * defines, if usage of frontend-user is enabled (this is needed, if the eID-script must determine the frontend-user)
63
     *
64
     * @var boolean
65
     */
66
    private $isFrontEndUserInitialized = false;
67
    /**
68
     * defines, if frontend-rendering is enabled (this is needed, if the eID-script must render some content-elements or RTE-fields)
69
     *
70
     * @var boolean
71
     */
72
    private $isFrontEndRenderingInitialized = false;
73
74
    /**
75
     * @return BackendUserAuthentication
76
     * @throws LogicException
77
     */
78
    public function getBackEndUser()
79
    {
80
        if ($this->isBackEndUserInitialized === false) {
81
            throw new LogicException('be-user is not initialized - initialize with BE-user with method \'initializeBackendEndUser\'');
82
        }
83
        return $GLOBALS['BE_USER'];
84
    }
85
86
    /**
87
     * @return FrontendUserAuthentication
88
     * @throws LogicException
89
     */
90
    public function getFrontEndUser()
91
    {
92
        if ($this->isFrontEndUserInitialized === false) {
93
            throw new LogicException('fe-user is not initialized - initialize with FE-user with method \'initializeFrontEndUser\'');
94
        }
95
        return $GLOBALS['TSFE']->fe_user;
96
    }
97
98
    /**
99
     * enable the usage of backend-user
100
     */
101
    public function initializeBackendEndUser()
102
    {
103
        if ($this->isBackEndUserInitialized === true) {
104
            return;
105
        }
106
107
        $bootstrapObj = Bootstrap::getInstance();
108
        $bootstrapObj->loadExtensionTables(true);
109
        $bootstrapObj->initializeBackendUser();
110
        $bootstrapObj->initializeBackendAuthentication();
111
        $bootstrapObj->initializeBackendUserMounts();
112
        $bootstrapObj->initializeLanguageObject();
113
114
        $this->isBackEndUserInitialized = true;
115
    }
116
117
    /**
118
     * enable the usage of frontend-user
119
     *
120
     * @param integer $pageId
121
     */
122
    public function initializeFrontEndUser($pageId = 0)
123
    {
124
        if (array_key_exists('TSFE', $GLOBALS) && is_object($GLOBALS['TSFE']->fe_user)) {
125
            // FE-user is already initialized - this can happen when we use/call internal REST-endpoints inside of a normal TYPO3-page
126
            $this->isFrontEndUserInitialized = true;
127
        }
128
        if ($this->isFrontEndUserInitialized === true) {
129
            return;
130
        }
131
132
        $tsfe = $this->getTsfe($pageId);
133
        $tsfe->initFEUser();
134
        $this->isFrontEndUserInitialized = true;
135
    }
136
137
    /**
138
     * enable the frontend-rendering
139
     *
140
     * @param integer $pageId
141
     */
142
    public function initializeFrontEndRendering($pageId = 0)
143
    {
144
        if (array_key_exists('TSFE', $GLOBALS) && is_object($GLOBALS['TSFE']->tmpl)) {
145
            // FE is already initialized - this can happen when we use/call internal REST-endpoints inside of a normal TYPO3-page
146
            $this->isFrontEndRenderingInitialized = true;
147
        }
148
        if ($this->isFrontEndRenderingInitialized === true) {
149
            return;
150
        }
151
152
        if ($this->isFrontEndUserInitialized === false) {
153
            $this->initializeFrontEndUser($pageId);
154
        }
155
156
        EidUtility::initTCA();
157
158
        $tsfe = $this->getTsfe($pageId);
159
        $tsfe->determineId();
160
        $tsfe->initTemplate();
161
        $tsfe->getConfigArray();
162
        $tsfe->newCObj();
163
        $tsfe->calculateLinkVars();
164
        $this->isFrontEndRenderingInitialized = true;
165
    }
166
167
    /**
168
     * enable the usage of TYPO3 - start TYPO3 by calling the TYPO3-bootstrap
169
     */
170
    public function initializeTypo3()
171
    {
172
        // we must define this constant, otherwise some TYPO3-extensions will not work!
173
        define('TYPO3_MODE', 'FE');
174
        // we define this constant, so that any TYPO3-Extension can check, if the REST-API is running
175
        define('REST_API_IS_RUNNING', true);
176
177
        // configure TYPO3 (e.g. paths, variables and classLoader)
178
        $bootstrapObj = Bootstrap::getInstance();
179
        $typo3Version = $this->determineTypo3Version($bootstrapObj);
180
181
        if ($typo3Version === self::TYPO3_VERSION_6LTS) {
182
            $bootstrapObj->baseSetup('typo3conf/ext/restler/Scripts/'); // server has called script 'restler/Scripts/restler_dispatch.php'
183
            $bootstrapObj->startOutputBuffering();
184
            $bootstrapObj->loadConfigurationAndInitialize();
185
186
            // configure TYPO3 (load ext_localconf.php-files of TYPO3-extensions)
187
            $this->getExtensionManagementUtility()->loadExtLocalconf();
188
189
            // configure TYPO3 (Database and further settings)
190
            $bootstrapObj->applyAdditionalConfigurationSettings();
191
            $bootstrapObj->initializeTypo3DbGlobal();
192
        }
193
194
        if ($typo3Version === self::TYPO3_VERSION_7LTS || $typo3Version === self::TYPO3_VERSION_8LTS) {
195
            $classLoader = require $this->getClassLoader();
196
            $bootstrapObj->initializeClassLoader($classLoader);
0 ignored issues
show
Unused Code introduced by
The call to Bootstrap::initializeClassLoader() has too many arguments starting with $classLoader.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
197
198
            if ($typo3Version === self::TYPO3_VERSION_7LTS) {
199
                // server has called script 'restler/Scripts/restler_dispatch.php'
200
                $bootstrapObj->baseSetup('typo3conf/ext/restler/Scripts/');
201
            }
202
            if ($typo3Version === self::TYPO3_VERSION_8LTS) {
203
                $bootstrapObj->setRequestType(TYPO3_REQUESTTYPE_FE);
0 ignored issues
show
Bug introduced by
The method setRequestType() does not seem to exist on object<TYPO3\CMS\Core\Core\Bootstrap>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
204
                $bootstrapObj->baseSetup(4);
205
            }
206
207
            $bootstrapObj->startOutputBuffering();
208
            $bootstrapObj->loadConfigurationAndInitialize();
209
210
            // configure TYPO3 (load ext_localconf.php-files of TYPO3-extensions)
211
            $this->getExtensionManagementUtility()->loadExtLocalconf();
212
213
            // configure TYPO3 (Database and further settings)
214
            $bootstrapObj->setFinalCachingFrameworkCacheConfiguration();
0 ignored issues
show
Bug introduced by
The method setFinalCachingFrameworkCacheConfiguration() cannot be called from this context as it is declared protected in class TYPO3\CMS\Core\Core\Bootstrap.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
215
            $bootstrapObj->defineLoggingAndExceptionConstants();
0 ignored issues
show
Bug introduced by
The method defineLoggingAndExceptionConstants() cannot be called from this context as it is declared protected in class TYPO3\CMS\Core\Core\Bootstrap.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
216
            $bootstrapObj->unsetReservedGlobalVariables();
0 ignored issues
show
Bug introduced by
The method unsetReservedGlobalVariables() cannot be called from this context as it is declared protected in class TYPO3\CMS\Core\Core\Bootstrap.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
217
            $bootstrapObj->initializeTypo3DbGlobal();
218
        }
219
220
        // create timeTracker-object (TYPO3 needs that)
221
        $GLOBALS['TT'] = new NullTimeTracker();
222
    }
223
224
    /**
225
     * @param Bootstrap $bootstrapObj
226
     * @return boolean
227
     */
228
    private function determineTypo3Version(Bootstrap $bootstrapObj)
229
    {
230
        if (true === method_exists($bootstrapObj, 'applyAdditionalConfigurationSettings')) {
231
            // it seams to be TYPO3 6.2 (LTS)
232
            return self::TYPO3_VERSION_6LTS;
233
        }
234
        if (false === method_exists($bootstrapObj, 'loadCachedTca')) {
235
            // it seams to be TYPO3 > 8.7 (LTS)
236
            return self::TYPO3_VERSION_8LTS;
237
        }
238
239
        // it seams to be TYPO3 7.6 (LTS)
240
        return self::TYPO3_VERSION_7LTS;
241
    }
242
243
    /**
244
     * Resolve the class loader file.
245
     *
246
     * @return string
247
     * @throws RuntimeException
248
     */
249
    private function getClassLoader()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
250
    {
251
        $possibleClassLoader1 = __DIR__ . '/../../../../../../typo3_src/vendor/autoload.php';
252
        $possibleClassLoader2 = __DIR__ . '/../../../../../../../vendor/typo3/cms/vendor/autoload.php';
253
        $possibleClassLoader3 = __DIR__ . '/../../../../../../Packages/Libraries/typo3/cms/vendor/autoload.php';
254
255
        if (is_file($possibleClassLoader1)) {
256
            return $possibleClassLoader1;
257
        }
258
        if (is_file($possibleClassLoader2)) {
259
            return $possibleClassLoader2;
260
        }
261
        if (is_file($possibleClassLoader3)) {
262
            return $possibleClassLoader3;
263
        }
264
        throw new RuntimeException('TNM: I could not find a valid autoload file.', 1458829787);
265
    }
266
267
    /**
268
     * create instance of ExtensionManagementUtility
269
     *
270
     * Attention:
271
     * Don't use the dependency-injection of TYPO3, because at this time (where we initialize TYPO3), the DI is not available!
272
     *
273
     * @return ExtensionManagementUtility
274
     */
275
    private function getExtensionManagementUtility()
276
    {
277
        $cacheManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager');
278
        $extensionConfiguration = GeneralUtility::makeInstance('Aoe\\Restler\\Configuration\\ExtensionConfiguration');
279
        return new ExtensionManagementUtility($cacheManager, $extensionConfiguration);
280
    }
281
282
    /**
283
     * @param integer $pageId
284
     * @return TypoScriptFrontendController
285
     */
286
    private function getTsfe($pageId)
287
    {
288
        if (false === array_key_exists('TSFE', $GLOBALS)) {
289
            $GLOBALS['TSFE'] = GeneralUtility::makeInstance(
290
                'TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController',
291
                $GLOBALS['TYPO3_CONF_VARS'],
292
                $pageId,
293
                0
294
            );
295
        }
296
        return $GLOBALS['TSFE'];
297
    }
298
}
299