Completed
Pull Request — master (#8)
by Fabien
08:26
created

Loader::initializeFrontEndRendering()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 18
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
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 RuntimeException;
29
use TYPO3\CMS\Core\Core\Bootstrap;
30
use TYPO3\CMS\Core\SingletonInterface;
31
use TYPO3\CMS\Core\TimeTracker\NullTimeTracker;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
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
38
// @codingStandardsIgnoreStart
39
// we must load some PHP/TYPO3-classes manually, because at this point, TYPO3 (and it's auto-loading) is not initialized
40
require_once __DIR__ . '/../../../../../../typo3/sysext/core/Classes/Core/ApplicationContext.php';
41
require_once __DIR__ . '/../../../../../../typo3/sysext/core/Classes/Core/Bootstrap.php';
42
require_once __DIR__ . '/../../../../../../typo3/sysext/core/Classes/SingletonInterface.php';
43
// @codingStandardsIgnoreEnd
44
45
/**
46
 * @package Restler
47
 */
48
class Loader implements SingletonInterface
49
{
50
    /**
51
     * defines, if frontend-rendering is enabled (this is needed, if the eID-script must render some content-elements or RTE-fields)
52
     *
53
     * @var boolean
54
     */
55
    private $isFrontEndRenderingInitialized = false;
56
    /**
57
     * defines, if usage of frontend-user is enabled (this is needed, if the eID-script must determine the frontend-user)
58
     *
59
     * @var boolean
60
     */
61
    private $isFrontEndUserInitialized = false;
62
63
    /**
64
     * @return FrontendUserAuthentication
65
     * @throws LogicException
66
     */
67
    public function getFrontEndUser()
68
    {
69
        if ($this->isFrontEndUserInitialized === false) {
70
            throw new LogicException('fe-user is not initialized - initialize with FE-user with method \'initializeFrontEndUser\'');
71
        }
72
        return $GLOBALS['TSFE']->fe_user;
73
    }
74
75
    /**
76
     * enable the frontend-rendering
77
     *
78
     * @param integer $pageId
79
     */
80
    public function initializeFrontEndRendering($pageId = 0)
81
    {
82
        if ($this->isFrontEndRenderingInitialized === true) {
83
            return;
84
        }
85
86
        if ($this->isFrontEndUserInitialized === false) {
87
            $this->initializeFrontEndUser($pageId);
88
        }
89
90
        EidUtility::initTCA();
91
92
        $tsfe = $this->getTsfe($pageId);
93
        $tsfe->determineId();
94
        $tsfe->initTemplate();
95
        $tsfe->getConfigArray();
96
        $this->isFrontEndRenderingInitialized = true;
97
    }
98
99
    /**
100
     * enable the usage of frontend-user
101
     *
102
     * @param integer $pageId
103
     */
104
    public function initializeFrontEndUser($pageId = 0)
105
    {
106
        if ($this->isFrontEndUserInitialized === true) {
107
            return;
108
        }
109
110
        $tsfe = $this->getTsfe($pageId);
111
        $tsfe->initFEUser();
112
        $this->isFrontEndUserInitialized = true;
113
    }
114
115
    /**
116
     * enable the usage of TYPO3 - start TYPO3 by calling the TYPO3-bootstrap
117
     */
118
    public function initializeTypo3()
119
    {
120
        // we must define this constant, otherwise some TYPO3-extensions will not work!
121
        define('TYPO3_MODE', 'FE');
122
        // we define this constant, so that any TYPO3-Extension can check, if the REST-API is running
123
        define('REST_API_IS_RUNNING', true);
124
        // configure TYPO3 (e.g. paths, variables and classLoader)
125
        $bootstrapObj = Bootstrap::getInstance();
126
        if (true === method_exists($bootstrapObj, 'applyAdditionalConfigurationSettings')) {
127
            // it seams to be TYPO3 6.2 (LTS)
128
            $bootstrapObj->baseSetup('typo3conf/ext/restler/Scripts/'); // server has called script 'restler/Scripts/restler_dispatch.php'
129
            $bootstrapObj->startOutputBuffering();
130
            $bootstrapObj->loadConfigurationAndInitialize();
131
132
            // configure TYPO3 (load ext_localconf.php-files of TYPO3-extensions)
133
            $this->getExtensionManagementUtility()->loadExtLocalconf();
134
135
            // configure TYPO3 (Database and further settings)
136
            $bootstrapObj->applyAdditionalConfigurationSettings();
137
            $bootstrapObj->initializeTypo3DbGlobal();
138
        } else {
139
            // it seams to be TYPO3 7.6 (LTS)
140
            $classLoader = require $this->getClassLoader();
141
142
            $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...
143
            $bootstrapObj->baseSetup('typo3conf/ext/restler/Scripts/'); // server has called script 'restler/Scripts/restler_dispatch.php'
144
            $bootstrapObj->startOutputBuffering();
145
            $bootstrapObj->loadConfigurationAndInitialize();
146
147
            // configure TYPO3 (load ext_localconf.php-files of TYPO3-extensions)
148
            $this->getExtensionManagementUtility()->loadExtLocalconf();
149
150
            // configure TYPO3 (Database and further settings)
151
            $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...
152
            $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...
153
            $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...
154
            $bootstrapObj->initializeTypo3DbGlobal();
155
        }
156
157
        // create timeTracker-object (TYPO3 needs that)
158
        $GLOBALS['TT'] = new NullTimeTracker();
159
    }
160
161
    /**
162
     * Resolve the class loader file.
163
     *
164
     * @return string
165
     * @throws \TYPO3\CMS\Core\Exception
166
     */
167
    private function getClassLoader()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
168
    {
169
        $possibleClassLoader1 = __DIR__ . '/../../../../../../typo3_src/vendor/autoload.php';
170
        $possibleClassLoader2 = __DIR__ . '/../../../../../../../vendor/typo3/cms/vendor/autoload.php';
171
172
        if (is_file($possibleClassLoader1)) {
173
            $classLoaderFile = $possibleClassLoader1;
174
        } elseif (is_file($possibleClassLoader2)) {
175
            $classLoaderFile = $possibleClassLoader2;
176
        } else {
177
            throw new RuntimeException('I could not find a valid autoload file.', 1458829787);
178
        }
179
180
        return $classLoaderFile;
181
    }
182
183
    /**
184
     * create instance of ExtensionManagementUtility
185
     *
186
     * Attention:
187
     * Don't use the dependency-injection of TYPO3, because at this time (where we initialize TYPO3), the DI is not available!
188
     *
189
     * @return ExtensionManagementUtility
190
     */
191
    private function getExtensionManagementUtility()
192
    {
193
        $cacheManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager');
194
        $extensionConfiguration = GeneralUtility::makeInstance('Aoe\\Restler\\Configuration\\ExtensionConfiguration');
195
        return new ExtensionManagementUtility($cacheManager, $extensionConfiguration);
196
    }
197
198
    /**
199
     * @param integer $pageId
200
     * @return TypoScriptFrontendController
201
     */
202
    private function getTsfe($pageId)
203
    {
204
        if (false === array_key_exists('TSFE', $GLOBALS)) {
205
            $GLOBALS['TSFE'] = GeneralUtility::makeInstance(
206
                'TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController',
207
                $GLOBALS['TYPO3_CONF_VARS'],
208
                $pageId,
209
                0
210
            );
211
        }
212
        return $GLOBALS['TSFE'];
213
    }
214
}
215