<?php
namespace Boab\CmsBundle\EventListener;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface as nic;
use Symfony\Component\Serializer\SerializerInterface;
class ApiResponseListener
{
private $serializer;
public function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
public function onKernelView(ViewEvent $event)
{
$request = $event->getRequest();
$controllerResult = $event->getControllerResult();
if(!$request->attributes->has('_api')){
return;
}
if(is_array($controllerResult)){
$response = new JsonResponse($controllerResult);
}else{
$response = new JsonResponse();
$response->setContent($controllerResult);
}
$response->setStatusCode(200);
$event->setResponse($response);
}
public function onKernelResponse(ResponseEvent $event)
{
$request = $event->getRequest();
$result = $event->getResponse();
if($result instanceof SelfAwareNormalizerInterface || $result instanceof NormalizerInterface ){
$json = $this->serializer->serialize($result);
$jsonResponse = new JsonResponse($json);
$event->setResponse($jsonResponse);
return;
}
}
}