1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license GPLv3, http://www.gnu.org/copyleft/gpl.html |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2016 |
6
|
|
|
* @package TYPO3 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
namespace Aimeos\Aimeos\Controller; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
use Aimeos\Aimeos\Base; |
14
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Controller for the JSON API |
19
|
|
|
* |
20
|
|
|
* @package TYPO3 |
21
|
|
|
*/ |
22
|
|
|
class JsonadmController extends AbstractController |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Initializes the object before the real action is called. |
26
|
|
|
*/ |
27
|
|
|
protected function initializeAction() |
28
|
|
|
{ |
29
|
|
|
$this->uriBuilder->setArgumentPrefix('tx_aimeos_web_aimeos'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Dispatches the REST API requests |
35
|
|
|
* |
36
|
|
|
* @return string Generated output |
37
|
|
|
*/ |
38
|
|
|
public function indexAction() |
39
|
|
|
{ |
40
|
|
|
$resource = $this->request->hasArgument('resource') ? $this->request->getArgument('resource') : ''; |
41
|
|
|
|
42
|
|
|
switch ($this->request->getMethod()) { |
43
|
|
|
case 'DELETE': return $this->deleteAction($resource); |
44
|
|
|
case 'PATCH': return $this->patchAction($resource); |
45
|
|
|
case 'POST': return $this->postAction($resource); |
46
|
|
|
case 'PUT': return $this->putAction($resource); |
47
|
|
|
case 'GET': return $this->getAction($resource); |
48
|
|
|
default: return $this->optionsAction($resource); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Deletes the resource object or a list of resource objects |
55
|
|
|
* |
56
|
|
|
* @param string Resource location, e.g. "product/property/type" |
57
|
|
|
* @return string Generated output |
58
|
|
|
*/ |
59
|
|
|
public function deleteAction(string $resource) |
60
|
|
|
{ |
61
|
|
|
return $this->createAdmin($resource)->delete($this->getPsrRequest(), (new Psr17Factory)->createResponse()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the requested resource object or list of resource objects |
67
|
|
|
* |
68
|
|
|
* @param string Resource location, e.g. "product/property/type" |
69
|
|
|
* @return string Generated output |
70
|
|
|
*/ |
71
|
|
|
public function getAction(string $resource) |
72
|
|
|
{ |
73
|
|
|
return $this->createAdmin($resource)->get($this->getPsrRequest(), (new Psr17Factory)->createResponse()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Updates a resource object or a list of resource objects |
79
|
|
|
* |
80
|
|
|
* @param string Resource location, e.g. "product/property/type" |
81
|
|
|
* @return string Generated output |
82
|
|
|
*/ |
83
|
|
|
public function patchAction(string $resource) |
84
|
|
|
{ |
85
|
|
|
return $this->createAdmin($resource)->patch($this->getPsrRequest(), (new Psr17Factory)->createResponse()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Creates a new resource object or a list of resource objects |
91
|
|
|
* |
92
|
|
|
* @param string Resource location, e.g. "product/property/type" |
93
|
|
|
* @return string Generated output |
94
|
|
|
*/ |
95
|
|
|
public function postAction(string $resource) |
96
|
|
|
{ |
97
|
|
|
return $this->createAdmin($resource)->post($this->getPsrRequest(), (new Psr17Factory)->createResponse()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Creates or updates a single resource object |
103
|
|
|
* |
104
|
|
|
* @param string Resource location, e.g. "product/property/type" |
105
|
|
|
* @return string Generated output |
106
|
|
|
*/ |
107
|
|
|
public function putAction(string $resource) |
108
|
|
|
{ |
109
|
|
|
return $this->createAdmin($resource)->put($this->getPsrRequest(), (new Psr17Factory)->createResponse()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns the available HTTP verbs and the resource URLs |
115
|
|
|
* |
116
|
|
|
* @param string Resource location, e.g. "product/property/type" |
117
|
|
|
* @return string Generated output |
118
|
|
|
*/ |
119
|
|
|
public function optionsAction(string $resource) |
120
|
|
|
{ |
121
|
|
|
return $this->createAdmin($resource)->options($this->getPsrRequest(), (new Psr17Factory)->createResponse()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the resource client |
127
|
|
|
* |
128
|
|
|
* @param string Resource location, e.g. "product/property/type" |
129
|
|
|
* @return \Aimeos\Admin\JsonAdm\Iface Jsonadm client |
130
|
|
|
*/ |
131
|
|
|
protected function createAdmin(string $resource) : \Aimeos\Admin\JsonAdm\Iface |
132
|
|
|
{ |
133
|
|
|
$context = $this->contextBackend('admin/jsonadm/templates'); |
134
|
|
|
return \Aimeos\Admin\JsonAdm::create($context, Base::aimeos(), $resource); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns a PSR-7 request object for the current request |
140
|
|
|
* |
141
|
|
|
* @return \Psr\Http\Message\RequestInterface PSR-7 request object |
142
|
|
|
*/ |
143
|
|
|
protected function getPsrRequest() : \Psr\Http\Message\RequestInterface |
144
|
|
|
{ |
145
|
|
|
$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory(); |
146
|
|
|
|
147
|
|
|
$creator = new \Nyholm\Psr7Server\ServerRequestCreator( |
148
|
|
|
$psr17Factory, // ServerRequestFactory |
149
|
|
|
$psr17Factory, // UriFactory |
150
|
|
|
$psr17Factory, // UploadedFileFactory |
151
|
|
|
$psr17Factory // StreamFactory |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
return $creator->fromGlobals(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|