|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the distributed-configuration-bundle package |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016 Guillaume Cavana |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* Feel free to edit as you please, and have fun. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Guillaume Cavana <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Maikuro\DistributedConfigurationBundle\Controller; |
|
17
|
|
|
|
|
18
|
|
|
use FOS\RestBundle\Util\Codes; |
|
19
|
|
|
use FOS\RestBundle\View\View; |
|
20
|
|
|
use FOS\RestBundle\View\ViewHandler; |
|
21
|
|
|
use Maikuro\DistributedConfigurationBundle\Form\KeyValueType; |
|
22
|
|
|
use Maikuro\DistributedConfigurationBundle\Model\KeyValue; |
|
23
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
24
|
|
|
use Symfony\Component\Form\FormInterface; |
|
25
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
26
|
|
|
use Webmozart\KeyValueStore\Api\WriteException; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class ApiController. |
|
30
|
|
|
*/ |
|
31
|
|
|
class ApiController extends Controller |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Get a value from a key. |
|
35
|
|
|
* |
|
36
|
|
|
* @param Request $request |
|
37
|
|
|
* @param string $key |
|
38
|
|
|
* |
|
39
|
|
|
* @throw Webmozart\KeyValueStore\Api\NoSuchKeyException |
|
40
|
|
|
* |
|
41
|
|
|
* @return KeyValue |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getAction(Request $request, $key) |
|
44
|
|
|
{ |
|
45
|
|
|
$storeHandler = $this->get('gundan_distributed_configuration.store_handler'); |
|
46
|
|
|
|
|
47
|
|
|
$view = View::create() |
|
48
|
|
|
->setStatusCode(Codes::HTTP_OK) |
|
49
|
|
|
->setData([ |
|
50
|
|
|
$storeHandler->get($key), |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
|
|
return $this->getViewHandler()->handle($view, $request); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Create a key value. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Request $request |
|
60
|
|
|
* |
|
61
|
|
|
* @return mixed|FormInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
public function createAction(Request $request) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->handleForm( |
|
66
|
|
|
$this->createRestForm(new KeyValueType(), new KeyValue()), |
|
|
|
|
|
|
67
|
|
|
$request |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Create a key. |
|
73
|
|
|
* |
|
74
|
|
|
* @param Request $request |
|
75
|
|
|
* @param string $key |
|
76
|
|
|
* |
|
77
|
|
|
* @return Response |
|
78
|
|
|
*/ |
|
79
|
|
|
public function updateAction(Request $request, $key) |
|
80
|
|
|
{ |
|
81
|
|
|
$storeHandler = $this->get('gundan_distributed_configuration.store_handler'); |
|
82
|
|
|
$keyValue = $storeHandler->get($key); |
|
83
|
|
|
|
|
84
|
|
|
return $this->handleForm($this->createRestForm( |
|
85
|
|
|
new KeyValueType(), |
|
|
|
|
|
|
86
|
|
|
$keyValue, |
|
87
|
|
|
['method' => $request->getMethod()] |
|
88
|
|
|
), $request); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Delete a key. |
|
93
|
|
|
* |
|
94
|
|
|
* @param Request $request |
|
95
|
|
|
* @param string $key |
|
96
|
|
|
* |
|
97
|
|
|
* @throw Webmozart\KeyValueStore\Api\WriteException |
|
98
|
|
|
* |
|
99
|
|
|
* @return Response |
|
100
|
|
|
*/ |
|
101
|
|
|
public function deleteAction(Request $request, $key) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
$view = View::create() |
|
104
|
|
|
->setStatusCode(Codes::HTTP_NO_CONTENT) |
|
105
|
|
|
; |
|
106
|
|
|
|
|
107
|
|
|
try { |
|
108
|
|
|
$storeHandler = $this->get('gundan_distributed_configuration.store_handler'); |
|
109
|
|
|
$storeHandler->remove($key); |
|
110
|
|
|
} catch (WriteException $e) { |
|
111
|
|
|
$view = View::create() |
|
112
|
|
|
->setStatusCode(Codes::HTTP_BAD_REQUEST) |
|
113
|
|
|
->setData(['errors' => $e->getMessage()]) |
|
114
|
|
|
; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $this->getViewHandler()->handle($view); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* handleForm. |
|
122
|
|
|
* |
|
123
|
|
|
* @param FormInterface $form |
|
124
|
|
|
* @param Request $request |
|
125
|
|
|
* |
|
126
|
|
|
* @return Response |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function handleForm(FormInterface $form, Request $request) |
|
129
|
|
|
{ |
|
130
|
|
|
$method = $request->getMethod(); |
|
131
|
|
|
$form->handleRequest($request); |
|
132
|
|
|
|
|
133
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
134
|
|
|
$entity = $form->getData(); |
|
135
|
|
|
|
|
136
|
|
|
$storeHandler = $this->get('gundan_distributed_configuration.store_handler'); |
|
137
|
|
|
$storeHandler->flush($entity); |
|
138
|
|
|
|
|
139
|
|
|
if (in_array($method, ['PUT', 'PATCH'])) { |
|
140
|
|
|
return $this->getViewHandler()->handle($this->onUpdateSuccess($entity)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $this->getViewHandler()->handle($this->onCreateSuccess($entity)); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $this->getViewHandler()->handle($this->onError($form)); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* onUpdateSuccess. |
|
151
|
|
|
* |
|
152
|
|
|
* @param KeyValue $entity |
|
153
|
|
|
* |
|
154
|
|
|
* @return View |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function onUpdateSuccess(KeyValue $entity) |
|
|
|
|
|
|
157
|
|
|
{ |
|
158
|
|
|
return View::create() |
|
159
|
|
|
->setStatusCode(Codes::HTTP_NO_CONTENT) |
|
160
|
|
|
; |
|
161
|
|
|
|
|
162
|
|
|
return $view; |
|
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Returns a HTTP_BAD_REQUEST response when the form submission fails. |
|
167
|
|
|
* |
|
168
|
|
|
* @param KeyValue $entity |
|
169
|
|
|
* |
|
170
|
|
|
* @return View |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function onCreateSuccess(KeyValue $entity) |
|
173
|
|
|
{ |
|
174
|
|
|
$view = View::create() |
|
175
|
|
|
->setStatusCode(Codes::HTTP_CREATED) |
|
176
|
|
|
->setData( |
|
177
|
|
|
$entity |
|
178
|
|
|
) |
|
179
|
|
|
; |
|
180
|
|
|
|
|
181
|
|
|
return $view; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Returns a HTTP_BAD_REQUEST response when the form submission fails. |
|
186
|
|
|
* |
|
187
|
|
|
* @param FormInterface $form |
|
188
|
|
|
* |
|
189
|
|
|
* @return View |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function onError(FormInterface $form) |
|
192
|
|
|
{ |
|
193
|
|
|
$view = View::create() |
|
194
|
|
|
->setStatusCode(Codes::HTTP_BAD_REQUEST) |
|
195
|
|
|
->setData([ |
|
196
|
|
|
'errors' => $form, |
|
197
|
|
|
]) |
|
198
|
|
|
; |
|
199
|
|
|
|
|
200
|
|
|
return $view; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* createRestForm. |
|
205
|
|
|
* |
|
206
|
|
|
* @param string $type |
|
207
|
|
|
* @param mixed $data |
|
208
|
|
|
* @param array $options |
|
209
|
|
|
* |
|
210
|
|
|
* @return FormInterface |
|
211
|
|
|
*/ |
|
212
|
|
|
protected function createRestForm($type, $data = null, array $options = []) |
|
213
|
|
|
{ |
|
214
|
|
|
// BC >= 2.8 |
|
215
|
|
|
if ('Symfony\Component\Form\Extension\Core\Type\FormType' === $type->getParent()) { |
|
|
|
|
|
|
216
|
|
|
$type = get_class($type); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
return $this->container->get('form.factory')->createNamed(null, $type, $data, $options); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* ViewHandler. |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function getViewHandler() |
|
226
|
|
|
{ |
|
227
|
|
|
return $this->get('fos_rest.view_handler'); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: