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