Created
December 12, 2010 01:14
-
-
Save docteurklein/737764 to your computer and use it in GitHub Desktop.
the service factory method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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