Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
the service factory method
<?php
namespace Bundle\ECommerce\ProductBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Doctrine\ODM\MongoDB\Query\Builder;
class ProductExtension extends Extension
{
/**
* Gets a closure that instanciates a paginator
*
* Call it like this:
* $builder = $this->get('ecommerce.repository.product')->getBuilder();
* $pagerFactory = $this->get('ecommerce.paginator.product.factory');
* $pager = \call_user_func($pagerFactory, $builder);*
*
* @static
* @param $paginatorClass the paginator class name
* @param $paginatorAdapterClass the adapter class name
* @return Closure the closure to invoke with a Query builder as first argument
*/
public static function getPaginatorFactory($paginatorClass, $paginatorAdapterClass)
{
return function(Builder $builder) use ($paginatorClass, $paginatorAdapterClass) {
return new $paginatorClass(new $paginatorAdapterClass($builder));
};
}
// some other stuff...
}
<?php
namespace Bundle\ECommerce\ProductBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ProductController extends Controller
{
public function indexAction($page = 1)
{
$pager = $this->getPager();
$pager->setCurrentPageNumber($this->get('request')->get('page', 1));
return $this->render('ProductBundle:Product:index.php', array('pager' => $pager));
}
protected function getPager()
{
$builder = $this->get('ecommerce.repository.product')->getBuilder();
$pagerFactory = $this->get('ecommerce.paginator.product.factory');
return \call_user_func($pagerFactory, $builder);
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://www.symfony-project.org/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="ecommerce.paginator.class">Zend\Paginator\Paginator</parameter>
<parameter key="ecommerce.paginator.adapter.class">ZendPaginatorAdapter\DoctrineMongoDBAdapter</parameter>
</parameters>
<service id="ecommerce.paginator.product.factory" class="Bundle\ECommerce\ProductBundle\DependencyInjection\ProductExtension" shared="false" factory-method="getPaginatorFactory">
<argument>%ecommerce.paginator.class%</argument>
<argument>%ecommerce.paginator.adapter.class%</argument>
</service>
</container>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.