1 | <?php |
||
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( |
|
73 | |||
74 | /** |
||
75 | * initialize and configure restler-framework and return restler-object |
||
76 | * |
||
77 | * @return RestlerExtended |
||
78 | */ |
||
79 | public function build() |
||
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 | ); |
||
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 | 2 | $message .= '$GLOBALS[\'TYPO3_CONF_VARS\'][\'SC_OPTIONS\'][\'restler\'][\'restlerConfigurationClasses\'][] = |
|
122 | \'[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 | 1 | $externalRestlerConfigurationClasses |
|
133 | ); |
||
134 | } |
||
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 | } |
||
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 | } |
||
164 | } |
||
165 | } |
||
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 | 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() |
|
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 | } |
||
204 | 1 | } |
|
205 | |||
206 | /** |
||
207 | * @return SimpleFileBackend |
||
208 | * @throws NoSuchCacheException |
||
209 | */ |
||
210 | 1 | private function getCache() |
|
214 | } |
||
215 |