1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license MIT, http://opensource.org/licenses/MIT |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2016 |
6
|
|
|
* @package symfony |
7
|
|
|
* @subpackage Controller |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\ShopBundle\Controller; |
12
|
|
|
|
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
14
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
15
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Aimeos controller for the JSON REST API |
24
|
|
|
* |
25
|
|
|
* @package symfony |
26
|
|
|
* @subpackage Controller |
27
|
|
|
*/ |
28
|
|
|
class JsonadmController extends AbstractController |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Deletes the resource object or a list of resource objects |
32
|
|
|
* |
33
|
|
|
* @param Request $request Request object |
34
|
|
|
* @param string Resource location, e.g. "product/property/type" |
35
|
|
|
* @param string $site Unique site code |
36
|
|
|
* @return Response Response object containing the generated output |
37
|
|
|
*/ |
38
|
|
|
public function deleteAction( Request $request, string $resource, string $site = 'default' ) : Response |
39
|
|
|
{ |
40
|
|
|
$req = $this->createRequest( $request ); |
41
|
|
|
$res = ( new Psr17Factory )->createResponse(); |
42
|
|
|
|
43
|
|
|
$client = $this->createAdmin( $site, $resource, $req->getAttribute( 'locale', 'en' ) ); |
44
|
|
|
return $this->createResponse( $client->delete( $req, $res ) ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the requested resource object or list of resource objects |
50
|
|
|
* |
51
|
|
|
* @param Request $request Request object |
52
|
|
|
* @param string Resource location, e.g. "product/property/type" |
53
|
|
|
* @param string $site Unique site code |
54
|
|
|
* @return Response Response object containing the generated output |
55
|
|
|
*/ |
56
|
|
|
public function getAction( Request $request, string $resource, string $site = 'default' ) : Response |
57
|
|
|
{ |
58
|
|
|
$req = $this->createRequest( $request ); |
59
|
|
|
$res = ( new Psr17Factory )->createResponse(); |
60
|
|
|
|
61
|
|
|
$client = $this->createAdmin( $site, $resource, $req->getAttribute( 'locale', 'en' ) ); |
62
|
|
|
return $this->createResponse( $client->get( $req, $res ) ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Updates a resource object or a list of resource objects |
68
|
|
|
* |
69
|
|
|
* @param Request $request Request object |
70
|
|
|
* @param string Resource location, e.g. "product/property/type" |
71
|
|
|
* @param string $site Unique site code |
72
|
|
|
* @return Response Response object containing the generated output |
73
|
|
|
*/ |
74
|
|
|
public function patchAction( Request $request, string $resource, string $site = 'default' ) : Response |
75
|
|
|
{ |
76
|
|
|
$req = $this->createRequest( $request ); |
77
|
|
|
$res = ( new Psr17Factory )->createResponse(); |
78
|
|
|
|
79
|
|
|
$client = $this->createAdmin( $site, $resource, $req->getAttribute( 'locale', 'en' ) ); |
80
|
|
|
return $this->createResponse( $client->patch( $req, $res ) ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Creates a new resource object or a list of resource objects |
86
|
|
|
* |
87
|
|
|
* @param Request $request Request object |
88
|
|
|
* @param string Resource location, e.g. "product/property/type" |
89
|
|
|
* @param string $site Unique site code |
90
|
|
|
* @return Response Response object containing the generated output |
91
|
|
|
*/ |
92
|
|
|
public function postAction( Request $request, string $resource, string $site = 'default' ) : Response |
93
|
|
|
{ |
94
|
|
|
$req = $this->createRequest( $request ); |
95
|
|
|
$res = ( new Psr17Factory )->createResponse(); |
96
|
|
|
|
97
|
|
|
$client = $this->createAdmin( $site, $resource, $req->getAttribute( 'locale', 'en' ) ); |
98
|
|
|
return $this->createResponse( $client->post( $req, $res ) ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Creates or updates a single resource object |
104
|
|
|
* |
105
|
|
|
* @param Request $request Request object |
106
|
|
|
* @param string Resource location, e.g. "product/property/type" |
107
|
|
|
* @param string $site Unique site code |
108
|
|
|
* @return Response Response object containing the generated output |
109
|
|
|
*/ |
110
|
|
|
public function putAction( Request $request, string $resource, string $site = 'default' ) : Response |
111
|
|
|
{ |
112
|
|
|
$req = $this->createRequest( $request ); |
113
|
|
|
$res = ( new Psr17Factory )->createResponse(); |
114
|
|
|
|
115
|
|
|
$client = $this->createAdmin( $site, $resource, $req->getAttribute( 'locale', 'en' ) ); |
116
|
|
|
return $this->createResponse( $client->put( $req, $res ) ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the available HTTP verbs and the resource URLs |
122
|
|
|
* |
123
|
|
|
* @param Request $request Request object |
124
|
|
|
* @param string Resource location, e.g. "product/property/type" |
125
|
|
|
* @param string $site Unique site code |
126
|
|
|
* @return Response Response object containing the generated output |
127
|
|
|
*/ |
128
|
|
|
public function optionsAction( Request $request, string $resource = '', string $site = 'default' ) : Response |
129
|
|
|
{ |
130
|
|
|
$req = $this->createRequest( $request ); |
131
|
|
|
$res = ( new Psr17Factory )->createResponse(); |
132
|
|
|
|
133
|
|
|
$client = $this->createAdmin( $site, $resource, $req->getAttribute( 'locale', 'en' ) ); |
134
|
|
|
return $this->createResponse( $client->options( $req, $res ) ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the resource controller |
140
|
|
|
* |
141
|
|
|
* @param string $site Unique site code |
142
|
|
|
* @param string Resource location, e.g. "product/property/type" |
143
|
|
|
* @param string $lang Language code |
144
|
|
|
* @return \Aimeos\Admin\JsonAdm\Iface Context item |
145
|
|
|
*/ |
146
|
|
|
protected function createAdmin( string $site, string $resource, string $lang ) : \Aimeos\Admin\JsonAdm\Iface |
147
|
|
|
{ |
148
|
|
|
$aimeos = $this->container->get( 'aimeos' )->get(); |
149
|
|
|
$templatePaths = $aimeos->getTemplatePaths( 'admin/jsonadm/templates' ); |
150
|
|
|
|
151
|
|
|
$context = $this->container->get( 'aimeos.context' )->get( false, 'backend' ); |
152
|
|
|
$context->setI18n( $this->container->get( 'aimeos.i18n' )->get( array( $lang, 'en' ) ) ); |
153
|
|
|
$context->setLocale( $this->container->get( 'aimeos.locale' )->getBackend( $context, $site ) ); |
154
|
|
|
|
155
|
|
|
$view = $this->container->get( 'aimeos.view' )->create( $context, $templatePaths, $lang ); |
156
|
|
|
$context->setView( $view ); |
157
|
|
|
|
158
|
|
|
return \Aimeos\Admin\JsonAdm::create( $context, $aimeos, $resource ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
protected function createRequest( Request $reqest ) : \Psr\Http\Message\RequestInterface |
163
|
|
|
{ |
164
|
|
|
$psr17Factory = new Psr17Factory(); |
165
|
|
|
$psrHttpFactory = new PsrHttpFactory( $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory ); |
166
|
|
|
|
167
|
|
|
return $psrHttpFactory->createRequest( $reqest ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
protected function createResponse( \Psr\Http\Message\ResponseInterface $response ) : Response |
172
|
|
|
{ |
173
|
|
|
$httpFoundationFactory = new HttpFoundationFactory(); |
174
|
|
|
return $httpFoundationFactory->createResponse( $response ); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|