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