1 | <?php |
||
22 | abstract class AbstractSerializerListener |
||
23 | { |
||
24 | /** |
||
25 | * Serializes a controller result and wraps it in a JSON response. |
||
26 | * |
||
27 | * @param GetResponseForControllerResultEvent $event The HTTP kernel event |
||
28 | */ |
||
29 | 4 | public function onKernelView(GetResponseForControllerResultEvent $event) |
|
30 | { |
||
31 | 4 | if (!$this->isControllerResultSupported($event->getControllerResult())) { |
|
32 | 2 | return; |
|
33 | } |
||
34 | |||
35 | 2 | $response = new Response( |
|
36 | 2 | $this->serializeControllerResult($event->getControllerResult()), |
|
37 | 2 | 200, |
|
38 | 2 | array('Content-Type' => 'application/json') |
|
39 | 2 | ); |
|
40 | 2 | $event->setResponse($response); |
|
41 | 2 | } |
|
42 | |||
43 | /** |
||
44 | * Checks whether the controller result object is supported by a serializer. |
||
45 | * |
||
46 | * @param mixed $result The controller result |
||
47 | * |
||
48 | * @return bool True if the object is supported, false otherwise |
||
49 | */ |
||
50 | abstract protected function isControllerResultSupported($result); |
||
51 | |||
52 | /** |
||
53 | * Serializes the controller result object. |
||
54 | * |
||
55 | * @param mixed $result The controller result to serialize |
||
56 | * |
||
57 | * @return string The serialized controller result |
||
58 | */ |
||
59 | abstract protected function serializeControllerResult($result); |
||
60 | } |
||
61 |