/**
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $e
*/
public function onKernelView(GetResponseForControllerResultEvent $e)
{
$queryset = $e->getControllerResult();
$routeName = $e->getRequest()->attributes->get('_route');
$route = $this->routes->get($routeName);
if (!$route) {
return;
}
$interface = 'SDispatcher\\Common\\PaginatorInterface';
$paginatorClass = $route->getOption(RouteOptions::PAGINATOR_CLASS);
if (!$paginatorClass || !is_subclass_of($paginatorClass, $interface)) {
return;
}
try {
/* @var \SDispatcher\Common\PaginatorInterface $paginator */
$paginator = new $paginatorClass();
if (!$paginator->supports($queryset)) {
return;
}
list($headers, $data) = $paginator->paginate($e->getRequest(), $queryset, 0, $route->getOption(RouteOptions::PAGE_LIMIT), $route->getOption(RouteOptions::PAGINATED_META_CONTAINER_NAME), $route->getOption(RouteOptions::PAGINATED_DATA_CONTAINER_NAME));
} catch (\Exception $ex) {
list($headers, $data) = array(array(), array());
}
$response = new DataResponse($data);
$response->headers->add($headers);
$e->setResponse($response);
}