|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2021 |
|
6
|
|
|
* @package Client |
|
7
|
|
|
* @subpackage JsonApi |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Client\JsonApi\Site; |
|
12
|
|
|
|
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* JSON API standard client |
|
19
|
|
|
* |
|
20
|
|
|
* @package Client |
|
21
|
|
|
* @subpackage JsonApi |
|
22
|
|
|
*/ |
|
23
|
|
|
class Standard |
|
24
|
|
|
extends \Aimeos\Client\JsonApi\Base |
|
25
|
|
|
implements \Aimeos\Client\JsonApi\Iface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Returns the resource or the resource list |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
31
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
32
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
33
|
|
|
*/ |
|
34
|
|
|
public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
|
35
|
|
|
{ |
|
36
|
|
|
$view = $this->getView(); |
|
37
|
|
|
|
|
38
|
|
|
try |
|
39
|
|
|
{ |
|
40
|
|
|
$response = $this->getItem( $view, $request, $response ); |
|
41
|
|
|
$status = 200; |
|
42
|
|
|
} |
|
43
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
|
44
|
|
|
{ |
|
45
|
|
|
$status = 404; |
|
46
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
|
47
|
|
|
} |
|
48
|
|
|
catch( \Exception $e ) |
|
49
|
|
|
{ |
|
50
|
|
|
$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
|
51
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** client/jsonapi/site/template |
|
55
|
|
|
* Relative path to the site lists JSON API template |
|
56
|
|
|
* |
|
57
|
|
|
* The template file contains the code and processing instructions |
|
58
|
|
|
* to generate the result shown in the JSON API body. The |
|
59
|
|
|
* configuration string is the path to the template file relative |
|
60
|
|
|
* to the templates directory (usually in client/jsonapi/templates). |
|
61
|
|
|
* |
|
62
|
|
|
* You can overwrite the template file configuration in extensions and |
|
63
|
|
|
* provide alternative templates. These alternative templates should be |
|
64
|
|
|
* named like the default one but with the string "default" replaced by |
|
65
|
|
|
* an unique name. You may use the name of your project for this. If |
|
66
|
|
|
* you've implemented an alternative client class as well, "standard" |
|
67
|
|
|
* should be replaced by the name of the new class. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string Relative path to the template creating the body for the GET method of the JSON API |
|
70
|
|
|
* @since 2021.04 |
|
71
|
|
|
* @site Developer |
|
72
|
|
|
*/ |
|
73
|
|
|
$tplconf = 'client/jsonapi/site/template'; |
|
74
|
|
|
$default = 'site/standard'; |
|
75
|
|
|
|
|
76
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
|
77
|
|
|
|
|
78
|
|
|
return $response->withHeader( 'Allow', 'GET,OPTIONS' ) |
|
79
|
|
|
->withHeader( 'Cache-Control', 'max-age=300' ) |
|
80
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
|
81
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
|
82
|
|
|
->withStatus( $status ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns the available REST verbs and the available parameters |
|
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 options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->getOptionsResponse( $request, $response, 'GET,OPTIONS' ); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Retrieves the items and adds the data to the view |
|
101
|
|
|
* |
|
102
|
|
|
* @param \Aimeos\MW\View\Iface $view View instance |
|
103
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
104
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
105
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function getItem( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
|
108
|
|
|
{ |
|
109
|
|
|
$map = []; |
|
|
|
|
|
|
110
|
|
|
$ref = $view->param( 'include', [] ); |
|
111
|
|
|
$level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE; |
|
112
|
|
|
|
|
113
|
|
|
if( is_string( $ref ) ) { |
|
114
|
|
|
$ref = explode( ',', $ref ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if( in_array( 'locale/site', $ref, true ) ) |
|
118
|
|
|
{ |
|
119
|
|
|
/** client/jsonapi/site/deep |
|
120
|
|
|
* Load the site tree instead of the nodes of the first level only |
|
121
|
|
|
* |
|
122
|
|
|
* If you want to use the site filter component to display the whole |
|
123
|
|
|
* site tree without loading data in an asynchcron way, set this |
|
124
|
|
|
* configuration option to "1" or true. |
|
125
|
|
|
* |
|
126
|
|
|
* **Warning:** If your site tree has a lot of nodes, it will |
|
127
|
|
|
* take a very long time to render all categories. Thus, it's only |
|
128
|
|
|
* recommended for small site trees with a limited node size |
|
129
|
|
|
* (less than 50). |
|
130
|
|
|
* |
|
131
|
|
|
* @param bool True for site tree, false for first level only |
|
132
|
|
|
* @since 2021.04 |
|
133
|
|
|
*/ |
|
134
|
|
|
$deep = $view->config( 'client/jsonapi/site/deep', false ); |
|
135
|
|
|
|
|
136
|
|
|
$level = $deep ? \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE : \Aimeos\MW\Tree\Manager\Base::LEVEL_LIST; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$total = 1; |
|
140
|
|
|
$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'site' ); |
|
141
|
|
|
|
|
142
|
|
|
if( ( $cond = (array) $view->param( 'filter', [] ) ) === [] ) { |
|
143
|
|
|
$view->items = $cntl->root( $view->param( 'id' ) )->getTree( $level ); |
|
144
|
|
|
} else { |
|
145
|
|
|
$view->items = $cntl->parse( $cond )->search( $total ); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$view->total = $total; |
|
149
|
|
|
|
|
150
|
|
|
return $response; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|