Completed
Pull Request — master (#24)
by Tymoteusz
07:15
created

Loader::initializeTypo3()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 48
ccs 0
cts 28
cp 0
rs 9.125
cc 3
eloc 27
nc 3
nop 0
crap 12
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
    /**
52
     * defines, if usage of backend-user is enabled
53
     *
54
     * @var boolean
55
     */
56
    private $isBackEndUserInitialized = false;
57
    /**
58
     * defines, if usage of frontend-user is enabled (this is needed, if the eID-script must determine the frontend-user)
59
     *
60
     * @var boolean
61
     */
62
    private $isFrontEndUserInitialized = false;
63
    /**
64
     * defines, if frontend-rendering is enabled (this is needed, if the eID-script must render some content-elements or RTE-fields)
65
     *
66
     * @var boolean
67
     */
68
    private $isFrontEndRenderingInitialized = false;
69
70
    /**
71
     * @return BackendUserAuthentication
72
     * @throws LogicException
73
     */
74
    public function getBackEndUser()
75
    {
76
        if ($this->isBackEndUserInitialized === false) {
77
            throw new LogicException('be-user is not initialized - initialize with BE-user with method \'initializeBackendEndUser\'');
78
        }
79
        return $GLOBALS['BE_USER'];
80
    }
81
82
    /**
83
     * @return FrontendUserAuthentication
84
     * @throws LogicException
85
     */
86
    public function getFrontEndUser()
87
    {
88
        if ($this->isFrontEndUserInitialized === false) {
89
            throw new LogicException('fe-user is not initialized - initialize with FE-user with method \'initializeFrontEndUser\'');
90
        }
91
        return $GLOBALS['TSFE']->fe_user;
92
    }
93
94
    /**
95
     * enable the usage of backend-user
96
     */
97
    public function initializeBackendEndUser()
98
    {
99
        if ($this->isBackEndUserInitialized === true) {
100
            return;
101
        }
102
103
        $bootstrapObj = Bootstrap::getInstance();
104
        $bootstrapObj->loadExtensionTables(true);
105
        $bootstrapObj->initializeBackendUser();
106
        $bootstrapObj->initializeBackendAuthentication();
107
        $bootstrapObj->initializeBackendUserMounts();
108
        $bootstrapObj->initializeLanguageObject();
109
110
        $this->isBackEndUserInitialized = true;
111
    }
112
113
    /**
114
     * enable the usage of frontend-user
115
     *
116
     * @param integer $pageId
117
     */
118
    public function initializeFrontEndUser($pageId = 0)
119
    {
120
        if (array_key_exists('TSFE', $GLOBALS) && is_object($GLOBALS['TSFE']->fe_user)) {
121
            // FE-user is already initialized - this can happen when we use/call internal REST-endpoints inside of a normal TYPO3-page
122
            $this->isFrontEndUserInitialized = true;
123
        }
124
        if ($this->isFrontEndUserInitialized === true) {
125
            return;
126
        }
127
128
        $tsfe = $this->getTsfe($pageId);
129
        $tsfe->initFEUser();
130
        $this->isFrontEndUserInitialized = true;
131
    }
132
133
    /**
134
     * enable the frontend-rendering
135
     *
136
     * @param integer $pageId
137
     */
138
    public function initializeFrontEndRendering($pageId = 0)
139
    {
140
        if (array_key_exists('TSFE', $GLOBALS) && is_object($GLOBALS['TSFE']->tmpl)) {
141
            // FE is already initialized - this can happen when we use/call internal REST-endpoints inside of a normal TYPO3-page
142
            $this->isFrontEndRenderingInitialized = true;
143
        }
144
        if ($this->isFrontEndRenderingInitialized === true) {
145
            return;
146
        }
147
148
        if ($this->isFrontEndUserInitialized === false) {
149
            $this->initializeFrontEndUser($pageId);
150
        }
151
152
        EidUtility::initTCA();
153
154
        $tsfe = $this->getTsfe($pageId);
155
        $tsfe->determineId();
156
        $tsfe->initTemplate();
157
        $tsfe->getConfigArray();
158
        $tsfe->newCObj();
159
        $tsfe->calculateLinkVars();
160
        $this->isFrontEndRenderingInitialized = true;
161
    }
162
163
    /**
164
     * enable the usage of TYPO3 - start TYPO3 by calling the TYPO3-bootstrap
165
     */
166
    public function initializeTypo3()
167
    {
168
        // we must define this constant, otherwise some TYPO3-extensions will not work!
169
        define('TYPO3_MODE', 'FE');
170
        // we define this constant, so that any TYPO3-Extension can check, if the REST-API is running
171
        define('REST_API_IS_RUNNING', true);
172
        // configure TYPO3 (e.g. paths, variables and classLoader)
173
        $bootstrapObj = Bootstrap::getInstance();
174
        if (true === method_exists($bootstrapObj, 'applyAdditionalConfigurationSettings')) {
175
            // it seams to be TYPO3 6.2 (LTS)
176
            $bootstrapObj->baseSetup('typo3conf/ext/restler/Scripts/'); // server has called script 'restler/Scripts/restler_dispatch.php'
177
            $bootstrapObj->startOutputBuffering();
178
            $bootstrapObj->loadConfigurationAndInitialize();
179
180
            // configure TYPO3 (load ext_localconf.php-files of TYPO3-extensions)
181
            $this->getExtensionManagementUtility()->loadExtLocalconf();
182
183
            // configure TYPO3 (Database and further settings)
184
            $bootstrapObj->applyAdditionalConfigurationSettings();
185
            $bootstrapObj->initializeTypo3DbGlobal();
186
        } else {
187
                // it seams to be TYPO3 >= 7.6 (LTS)
188
                $classLoader = require $this->getClassLoader();
189
                $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...
190
191
            if (!method_exists($bootstrapObj, 'loadCachedTca')) {
192
                // it seams to be TYPO3 > 8.7 (LTS)
193
                $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...
194
                $bootstrapObj->baseSetup(4);
195
            } else {
196
                $bootstrapObj->baseSetup('typo3conf/ext/restler/Scripts/'); // server has called script 'restler/Scripts/restler_dispatch.php'
197
            }
198
                $bootstrapObj->startOutputBuffering();
199
                $bootstrapObj->loadConfigurationAndInitialize();
200
201
                // configure TYPO3 (load ext_localconf.php-files of TYPO3-extensions)
202
                $this->getExtensionManagementUtility()->loadExtLocalconf();
203
204
                // configure TYPO3 (Database and further settings)
205
                $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...
206
                $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...
207
                $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...
208
                $bootstrapObj->initializeTypo3DbGlobal();
209
        }
210
211
        // create timeTracker-object (TYPO3 needs that)
212
        $GLOBALS['TT'] = new NullTimeTracker();
213
    }
214
215
    /**
216
     * Resolve the class loader file.
217
     *
218
     * @return string
219
     * @throws RuntimeException
220
     */
221
    private function getClassLoader()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
222
    {
223
        $possibleClassLoader1 = __DIR__ . '/../../../../../../typo3_src/vendor/autoload.php';
224
        $possibleClassLoader2 = __DIR__ . '/../../../../../../../vendor/typo3/cms/vendor/autoload.php';
225
        $possibleClassLoader3 = __DIR__ . '/../../../../../../Packages/Libraries/typo3/cms/vendor/autoload.php';
226
227
        if (is_file($possibleClassLoader1)) {
228
            return $possibleClassLoader1;
229
        }
230
        if (is_file($possibleClassLoader2)) {
231
            return $possibleClassLoader2;
232
        }
233
        if (is_file($possibleClassLoader3)) {
234
            return $possibleClassLoader3;
235
        }
236
        throw new RuntimeException('TNM: I could not find a valid autoload file.', 1458829787);
237
    }
238
239
    /**
240
     * create instance of ExtensionManagementUtility
241
     *
242
     * Attention:
243
     * Don't use the dependency-injection of TYPO3, because at this time (where we initialize TYPO3), the DI is not available!
244
     *
245
     * @return ExtensionManagementUtility
246
     */
247
    private function getExtensionManagementUtility()
248
    {
249
        $cacheManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager');
250
        $extensionConfiguration = GeneralUtility::makeInstance('Aoe\\Restler\\Configuration\\ExtensionConfiguration');
251
        return new ExtensionManagementUtility($cacheManager, $extensionConfiguration);
252
    }
253
254
    /**
255
     * @param integer $pageId
256
     * @return TypoScriptFrontendController
257
     */
258
    private function getTsfe($pageId)
259
    {
260
        if (false === array_key_exists('TSFE', $GLOBALS)) {
261
            $GLOBALS['TSFE'] = GeneralUtility::makeInstance(
262
                'TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController',
263
                $GLOBALS['TYPO3_CONF_VARS'],
264
                $pageId,
265
                0
266
            );
267
        }
268
        return $GLOBALS['TSFE'];
269
    }
270
}
271