1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017-2021 |
6
|
|
|
* @package Client |
7
|
|
|
* @subpackage JsonApi |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Client\JsonApi; |
12
|
|
|
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* JSON API common client |
19
|
|
|
* |
20
|
|
|
* @package Client |
21
|
|
|
* @subpackage JsonApi |
22
|
|
|
*/ |
23
|
|
|
abstract class Base |
24
|
|
|
implements \Aimeos\Client\JsonApi\Iface, \Aimeos\MW\Macro\Iface |
25
|
|
|
{ |
26
|
|
|
use \Aimeos\MW\Macro\Traits; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
private $view; |
30
|
|
|
private $context; |
31
|
|
|
private $path; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initializes the client |
36
|
|
|
* |
37
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context MShop context object |
38
|
|
|
* @param string $path Name of the client separated by slashes, e.g "catalog/lists" |
39
|
|
|
*/ |
40
|
|
|
public function __construct( \Aimeos\MShop\Context\Item\Iface $context, string $path ) |
41
|
|
|
{ |
42
|
|
|
$this->context = $context; |
43
|
|
|
$this->path = $path; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Deletes the resource or the resource list |
49
|
|
|
* |
50
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
51
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
52
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
53
|
|
|
*/ |
54
|
|
|
public function delete( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
55
|
|
|
{ |
56
|
|
|
return $this->defaultAction( $request, $response ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Retrieves the resource or the resource list |
62
|
|
|
* |
63
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
64
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
65
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
66
|
|
|
*/ |
67
|
|
|
public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
68
|
|
|
{ |
69
|
|
|
return $this->defaultAction( $request, $response ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Updates the resource or the resource list partitially |
75
|
|
|
* |
76
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
77
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
78
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
79
|
|
|
*/ |
80
|
|
|
public function patch( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
81
|
|
|
{ |
82
|
|
|
return $this->defaultAction( $request, $response ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Creates or updates the resource or the resource list |
88
|
|
|
* |
89
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
90
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
91
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
92
|
|
|
*/ |
93
|
|
|
public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
94
|
|
|
{ |
95
|
|
|
return $this->defaultAction( $request, $response ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Creates or updates the resource or the resource list |
101
|
|
|
* |
102
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
103
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
104
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
105
|
|
|
*/ |
106
|
|
|
public function put( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
107
|
|
|
{ |
108
|
|
|
return $this->defaultAction( $request, $response ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Creates or updates the resource or the resource list |
114
|
|
|
* |
115
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
116
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
117
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
118
|
|
|
*/ |
119
|
|
|
public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
120
|
|
|
{ |
121
|
|
|
return $this->defaultAction( $request, $response ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Sets the view object that will generate the admin output. |
127
|
|
|
* |
128
|
|
|
* @param \Aimeos\MW\View\Iface $view The view object which generates the admin output |
129
|
|
|
* @return \Aimeos\Client\JsonApi\Iface Reference to this object for fluent calls |
130
|
|
|
*/ |
131
|
|
|
public function setView( \Aimeos\MW\View\Iface $view ) : \Aimeos\Client\JsonApi\Iface |
132
|
|
|
{ |
133
|
|
|
$this->view = $view; |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the default response for the resource |
140
|
|
|
* |
141
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
142
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
143
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
144
|
|
|
*/ |
145
|
|
|
protected function defaultAction( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
146
|
|
|
{ |
147
|
|
|
$status = 403; |
148
|
|
|
$view = $this->view(); |
149
|
|
|
|
150
|
|
|
$view->errors = array( array( |
151
|
|
|
'title' => $this->getContext()->translate( 'client/jsonapi', 'Not allowed for this resource' ), |
152
|
|
|
) ); |
153
|
|
|
|
154
|
|
|
/** client/jsonapi/template-error |
155
|
|
|
* Relative path to the default JSON API template |
156
|
|
|
* |
157
|
|
|
* The template file contains the code and processing instructions |
158
|
|
|
* to generate the result shown in the JSON API body. The |
159
|
|
|
* configuration string is the path to the template file relative |
160
|
|
|
* to the templates directory (usually in client/jsonapi/templates). |
161
|
|
|
* |
162
|
|
|
* You can overwrite the template file configuration in extensions and |
163
|
|
|
* provide alternative templates. These alternative templates should be |
164
|
|
|
* named like the default one but with the string "standard" replaced by |
165
|
|
|
* an unique name. You may use the name of your project for this. If |
166
|
|
|
* you've implemented an alternative client class as well, "standard" |
167
|
|
|
* should be replaced by the name of the new class. |
168
|
|
|
* |
169
|
|
|
* @param string Relative path to the template creating the body for the JSON API response |
170
|
|
|
* @since 2017.02 |
171
|
|
|
* @category Developer |
172
|
|
|
* @see client/jsonapi/template-delete |
173
|
|
|
* @see client/jsonapi/template-patch |
174
|
|
|
* @see client/jsonapi/template-post |
175
|
|
|
* @see client/jsonapi/template-get |
176
|
|
|
* @see client/jsonapi/template-options |
177
|
|
|
*/ |
178
|
|
|
$tplconf = 'client/jsonapi/template-error'; |
179
|
|
|
$default = 'error-standard'; |
180
|
|
|
|
181
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
182
|
|
|
|
183
|
|
|
return $response->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
184
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
185
|
|
|
->withStatus( $status ); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Returns the context item object |
191
|
|
|
* |
192
|
|
|
* @return \Aimeos\MShop\Context\Item\Iface Context object |
193
|
|
|
*/ |
194
|
|
|
protected function getContext() : \Aimeos\MShop\Context\Item\Iface |
195
|
|
|
{ |
196
|
|
|
return $this->context; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Returns the translated title and the details of the error |
202
|
|
|
* |
203
|
|
|
* @param \Exception $e Thrown exception |
204
|
|
|
* @param string|null $domain Translation domain |
205
|
|
|
* @return array Associative list with "title" and "detail" key (if debug config is enabled) |
206
|
|
|
*/ |
207
|
|
|
protected function getErrorDetails( \Exception $e, string $domain = null ) : array |
208
|
|
|
{ |
209
|
|
|
$details = []; |
210
|
|
|
|
211
|
|
|
if( $domain !== null ) { |
212
|
|
|
$details['title'] = $this->context->translate( $domain, $e->getMessage() ); |
213
|
|
|
} else { |
214
|
|
|
$details['title'] = $e->getMessage(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** client/jsonapi/debug |
218
|
|
|
* Send debug information withing responses to clients if an error occurrs |
219
|
|
|
* |
220
|
|
|
* By default, the Aimeos client JSON REST API won't send any details |
221
|
|
|
* besides the error message to the client if an error occurred. This |
222
|
|
|
* prevents leaking sensitive information to attackers. For debugging |
223
|
|
|
* your requests it's helpful to see the stack strace. If you set this |
224
|
|
|
* configuration option to true, the stack trace will be returned too. |
225
|
|
|
* |
226
|
|
|
* @param boolean True to return the stack trace in JSON response, false for error message only |
227
|
|
|
* @since 2017.07 |
228
|
|
|
* @category Developer |
229
|
|
|
*/ |
230
|
|
|
if( $this->context->getConfig()->get( 'client/jsonapi/debug', false ) == true ) { |
231
|
|
|
$details['detail'] = $e->getTraceAsString(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return [$details]; // jsonapi.org requires a list of error objects |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Returns the path to the client |
240
|
|
|
* |
241
|
|
|
* @return string Client path, e.g. "product/property" |
242
|
|
|
*/ |
243
|
|
|
protected function getPath() : string |
244
|
|
|
{ |
245
|
|
|
return $this->path; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Initializes the criteria object based on the given parameter |
251
|
|
|
* |
252
|
|
|
* @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
253
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
254
|
|
|
* @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
255
|
|
|
*/ |
256
|
|
|
protected function initCriteria( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface |
257
|
|
|
{ |
258
|
|
|
return $criteria->order( $params['sort'] ?? [] ) |
259
|
|
|
->add( $criteria->parse( $params['filter'] ?? [] ) ) |
260
|
|
|
->slice( $params['page']['offset'] ?? 0, $params['page']['limit'] ?? 25 ); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Returns the available REST verbs and the available parameters |
266
|
|
|
* |
267
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
268
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
269
|
|
|
* @param string $allow Allowed HTTP methods |
270
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
271
|
|
|
*/ |
272
|
|
|
public function getOptionsResponse( ServerRequestInterface $request, ResponseInterface $response, string $allow ) : \Psr\Http\Message\ResponseInterface |
273
|
|
|
{ |
274
|
|
|
$view = $this->view(); |
275
|
|
|
|
276
|
|
|
$tplconf = 'client/jsonapi/template-options'; |
277
|
|
|
$default = 'options-standard'; |
278
|
|
|
|
279
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
280
|
|
|
|
281
|
|
|
return $response->withHeader( 'Allow', $allow ) |
282
|
|
|
->withHeader( 'Cache-Control', 'max-age=300' ) |
283
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
284
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
285
|
|
|
->withStatus( 200 ); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Returns the view object that will generate the admin output. |
291
|
|
|
* |
292
|
|
|
* @return \Aimeos\MW\View\Iface The view object which generates the admin output |
293
|
|
|
*/ |
294
|
|
|
protected function view() : \Aimeos\MW\View\Iface |
295
|
|
|
{ |
296
|
|
|
if( !isset( $this->view ) ) { |
297
|
|
|
throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'No view available' ) ); |
|
|
|
|
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return $this->view; |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths