/**
* Renders a pagerfanta.
*
* @param PagerfantaInterface $pagerfanta The pagerfanta.
* @param string $viewName The view name.
* @param array $options An array of options (optional).
*
* @return string The pagerfanta rendered.
*/
public function renderPagerfanta(PagerfantaInterface $pagerfanta, $viewName = 'default', array $options = array())
{
$options = array_replace(array('routeName' => null, 'routeParams' => array(), 'pageParameter' => 'page'), $options);
$router = $this->container->get('router');
$request = $this->container->get('request');
if (null === $options['routeName']) {
$options['routeName'] = $request->attributes->get('_route');
$options['routeParams'] = $request->query->all();
if ($options['routeName'] === '_internal') {
throw new \Exception('PagerfantaBundle can not guess the route when used in a subrequest');
}
foreach ($router->getRouteCollection()->get($options['routeName'])->compile()->getVariables() as $variable) {
$options['routeParams'][$variable] = $request->attributes->get($variable);
}
}
$routeName = $options['routeName'];
$routeParams = $options['routeParams'];
$pagePropertyPath = new PropertyPath($options['pageParameter']);
$routeGenerator = function ($page) use($router, $routeName, $routeParams, $pagePropertyPath) {
$pagePropertyPath->setValue($routeParams, $page);
return $router->generate($routeName, $routeParams);
};
return $this->container->get('white_october_pagerfanta.view_factory')->get($viewName)->render($pagerfanta, $routeGenerator, $options);
}