| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
| 5 | * @copyright Aimeos (aimeos.org), 2021-2025 |
||
| 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 | /** client/jsonapi/site/name |
||
| 28 | * Class name of the used site client implementation |
||
| 29 | * |
||
| 30 | * Each default JSON API client can be replace by an alternative imlementation. |
||
| 31 | * To use this implementation, you have to set the last part of the class |
||
| 32 | * name as configuration value so the client factory knows which class it |
||
| 33 | * has to instantiate. |
||
| 34 | * |
||
| 35 | * For example, if the name of the default class is |
||
| 36 | * |
||
| 37 | * \Aimeos\Client\JsonApi\Site\Standard |
||
| 38 | * |
||
| 39 | * and you want to replace it with your own version named |
||
| 40 | * |
||
| 41 | * \Aimeos\Client\JsonApi\Site\Mysite |
||
| 42 | * |
||
| 43 | * then you have to set the this configuration option: |
||
| 44 | * |
||
| 45 | * client/jsonapi/site/name = Mysite |
||
| 46 | * |
||
| 47 | * The value is the last part of your own class name and it's case sensitive, |
||
| 48 | * so take care that the configuration value is exactly named like the last |
||
| 49 | * part of the class name. |
||
| 50 | * |
||
| 51 | * The allowed characters of the class name are A-Z, a-z and 0-9. No other |
||
| 52 | * characters are possible! You should always start the last part of the class |
||
| 53 | * name with an upper case character and continue only with lower case characters |
||
| 54 | * or numbers. Avoid chamel case names like "MySite"! |
||
| 55 | * |
||
| 56 | * @param string Last part of the class name |
||
| 57 | * @since 2021.04 |
||
| 58 | * @category Developer |
||
| 59 | */ |
||
| 60 | |||
| 61 | /** client/jsonapi/site/decorators/excludes |
||
| 62 | * Excludes decorators added by the "common" option from the JSON API clients |
||
| 63 | * |
||
| 64 | * Decorators extend the functionality of a class by adding new aspects |
||
| 65 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 66 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 67 | * modify what is returned to the caller. |
||
| 68 | * |
||
| 69 | * This option allows you to remove a decorator added via |
||
| 70 | * "client/jsonapi/common/decorators/default" before they are wrapped |
||
| 71 | * around the JsonApi client. |
||
| 72 | * |
||
| 73 | * client/jsonapi/decorators/excludes = array( 'decorator1' ) |
||
| 74 | * |
||
| 75 | * This would remove the decorator named "decorator1" from the list of |
||
| 76 | * common decorators ("\Aimeos\Client\JsonApi\Common\Decorator\*") added via |
||
| 77 | * "client/jsonapi/common/decorators/default" for the JSON API client. |
||
| 78 | * |
||
| 79 | * @param array List of decorator names |
||
| 80 | * @since 2021.04 |
||
| 81 | * @category Developer |
||
| 82 | * @see client/jsonapi/common/decorators/default |
||
| 83 | * @see client/jsonapi/site/decorators/global |
||
| 84 | * @see client/jsonapi/site/decorators/local |
||
| 85 | */ |
||
| 86 | |||
| 87 | /** client/jsonapi/site/decorators/global |
||
| 88 | * Adds a list of globally available decorators only to the JsonApi client |
||
| 89 | * |
||
| 90 | * Decorators extend the functionality of a class by adding new aspects |
||
| 91 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 92 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 93 | * modify what is returned to the caller. |
||
| 94 | * |
||
| 95 | * This option allows you to wrap global decorators |
||
| 96 | * ("\Aimeos\Client\JsonApi\Common\Decorator\*") around the JsonApi |
||
| 97 | * client. |
||
| 98 | * |
||
| 99 | * client/jsonapi/site/decorators/global = array( 'decorator1' ) |
||
| 100 | * |
||
| 101 | * This would add the decorator named "decorator1" defined by |
||
| 102 | * "\Aimeos\Client\JsonApi\Common\Decorator\Decorator1" only to the |
||
| 103 | * "site" JsonApi client. |
||
| 104 | * |
||
| 105 | * @param array List of decorator names |
||
| 106 | * @since 2021.04 |
||
| 107 | * @category Developer |
||
| 108 | * @see client/jsonapi/common/decorators/default |
||
| 109 | * @see client/jsonapi/site/decorators/excludes |
||
| 110 | * @see client/jsonapi/site/decorators/local |
||
| 111 | */ |
||
| 112 | |||
| 113 | /** client/jsonapi/site/decorators/local |
||
| 114 | * Adds a list of local decorators only to the JsonApi client |
||
| 115 | * |
||
| 116 | * Decorators extend the functionality of a class by adding new aspects |
||
| 117 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 118 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 119 | * modify what is returned to the caller. |
||
| 120 | * |
||
| 121 | * This option allows you to wrap local decorators |
||
| 122 | * ("\Aimeos\Client\JsonApi\Site\Decorator\*") around the JsonApi |
||
| 123 | * client. |
||
| 124 | * |
||
| 125 | * client/jsonapi/site/decorators/local = array( 'decorator2' ) |
||
| 126 | * |
||
| 127 | * This would add the decorator named "decorator2" defined by |
||
| 128 | * "\Aimeos\Client\JsonApi\Site\Decorator\Decorator2" only to the |
||
| 129 | * "site" JsonApi client. |
||
| 130 | * |
||
| 131 | * @param array List of decorator names |
||
| 132 | * @since 2021.04 |
||
| 133 | * @category Developer |
||
| 134 | * @see client/jsonapi/common/decorators/default |
||
| 135 | * @see client/jsonapi/site/decorators/excludes |
||
| 136 | * @see client/jsonapi/site/decorators/global |
||
| 137 | */ |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns the resource or the resource list |
||
| 142 | * |
||
| 143 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 144 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 145 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 146 | */ |
||
| 147 | public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 148 | { |
||
| 149 | $view = $this->view(); |
||
| 150 | |||
| 151 | try |
||
| 152 | { |
||
| 153 | $response = $this->getItem( $view, $request, $response ); |
||
| 154 | $status = 200; |
||
| 155 | } |
||
| 156 | catch( \Aimeos\MShop\Exception $e ) |
||
| 157 | { |
||
| 158 | $status = 404; |
||
| 159 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
| 160 | } |
||
| 161 | catch( \Exception $e ) |
||
| 162 | { |
||
| 163 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
| 164 | $view->errors = $this->getErrorDetails( $e ); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** client/jsonapi/site/template |
||
| 168 | * Relative path to the site lists JSON API template |
||
| 169 | * |
||
| 170 | * The template file contains the code and processing instructions |
||
| 171 | * to generate the result shown in the JSON API body. The |
||
| 172 | * configuration string is the path to the template file relative |
||
| 173 | * to the templates directory (usually in templates/client/jsonapi). |
||
| 174 | * |
||
| 175 | * You can overwrite the template file configuration in extensions and |
||
| 176 | * provide alternative templates. These alternative templates should be |
||
| 177 | * named like the default one but with the string "default" replaced by |
||
| 178 | * an unique name. You may use the name of your project for this. If |
||
| 179 | * you've implemented an alternative client class as well, "standard" |
||
| 180 | * should be replaced by the name of the new class. |
||
| 181 | * |
||
| 182 | * @param string Relative path to the template creating the body for the GET method of the JSON API |
||
| 183 | * @since 2021.04 |
||
| 184 | * @site Developer |
||
| 185 | */ |
||
| 186 | $tplconf = 'client/jsonapi/site/template'; |
||
| 187 | $default = 'site/standard'; |
||
| 188 | |||
| 189 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
| 190 | |||
| 191 | return $response->withHeader( 'Allow', 'GET,OPTIONS' ) |
||
| 192 | ->withHeader( 'Cache-Control', 'max-age=300' ) |
||
| 193 | ->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
||
| 194 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
| 195 | ->withStatus( $status ); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 196 | } |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the available REST verbs and the available parameters |
||
| 201 | * |
||
| 202 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 203 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 204 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 205 | */ |
||
| 206 | public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 207 | { |
||
| 208 | return $this->getOptionsResponse( $request, $response, 'GET,OPTIONS' ); |
||
| 209 | } |
||
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * Retrieves the items and adds the data to the view |
||
| 214 | * |
||
| 215 | * @param \Aimeos\Base\View\Iface $view View instance |
||
| 216 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 217 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 218 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 219 | */ |
||
| 220 | protected function getItem( \Aimeos\Base\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 221 | { |
||
| 222 | $map = []; |
||
|
0 ignored issues
–
show
|
|||
| 223 | $ref = $view->param( 'include', [] ); |
||
| 224 | $level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE; |
||
| 225 | |||
| 226 | if( is_string( $ref ) ) { |
||
| 227 | $ref = explode( ',', str_replace( '.', '/', $ref ) ); |
||
| 228 | } |
||
| 229 | |||
| 230 | if( in_array( 'locale/site', $ref, true ) ) |
||
| 231 | { |
||
| 232 | /** client/jsonapi/site/deep |
||
| 233 | * Load the site tree instead of the nodes of the first level only |
||
| 234 | * |
||
| 235 | * If you want to use the site filter component to display the whole |
||
| 236 | * site tree without loading data in an asynchcron way, set this |
||
| 237 | * configuration option to "1" or true. |
||
| 238 | * |
||
| 239 | * **Warning:** If your site tree has a lot of nodes, it will |
||
| 240 | * take a very long time to render all categories. Thus, it's only |
||
| 241 | * recommended for small site trees with a limited node size |
||
| 242 | * (less than 50). |
||
| 243 | * |
||
| 244 | * @param bool True for site tree, false for first level only |
||
| 245 | * @since 2021.04 |
||
| 246 | */ |
||
| 247 | $deep = $view->config( 'client/jsonapi/site/deep', false ); |
||
| 248 | |||
| 249 | $level = $deep ? \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE : \Aimeos\MW\Tree\Manager\Base::LEVEL_LIST; |
||
| 250 | } |
||
| 251 | |||
| 252 | $total = 1; |
||
| 253 | $cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'site' ) |
||
| 254 | ->slice( $view->param( 'page/offset', 0 ), $view->param( 'page/limit', 100 ) ); |
||
| 255 | |||
| 256 | if( ( $cond = (array) $view->param( 'filter', [] ) ) === [] ) { |
||
| 257 | $view->items = $cntl->root( $view->param( 'id' ) )->getTree( $level ); |
||
| 258 | } else { |
||
| 259 | $view->items = $cntl->parse( $cond )->search( $total ); |
||
| 260 | } |
||
| 261 | |||
| 262 | $view->total = $total; |
||
| 263 | |||
| 264 | return $response; |
||
| 265 | } |
||
| 266 | } |
||
| 267 |