parent
5cfc1e54e1
commit
ab078b3b98
@ -0,0 +1,91 @@ |
||||
## PagerfantaServiceProvider |
||||
|
||||
Provider to use [Pagerfanta](https://github.com/whiteoctober/Pagerfanta) with [Silex](https://github.com/fabpot/Silex) |
||||
|
||||
This Provider is based on [WhiteOctoberPagerfantaBundle](https://github.com/whiteoctober/WhiteOctoberPagerfantaBundle) and includes: |
||||
* Twig function to render pagerfantas with views and options. |
||||
* Way to use easily views. |
||||
|
||||
#### Registering |
||||
|
||||
``` |
||||
$app->register(new FranMoreno\Silex\Provider\PagerfantaServiceProvider()); |
||||
``` |
||||
|
||||
#### Parameters |
||||
|
||||
This are the default parameters: |
||||
|
||||
``` |
||||
$app['pagerfanta.view.options'] = array( |
||||
'routeName' => null, |
||||
'routeParams' => array(), |
||||
'pageParameter' => '[page]', |
||||
'proximity' => 3, |
||||
'next_message' => '»', |
||||
'prev_message' => '«', |
||||
'default_view' => 'default' |
||||
); |
||||
``` |
||||
|
||||
#### Rendering pagination |
||||
|
||||
The Twig Extension provides this function: |
||||
|
||||
{{ pagerfanta(my_pager, view_name, view_options) }} |
||||
|
||||
The routes are generated automatically for the current route using the variable |
||||
"page" to propagate the page number. By default, the bundle uses the |
||||
*DefaultView* with the *default* name. |
||||
|
||||
{{ pagerfanta(my_pager) }} |
||||
|
||||
If you want to use a custom template, add another argument |
||||
|
||||
<div class="pagerfanta"> |
||||
{{ pagerfanta(my_pager, 'my_template') }} |
||||
</div> |
||||
|
||||
With Options |
||||
|
||||
{{ pagerfanta(my_pager, 'default', { 'proximity': 2}) }} |
||||
|
||||
See the Pagerfanta documentation for the list of the parameters. |
||||
|
||||
#### Adding a custom template |
||||
|
||||
You can use the CSS classes provided with DefaultView, but if you want to create a Custom View, you have to implement ```Pagerfanta\View\ViewInterface```, then add the View to the Factory: |
||||
|
||||
$app['pagerfanta.view_factory'] = $app->share($app->extend('pagerfanta.view_factory', function($viewFactory, $app) { |
||||
$customView = new \Foo\Bar\View\CustomView(); |
||||
$viewFactory->add(array( |
||||
'my_view' => $customView |
||||
)); |
||||
|
||||
return $viewFactory; |
||||
})); |
||||
|
||||
And if you want set as default view: |
||||
|
||||
$app['pagerfanta.view.options'] = array( |
||||
'default_view' => 'my_view' |
||||
); |
||||
|
||||
#### Use in controllers |
||||
|
||||
use Pagerfanta\Pagerfanta; |
||||
use Pagerfanta\Adapter\ArrayAdapter; |
||||
|
||||
$app->get('/index', function (Request $request) use ($app) { |
||||
|
||||
$results = $app['some.service']->getResults(); |
||||
|
||||
$adapter = new ArrayAdapter($results); |
||||
$pagerfanta = new Pagerfanta($adapter); |
||||
$pagerfanta->setMaxPerPage(10); |
||||
$pagerfanta->setCurrentPage($request->query->get('page', 1)); |
||||
|
||||
return $app['twig']->render('index.html', array( |
||||
'my_pager' => $pagerfanta |
||||
)); |
||||
}) |
@ -0,0 +1,30 @@ |
||||
{ |
||||
"name": "franmomu/silex-pagerfanta-provider", |
||||
"type": "library", |
||||
"description": "Silex ServiceProvider for Pagerfanta Library", |
||||
"keywords": ["silex", "service provider", "pagerfanta"], |
||||
"homepage": "https://github.com/franmomu/silex-pagerfanta-provider", |
||||
"license": "MIT", |
||||
"authors": [ |
||||
{ |
||||
"name": "Fran Moreno", |
||||
"email": "franmomu@gmail.com", |
||||
"homepage": "http://showmethecode.es" |
||||
} |
||||
], |
||||
"require": { |
||||
"silex/silex": "1.*", |
||||
"pagerfanta/pagerfanta": "dev-master", |
||||
"symfony/property-access": "~2.3" |
||||
}, |
||||
"autoload": { |
||||
"psr-0": { |
||||
"FranMoreno": "src/" |
||||
} |
||||
}, |
||||
"extra": { |
||||
"branch-alias": { |
||||
"dev-master": "1.0.x-dev" |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,59 @@ |
||||
<?php |
||||
|
||||
namespace FranMoreno\Silex\Provider; |
||||
|
||||
use Silex\Application; |
||||
use Silex\ServiceProviderInterface; |
||||
|
||||
use Pagerfanta\View\DefaultView; |
||||
use Pagerfanta\View\TwitterBootstrapView; |
||||
use Pagerfanta\View\ViewFactory; |
||||
use FranMoreno\Silex\Service\PagerfantaFactory; |
||||
use FranMoreno\Silex\Twig\PagerfantaExtension; |
||||
|
||||
class PagerfantaServiceProvider implements ServiceProviderInterface |
||||
{ |
||||
public function register(Application $app) |
||||
{ |
||||
$app['pagerfanta.pager_factory'] = $app->share(function ($app) { |
||||
return new PagerfantaFactory(); |
||||
}); |
||||
|
||||
$app['pagerfanta.view.default_options'] = array( |
||||
'routeName' => null, |
||||
'routeParams' => array(), |
||||
'pageParameter' => '[page]', |
||||
'proximity' => 3, |
||||
'next_message' => '»', |
||||
'prev_message' => '«', |
||||
'default_view' => 'default' |
||||
); |
||||
|
||||
$app['pagerfanta.view_factory'] = $app->share(function ($app) { |
||||
$defaultView = new DefaultView(); |
||||
$twitterBoostrapView = new TwitterBootstrapView(); |
||||
|
||||
$factoryView = new ViewFactory(); |
||||
$factoryView->add(array( |
||||
$defaultView->getName() => $defaultView, |
||||
$twitterBoostrapView->getName() => $twitterBoostrapView |
||||
)); |
||||
|
||||
return $factoryView; |
||||
}); |
||||
|
||||
if (isset($app['twig'])) { |
||||
$app->extend('twig', function($twig, $app) { |
||||
$twig->addExtension(new PagerfantaExtension($app)); |
||||
|
||||
return $twig; |
||||
}); |
||||
} |
||||
} |
||||
|
||||
public function boot(Application $app) |
||||
{ |
||||
$options = isset($app['pagerfanta.view.options']) ? $app['pagerfanta.view.options'] : array(); |
||||
$app['pagerfanta.view.options'] = array_replace($app['pagerfanta.view.default_options'], $options); |
||||
} |
||||
} |
@ -0,0 +1,30 @@ |
||||
<?php |
||||
|
||||
namespace FranMoreno\Silex\Service; |
||||
|
||||
use Pagerfanta\Pagerfanta; |
||||
use Pagerfanta\Adapter\AdapterInterface; |
||||
use Pagerfanta\Adapter\ArrayAdapter; |
||||
use Pagerfanta\Adapter\DoctrineORMAdapter; |
||||
|
||||
class PagerfantaFactory |
||||
{ |
||||
public function getForArray($array) |
||||
{ |
||||
$adapter = new ArrayAdapter($array); |
||||
|
||||
return $this->createPagerfanta($adapter); |
||||
} |
||||
|
||||
public function getForDoctrineORM($query, $fetchJoinCollection = true) |
||||
{ |
||||
$adapter = new DoctrineORMAdapter($query, $fetchJoinCollection); |
||||
|
||||
return $this->createPagerfanta($adapter); |
||||
} |
||||
|
||||
protected function createPagerfanta(AdapterInterface $adapter) |
||||
{ |
||||
return new Pagerfanta($adapter); |
||||
} |
||||
} |
@ -0,0 +1,80 @@ |
||||
<?php |
||||
|
||||
namespace FranMoreno\Silex\Twig; |
||||
|
||||
use Silex\Application; |
||||
use Symfony\Component\PropertyAccess\PropertyAccess; |
||||
use Pagerfanta\PagerfantaInterface; |
||||
|
||||
class PagerfantaExtension extends \Twig_Extension |
||||
{ |
||||
private $app; |
||||
|
||||
public function __construct(Application $app) |
||||
{ |
||||
$this->app = $app; |
||||
} |
||||
|
||||
public function getFunctions() |
||||
{ |
||||
return array( |
||||
'pagerfanta' => new \Twig_Function_Method($this, 'renderPagerfanta', array('is_safe' => array('html'))), |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* 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 = null, array $options = array()) |
||||
{ |
||||
$options = array_replace($this->app['pagerfanta.view.options'], $options); |
||||
|
||||
if (null === $viewName) { |
||||
$viewName = $options['default_view']; |
||||
} |
||||
|
||||
$router = $this->app['url_generator']; |
||||
|
||||
//Custom router and router params |
||||
if (isset($this->app['pagerfanta.view.router.name'])) { |
||||
$options['routeName'] = $this->app['pagerfanta.view.router.name']; |
||||
} |
||||
|
||||
if (isset($this->app['pagerfanta.view.router.params'])) { |
||||
$options['routeParams'] = $this->app['pagerfanta.view.router.params']; |
||||
} |
||||
|
||||
if (null === $options['routeName']) { |
||||
$request = $this->app['request']; |
||||
|
||||
$options['routeName'] = $request->attributes->get('_route'); |
||||
$options['routeParams'] = array_merge($request->query->all(), $request->attributes->get('_route_params')); |
||||
} |
||||
|
||||
$routeName = $options['routeName']; |
||||
$routeParams = $options['routeParams']; |
||||
$pageParameter = $options['pageParameter']; |
||||
$propertyAccessor = PropertyAccess::getPropertyAccessor(); |
||||
|
||||
$routeGenerator = function($page) use($router, $routeName, $routeParams, $pageParameter, $propertyAccessor) { |
||||
$propertyAccessor->setValue($routeParams, $pageParameter, $page); |
||||
return $router->generate($routeName, $routeParams); |
||||
}; |
||||
|
||||
return $this->app['pagerfanta.view_factory']->get($viewName)->render($pagerfanta, $routeGenerator, $options); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function getName() |
||||
{ |
||||
return 'pagerfanta'; |
||||
} |
||||
} |
Loading…
Reference in new issue