| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
| 5 | * @copyright Aimeos (aimeos.org), 2017-2025 |
||
| 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 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 | * Retrieves 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->view(); |
||
| 37 | |||
| 38 | /** client/jsonapi/template-get |
||
| 39 | * Relative path to the default JSON API template for unknown GET request |
||
| 40 | * |
||
| 41 | * The template file contains the code and processing instructions |
||
| 42 | * to generate the result shown in the JSON API body. The |
||
| 43 | * configuration string is the path to the template file relative |
||
| 44 | * to the templates directory (usually in templates/client/jsonapi). |
||
| 45 | * |
||
| 46 | * You can overwrite the template file configuration in extensions and |
||
| 47 | * provide alternative templates. These alternative templates should be |
||
| 48 | * named like the default one but with the string "standard" replaced by |
||
| 49 | * an unique name. You may use the name of your project for this. If |
||
| 50 | * you've implemented an alternative client class as well, "standard" |
||
| 51 | * should be replaced by the name of the new class. |
||
| 52 | * |
||
| 53 | * @param string Relative path to the template creating the body for the JSON API GET response |
||
| 54 | * @since 2017.05 |
||
| 55 | * @category Developer |
||
| 56 | * @see client/jsonapi/template-options |
||
| 57 | */ |
||
| 58 | $tplconf = 'client/jsonapi/template-get'; |
||
| 59 | $default = 'get-standard'; |
||
| 60 | |||
| 61 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
| 62 | |||
| 63 | return $response->withHeader( 'Allow', 'OPTIONS' ) |
||
| 64 | ->withHeader( 'Cache-Control', 'max-age=300' ) |
||
| 65 | ->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
||
| 66 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
| 67 | ->withStatus( 200 ); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 68 | } |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * Returns the available REST verbs and the available resources |
||
| 73 | * |
||
| 74 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
| 75 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
| 76 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
| 77 | */ |
||
| 78 | public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
| 79 | { |
||
| 80 | $view = $this->view(); |
||
| 81 | |||
| 82 | try |
||
| 83 | { |
||
| 84 | /** client/jsonapi/resources |
||
| 85 | * A list of resource names whose clients are available for the JSON API |
||
| 86 | * |
||
| 87 | * The HTTP OPTIONS method returns a list of resources known by the |
||
| 88 | * JSON API including their URLs. The list of available resources |
||
| 89 | * can be exteded dynamically be implementing a new Jsonadm client |
||
| 90 | * class handling request for this new domain. |
||
| 91 | * |
||
| 92 | * To add the new domain client to the list of resources returned |
||
| 93 | * by the HTTP OPTIONS method, you have to add its name in lower case |
||
| 94 | * to the existing configuration. |
||
| 95 | * |
||
| 96 | * @param array List of resource names |
||
| 97 | * @since 2017.03 |
||
| 98 | * @category Developer |
||
| 99 | */ |
||
| 100 | $resources = $this->context()->config()->get( 'client/jsonapi/resources', [] ); |
||
| 101 | |||
| 102 | $view->locale = $this->context()->locale()->toArray(); |
||
| 103 | $view->resources = (array) $resources; |
||
| 104 | $status = 200; |
||
| 105 | } |
||
| 106 | catch( \Exception $e ) |
||
| 107 | { |
||
| 108 | $status = 500; |
||
| 109 | $view->errors = $this->getErrorDetails( $e ); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** client/jsonapi/template-options |
||
| 113 | * Relative path to the JSON API template for OPTIONS requests |
||
| 114 | * |
||
| 115 | * The template file contains the code and processing instructions |
||
| 116 | * to generate the result shown in the JSON API body. The |
||
| 117 | * configuration string is the path to the template file relative |
||
| 118 | * to the templates directory (usually in templates/client/jsonapi). |
||
| 119 | * |
||
| 120 | * You can overwrite the template file configuration in extensions and |
||
| 121 | * provide alternative templates. These alternative templates should be |
||
| 122 | * named like the default one but with the string "standard" replaced by |
||
| 123 | * an unique name. You may use the name of your project for this. If |
||
| 124 | * you've implemented an alternative client class as well, "standard" |
||
| 125 | * should be replaced by the name of the new class. |
||
| 126 | * |
||
| 127 | * @param string Relative path to the template creating the body for the OPTIONS method of the JSON API |
||
| 128 | * @since 2017.02 |
||
| 129 | * @category Developer |
||
| 130 | * @see client/jsonapi/template-get |
||
| 131 | */ |
||
| 132 | $tplconf = 'client/jsonapi/template-options'; |
||
| 133 | $default = 'options-standard'; |
||
| 134 | |||
| 135 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
| 136 | |||
| 137 | return $response->withHeader( 'Allow', 'GET' ) |
||
| 138 | ->withHeader( 'Cache-Control', 'max-age=300' ) |
||
| 139 | ->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
||
| 140 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
| 141 | ->withStatus( $status ); |
||
| 142 | } |
||
| 143 | } |
||
| 144 |