1
|
|
|
<?php |
2
|
|
|
namespace Aoe\Restler\System\RestApi; |
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\Restler\Builder as RestlerBuilder; |
30
|
|
|
use Aoe\Restler\System\TYPO3\Cache as Typo3Cache; |
31
|
|
|
use Luracast\Restler\RestException; |
32
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
33
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
34
|
|
|
use TYPO3\CMS\Core\Http\ServerRequest; |
35
|
|
|
use stdClass; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @package Restler |
39
|
|
|
*/ |
40
|
|
|
class RestApiClient implements SingletonInterface |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var Typo3Cache |
44
|
|
|
*/ |
45
|
|
|
private $typo3Cache; |
46
|
|
|
/** |
47
|
|
|
* @var ExtensionConfiguration |
48
|
|
|
*/ |
49
|
|
|
private $extensionConfiguration; |
50
|
|
|
/** |
51
|
|
|
* @var boolean |
52
|
|
|
*/ |
53
|
|
|
private $isExecutingRequest = false; |
54
|
|
|
/** |
55
|
|
|
* @var boolean |
56
|
|
|
*/ |
57
|
|
|
private $isRequestPrepared = false; |
58
|
|
|
/** |
59
|
|
|
* @var RestApiRequestScope |
60
|
|
|
*/ |
61
|
|
|
private $restApiRequestScope; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param ExtensionConfiguration $extensionConfiguration |
65
|
|
|
* @param RestApiRequestScope $restApiRequestScope |
66
|
|
|
* @param Typo3Cache $typo3Cache |
67
|
|
|
*/ |
68
|
5 |
|
public function __construct( |
69
|
|
|
ExtensionConfiguration $extensionConfiguration, |
70
|
|
|
RestApiRequestScope $restApiRequestScope, |
71
|
|
|
Typo3Cache $typo3Cache |
72
|
|
|
) { |
73
|
5 |
|
$this->extensionConfiguration = $extensionConfiguration; |
74
|
5 |
|
$this->restApiRequestScope = $restApiRequestScope; |
75
|
5 |
|
$this->typo3Cache = $typo3Cache; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return boolean |
80
|
|
|
*/ |
81
|
1 |
|
public function isExecutingRequest() |
82
|
|
|
{ |
83
|
1 |
|
return $this->isExecutingRequest; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return boolean |
88
|
|
|
*/ |
89
|
2 |
|
public function isProductionContextSet() |
90
|
|
|
{ |
91
|
2 |
|
return $this->extensionConfiguration->isProductionContextSet(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $requestMethod e.g. 'GET', 'POST', 'PUT' or 'DELETE' |
96
|
|
|
* @param string $requestUri e.g. '/api/products/320' (without GET-params) |
97
|
|
|
* @param array|stdClass $getData |
98
|
|
|
* @param array|stdClass $postData |
99
|
|
|
* @return mixed can be a primitive or array or object |
100
|
|
|
* @throws RestApiRequestException |
101
|
|
|
*/ |
102
|
3 |
|
public function executeRequest($requestMethod, $requestUri, $getData = null, $postData = null) |
103
|
|
|
{ |
104
|
3 |
|
if ($this->isRequestPreparationRequired()) { |
105
|
1 |
|
$this->prepareRequest($requestMethod, $requestUri, $getData, $postData); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
try { |
109
|
3 |
|
$this->isExecutingRequest = true; |
110
|
3 |
|
$result = $this->createRequest()->executeRestApiRequest($requestMethod, $requestUri, $getData, $postData); |
111
|
2 |
|
$this->isExecutingRequest = false; |
112
|
2 |
|
return $result; |
113
|
1 |
|
} catch (RestException $e) { |
114
|
1 |
|
$this->isExecutingRequest = false; |
115
|
1 |
|
$e = $this->createRequestException($e, $requestMethod, $requestUri); |
116
|
1 |
|
throw $e; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* We must create for every REST-API-request a new object, because the object will contain data, which is related to the request |
122
|
|
|
* |
123
|
|
|
* @return RestApiRequest |
124
|
|
|
*/ |
125
|
|
|
protected function createRequest() |
126
|
|
|
{ |
127
|
|
|
return new RestApiRequest($this->restApiRequestScope, $this->typo3Cache); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param RestException $e |
132
|
|
|
* @param string $requestMethod |
133
|
|
|
* @param string $requestUri |
134
|
|
|
* @return RestApiRequestException |
135
|
|
|
*/ |
136
|
1 |
|
protected function createRequestException(RestException $e, $requestMethod, $requestUri) |
137
|
|
|
{ |
138
|
1 |
|
$errorMessage = 'internal REST-API-request \''.$requestMethod.':'.$requestUri.'\' could not be processed'; |
139
|
1 |
|
if (false === $this->isProductionContextSet()) { |
140
|
|
|
$errorMessage .= ' (message: '.$e->getMessage().', details: '.json_encode($e->getDetails()).')'; |
141
|
|
|
} |
142
|
1 |
|
return new RestApiRequestException( |
143
|
|
|
$errorMessage, |
144
|
|
|
RestApiRequestException::EXCEPTION_CODE_REQUEST_COULD_NOT_PROCESSED, |
145
|
|
|
$e |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return RestlerBuilder |
151
|
|
|
*/ |
152
|
|
|
protected function getRestlerBuilder() |
153
|
|
|
{ |
154
|
|
|
return GeneralUtility::makeInstance(RestlerBuilder::class); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* We must prepare the REST-API-request when we are in the 'normal' TYPO3-context (the client, which called this PHP-request, has |
159
|
|
|
* NOT requested an REST-API-endpoint). In this case, we must build the 'original' REST-API-Request (aka Restler-object, which is |
160
|
|
|
* always required), before we can execute any REST-API-request via this PHP-client. |
161
|
|
|
* |
162
|
|
|
* @return boolean |
163
|
|
|
*/ |
164
|
|
|
protected function isRequestPreparationRequired() |
165
|
|
|
{ |
166
|
|
|
if (defined('REST_API_IS_RUNNING') || $this->isRequestPrepared === true) { |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
return true; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* build the 'original' REST-API-Request (aka Restler-object, which is always |
174
|
|
|
* required) and store it in the REST-API-Request-Scope (aka Scope-object) |
175
|
|
|
*/ |
176
|
1 |
|
private function prepareRequest($requestMethod, $requestUri, $getData = null, $postData = null) |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
// TODO: pass along the post data |
179
|
1 |
|
$originalRestApiRequest = $this->getRestlerBuilder()->build(new ServerRequest($requestUri, $requestMethod)); |
180
|
1 |
|
$this->restApiRequestScope->storeOriginalRestApiRequest($originalRestApiRequest); |
181
|
1 |
|
$this->isRequestPrepared = true; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.