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