Passed
Push — main ( 9da72c...4c09d2 )
by Felix
20:30 queued 11:53
created

Builder::getCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
namespace Aoe\Restler\System\Restler;
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 Aoe\Restler\Configuration\ExtensionConfiguration;
29
use Aoe\Restler\System\TYPO3\Cache;
30
use InvalidArgumentException;
31
use Luracast\Restler\Defaults;
32
use Luracast\Restler\Scope;
33
use Psr\Http\Message\ServerRequestInterface;
34
use TYPO3\CMS\Core\Cache\Backend\SimpleFileBackend;
35
use TYPO3\CMS\Core\Cache\CacheManager;
36
use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException;
37
use TYPO3\CMS\Core\Core\Environment;
38
use TYPO3\CMS\Core\SingletonInterface;
39
use TYPO3\CMS\Core\Utility\GeneralUtility;
40
use TYPO3\CMS\Extbase\Object\ObjectManager;
41
42
/**
43
 * @package Restler
44
 */
45
class Builder implements SingletonInterface
46
{
47
    /**
48
     * @var ExtensionConfiguration
49
     */
50
    private $extensionConfiguration;
51
52
    /**
53
     * @var CacheManager
54
     */
55
    private $cacheManager;
56
57
    /**
58
     * @param ExtensionConfiguration $extensionConfiguration
59
     * @param CacheManager $cacheManager
60
     */
61 10
    public function __construct(
62
        ExtensionConfiguration $extensionConfiguration = null,
63
        CacheManager $cacheManager = null
64
    ) {
65 10
        $this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ObjectManager::class)
66
                ->get(ExtensionConfiguration::class);
67
68 10
        $this->cacheManager = $cacheManager ?? GeneralUtility::makeInstance(ObjectManager::class)->get(CacheManager::class);
69 10
    }
70
71
    /**
72
     * initialize and configure restler-framework and return restler-object
73
     *
74
     * @return RestlerExtended
75
     */
76
    public function build(ServerRequestInterface $request = null)
77
    {
78
        $this->setAutoLoading();
79
        $this->setCacheDirectory();
80
        $this->setServerConfiguration();
81
82
        $restlerObj = $this->createRestlerObject($request);
83
        $this->configureRestler($restlerObj);
84
        $this->addApiClassesByGlobalArray($restlerObj);
85
        return $restlerObj;
86
    }
87
88
    /**
89
     * @return RestlerExtended
90
     */
91 1
    protected function createRestlerObject(ServerRequestInterface $request = null)
92
    {
93 1
        return new RestlerExtended(
94 1
            GeneralUtility::makeInstance(ObjectManager::class)->get(Cache::class),
95 1
            $this->extensionConfiguration->isProductionContextSet(),
96 1
            $this->extensionConfiguration->isCacheRefreshingEnabled(),
97 1
            $request
98
        );
99
    }
100
101
    /**
102
     * Call all classes, which implements the interface 'Aoe\Restler\System\Restler\ConfigurationInterface'.
103
     * Those classes includes further restler-configurations, e.g.:
104
     *  - add API-classes
105
     *  - add authentication-classes
106
     *  - configure/set properties of several classes inside the restler-framework
107
     *  - configure overwriting of several classes inside the restler-framework
108
     *
109
     * @param RestlerExtended $restler
110
     * @throws InvalidArgumentException
111
     */
112 5
    private function configureRestler(RestlerExtended $restler)
113
    {
114 5
        $restlerConfigurationClasses = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['restler']['restlerConfigurationClasses'];
115
116 5
        if (false === is_array($restlerConfigurationClasses) || count($restlerConfigurationClasses) === 0) {
117 2
            $message = 'No restler-configuration-class found (at least one restler-configuration-class is required)! ';
118 2
            $message .= 'The configuration-class must be registered in ext_localconf.php of your TYPO3-extension like this: ';
119 2
            $message .= '$GLOBALS[\'TYPO3_CONF_VARS\'][\'SC_OPTIONS\'][\'restler\'][\'restlerConfigurationClasses\'][] =
120
                \'[YourConfigurationClass]\';';
121 2
            $message .= 'The configuration-class must implement this interface: Aoe\Restler\System\Restler\ConfigurationInterface';
122 2
            throw new InvalidArgumentException($message, 1428562059);
123
        }
124
125
        // append configuration classes from external GLOBAL registration
126 3
        if (is_array($GLOBALS['TYPO3_Restler']['restlerConfigurationClasses'])) {
127 1
            $externalRestlerConfigurationClasses = array_unique($GLOBALS['TYPO3_Restler']['restlerConfigurationClasses']);
128 1
            $restlerConfigurationClasses = array_merge(
129 1
                $restlerConfigurationClasses,
130 1
                $externalRestlerConfigurationClasses
131
            );
132
        }
133
134 3
        foreach ($restlerConfigurationClasses as $restlerConfigurationClass) {
135 3
            $configurationObj = GeneralUtility::makeInstance(ObjectManager::class)->get($restlerConfigurationClass);
136
137
            /* @var $configurationObj ConfigurationInterface */
138 3
            if (false === $configurationObj instanceof ConfigurationInterface) {
139 1
                $message = 'class "' . $restlerConfigurationClass . '" did not implement the ';
140 1
                $message .= 'interface "Aoe\Restler\System\Restler\ConfigurationInterface"!';
141 1
                throw new InvalidArgumentException($message, 1428562081);
142
            }
143
144 2
            $configurationObj->configureRestler($restler);
145
        }
146 2
    }
147
148
    /**
149
     * Add API-Controller-Classes that are registered by global array
150
     *
151
     * @param RestlerExtended $restler
152
     */
153 1
    private function addApiClassesByGlobalArray(RestlerExtended $restler)
154
    {
155 1
        $addApiController = $GLOBALS['TYPO3_Restler']['addApiClass'];
156 1
        if (is_array($addApiController)) {
157 1
            foreach ($addApiController as $apiEndpoint => $apiControllers) {
158 1
                $uniqueApiControllers = array_unique($apiControllers);
159 1
                foreach ($uniqueApiControllers as $apiController) {
160 1
                    $restler->addAPIClass($apiController, $apiEndpoint);
161
                }
162
            }
163
        }
164 1
    }
165
166
    /**
167
     * use auto-loading for PHP-classes of restler-framework and extBase/TYPO3 (use dependency-injection of extBase)
168
     */
169 1
    private function setAutoLoading()
170
    {
171
        // set auto-loading for restler
172 1
        if (class_exists('\TYPO3\CMS\Core\Core\Environment')) {
173 1
            $autoload = Environment::getPublicPath() . 'typo3conf/ext/restler/vendor/autoload.php';
174
        } else {
175
            $autoload = PATH_site . 'typo3conf/ext/restler/vendor/autoload.php';
176
        }
177
178 1
        if (file_exists($autoload)) {
179
            require_once $autoload;
180
        }
181
182
        // set auto-loading for extBase/TYPO3-classes
183
        Scope::$resolver = function ($className) {
184 1
            return GeneralUtility::makeInstance(ObjectManager::class)->get($className);
185 1
        };
186 1
    }
187
188
    /**
189
     * configure cache-directory (where restler can write cache-files)
190
     */
191 1
    private function setCacheDirectory()
192
    {
193 1
        Defaults::$cacheDirectory = $this->getCache()->getCacheDirectory();
194 1
    }
195
196
    /**
197
     * fix server-port (if not correct set)
198
     */
199 1
    private function setServerConfiguration()
200
    {
201 1
        if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' && $_SERVER['SERVER_PORT'] === '80') {
202
            // Fix port for HTTPS
203
            // Otherwise restler will create those urls for online-documentation, when HTTPS is used: https://www.example.com:80
204 1
            $_SERVER['SERVER_PORT'] = '443';
205
        }
206 1
    }
207
208
    /**
209
     * @return SimpleFileBackend
210
     * @throws NoSuchCacheException
211
     */
212 1
    private function getCache()
213
    {
214 1
        return $this->cacheManager->getCache('tx_restler_cache')->getBackend();
215
    }
216
}
217