Permalink
Browse files

Review pagination system, please do not use, WIP

  • Loading branch information...
1 parent 91dc750 commit 20d54986f77f82750ebcde0b3979a5ec6b6044c5 @stephpy committed Dec 19, 2012
@@ -32,6 +32,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('filter.xml');
$loader->load('notification.xml');
$loader->load('paginator.xml');
+ $loader->load('result_builder.xml');
$loader->load('spread.xml');
$loader->load('twig.xml');
@@ -69,7 +70,6 @@ public function load(array $configs, ContainerBuilder $container)
$paginator = sprintf('spy_timeline.pager.%s', $driver);
}
- $container->setAlias('spy_timeline.pager', $paginator);
// filters
$filters = isset($config['filters']) ? $config['filters'] : array();
@@ -97,6 +97,16 @@ public function load(array $configs, ContainerBuilder $container)
$filterManager->addMethodCall('add', array($service));
}
+ // result builder
+
+ $definition = $container->getDefinition('spy_timeline.result_builder');
+ $definition->addArgument($container->getDefinition(sprintf('spy_timeline.query_executor.%s', $driver)));
+ $definition->addArgument($filterManager);
+
+ if ($paginator) {
+ $definition->addMethodCall('setPager', array($container->getDefinition($paginator)));
+ }
+
// notifiers
$notifiers = $config['notifiers'];
$definition = $container->getDefinition('spy_timeline.notification_manager');
View
@@ -4,7 +4,7 @@
use Doctrine\ODM\MongoDB\Query\Builder;
use Spy\Timeline\Filter\FilterManager;
-use Spy\Timeline\Pager\PagerInterface;
+use Spy\Timeline\ResultBuilder\Pager\PagerInterface;
/**
* Pager
@@ -0,0 +1,15 @@
+<?php
+
+namespace Spy\TimelineBundle\Driver\ODM;
+
+use Spy\Timeline\ResultBuilder\QueryExecutor\QueryExecutorInterface;
+
+/**
+ * QueryExecutor
+ *
+ * @uses QueryExecutorInterface
+ * @author Stephane PY <py.stephane1@gmail.com>
+ */
+class QueryExecutor implements QueryExecutorInterface
+{
+}
View
@@ -4,7 +4,7 @@
use Doctrine\ORM\QueryBuilder as DoctrineQueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator;
-use Spy\Timeline\Pager\PagerInterface;
+use Spy\Timeline\ResultBuilder\Pager\PagerInterface;
use Spy\Timeline\Filter\FilterManagerInterface;
/**
@@ -18,19 +18,6 @@ class Pager implements PagerInterface, \IteratorAggregate, \Countable
protected $items = array();
/**
- * @var FilterManagerInterface
- */
- protected $filterManager;
-
- /**
- * @param FilterManagerInterface $filterManager filterManager
- */
- public function __construct(FilterManagerInterface $filterManager)
- {
- $this->filterManager = $filterManager;
- }
-
- /**
* {@inheritdoc}
*/
public function paginate($target, $page = 1, $limit = 10, $options = array())
@@ -8,8 +8,8 @@
use Spy\Timeline\Driver\QueryBuilder\Criteria\CriteriaInterface;
use Spy\Timeline\Driver\QueryBuilder\Criteria\Operator;
use Doctrine\Common\Persistence\ObjectManager;
+use Spy\Timeline\ResultBuilder\ResultBuilderInterface;
use Spy\Timeline\Model\ActionInterface;
-use Spy\Timeline\Pager\PagerInterface;
/**
* QueryBuilder
@@ -25,9 +25,9 @@ class QueryBuilder extends BaseQueryBuilder
protected $objectManager;
/**
- * @var PagerInterface
+ * @var ResultBuilderInterface
*/
- protected $pager;
+ protected $resultBuilder;
/**
* @var string
@@ -38,18 +38,18 @@ class QueryBuilder extends BaseQueryBuilder
CONST NOT_APPLY_FILTER = false;
/**
- * @param QueryBuilderFactory $factory factory
- * @param ObjectManager $objectManager objectManager
- * @param PagerInterface $pager pager
- * @param string $timelineClass timelineClass
+ * @param QueryBuilderFactory $factory factory
+ * @param ObjectManager $objectManager objectManager
+ * @param ResultBuilderInterface $resultBuilder resultBuilder
+ * @param string $timelineClass timelineClass
*/
- public function __construct(QueryBuilderFactory $factory, ObjectManager $objectManager, PagerInterface $pager, $timelineClass)
+ public function __construct(QueryBuilderFactory $factory, ObjectManager $objectManager, ResultBuilderInterface $resultBuilder, $timelineClass)
{
parent::__construct($factory);
- $this->objectManager = $objectManager;
- $this->pager = $pager;
- $this->timelineClass = $timelineClass;
+ $this->objectManager = $objectManager;
+ $this->resultBuilder = $resultBuilder;
+ $this->timelineClass = $timelineClass;
}
/**
@@ -96,22 +96,8 @@ public function createQueryBuilder()
public function execute($filter = self::APPLY_FILTER)
{
$qb = $this->createQueryBuilder();
- $pager = $this->pager->paginate($qb, $this->page, $this->maxPerPage);
- $actions = array_map(
- function ($timeline) {
- return $timeline->getAction();
- },
- $pager->getItems()
- );
-
- $pager->setItems($actions);
-
- if ($filter) {
- return $pager->filter($pager);
- }
-
- return $pager;
+ return $this->resultBuilder->fetchResults($qb, $this->page, $this->maxPerPage, $filter, true);
}
/**
@@ -0,0 +1,43 @@
+<?php
+
+namespace Spy\TimelineBundle\Driver\ORM;
+
+use Spy\Timeline\ResultBuilder\QueryExecutor\QueryExecutorInterface;
+use Doctrine\ORM\QueryBuilder as DoctrineQueryBuilder;
+use Doctrine\ORM\Tools\Pagination\Paginator;
+
+/**
+ * QueryExecutor
+ *
+ * @uses QueryExecutorInterface
+ * @author Stephane PY <py.stephane1@gmail.com>
+ */
+class QueryExecutor implements QueryExecutorInterface
+{
+ /**
+ * @param mixed $query query
+ * @param int $page page
+ * @param int $maxPerPage maxPerPage
+ *
+ * @return \Traversable
+ */
+ public function fetch($query, $page = 1, $maxPerPage = 10)
+ {
+ if (!$query instanceof DoctrineQueryBuilder) {
+ throw new \Exception('Not supported yet');
+ }
+
+ if ($maxPerPage) {
+ $offset = ($page - 1) * (int) $maxPerPage;
+
+ $query
+ ->setFirstResult($offset)
+ ->setMaxResults($maxPerPage);
+ }
+
+ // use pager even if it's a query executor, to fix number of results returned issue.
+ $paginator = new Paginator($query, true);
+
+ return (array) $paginator->getIterator();
+ }
+}
@@ -13,13 +13,13 @@
<services>
<service id="spy_timeline.timeline_manager.odm" class="%spy_timeline.timeline_manager.odm.class%">
<argument type="service" id="spy_timeline.driver.object_manager" />
- <argument type="service" id="spy_timeline.pager" />
+ <argument type="service" id="spy_timeline.result_builder" />
<argument>%spy_timeline.class.timeline%</argument>
</service>
<service id="spy_timeline.action_manager.odm" class="%spy_timeline.action_manager.odm.class%">
<argument type="service" id="spy_timeline.driver.object_manager" />
- <argument type="service" id="spy_timeline.pager" />
+ <argument type="service" id="spy_timeline.result_builder" />
<argument>%spy_timeline.class.action%</argument>
<argument>%spy_timeline.class.component%</argument>
<argument>%spy_timeline.class.action_component%</argument>
@@ -8,19 +8,20 @@
<parameter key="spy_timeline.timeline_manager.orm.class">Spy\TimelineBundle\Driver\ORM\TimelineManager</parameter>
<parameter key="spy_timeline.action_manager.orm.class">Spy\TimelineBundle\Driver\ORM\ActionManager</parameter>
<parameter key="spy_timeline.pager.orm.class">Spy\TimelineBundle\Driver\ORM\Pager</parameter>
+ <parameter key="spy_timeline.query_executor.orm.class">Spy\TimelineBundle\Driver\ORM\QueryExecutor</parameter>
<parameter key="spy_timeline.query_builder.class">Spy\TimelineBundle\Driver\ORM\QueryBuilder\QueryBuilder</parameter>
</parameters>
<services>
<service id="spy_timeline.timeline_manager.orm" class="%spy_timeline.timeline_manager.orm.class%">
<argument type="service" id="spy_timeline.driver.object_manager" />
- <argument type="service" id="spy_timeline.pager" />
+ <argument type="service" id="spy_timeline.result_builder" />
<argument>%spy_timeline.class.timeline%</argument>
</service>
<service id="spy_timeline.action_manager.orm" class="%spy_timeline.action_manager.orm.class%">
<argument type="service" id="spy_timeline.driver.object_manager" />
- <argument type="service" id="spy_timeline.pager" />
+ <argument type="service" id="spy_timeline.result_builder" />
<argument>%spy_timeline.class.action%</argument>
<argument>%spy_timeline.class.component%</argument>
<argument>%spy_timeline.class.action_component%</argument>
@@ -29,25 +30,13 @@
</call>
</service>
- <service id="spy_timeline.action_manager.orm" class="%spy_timeline.action_manager.orm.class%">
- <argument type="service" id="spy_timeline.driver.object_manager" />
- <argument type="service" id="spy_timeline.pager" />
- <argument>%spy_timeline.class.action%</argument>
- <argument>%spy_timeline.class.component%</argument>
- <argument>%spy_timeline.class.action_component%</argument>
- <call method="setDeployer">
- <argument type="service" id="spy_timeline.spread.deployer" />
- </call>
- </service>
-
- <service id="spy_timeline.pager.orm" class="%spy_timeline.pager.orm.class%">
- <argument type="service" id="spy_timeline.filter.manager" />
- </service>
+ <service id="spy_timeline.pager.orm" class="%spy_timeline.pager.orm.class%" />
+ <service id="spy_timeline.query_executor.orm" class="%spy_timeline.query_executor.orm.class%" />
<service id="spy_timeline.query_builder.orm" class="%spy_timeline.query_builder.class%">
<argument type="service" id="spy_timeline.query_builder.factory" />
<argument type="service" id="spy_timeline.driver.object_manager" />
- <argument type="service" id="spy_timeline.pager" />
+ <argument type="service" id="spy_timeline.result_builder" />
<argument>%spy_timeline.class.timeline%</argument>
</service>
</services>
@@ -13,14 +13,14 @@
<services>
<service id="spy_timeline.timeline_manager.redis" class="%spy_timeline.timeline_manager.redis.class%">
<argument type="service" id="spy_timeline.driver.redis.client" />
- <argument type="service" id="spy_timeline.pager" />
+ <argument type="service" id="spy_timeline.result_builder" />
<argument>%spy_timeline.driver.redis.prefix%</argument>
<argument>%spy_timeline.driver.redis.pipeline%</argument>
</service>
<service id="spy_timeline.action_manager.redis" class="%spy_timeline.action_manager.redis.class%">
<argument type="service" id="spy_timeline.driver.redis.client" />
- <argument type="service" id="spy_timeline.pager" />
+ <argument type="service" id="spy_timeline.result_builder" />
<argument>%spy_timeline.driver.redis.prefix%</argument>
<argument>%spy_timeline.class.action%</argument>
<argument>%spy_timeline.class.component%</argument>
@@ -0,0 +1,13 @@
+<container xmlns="http://symfony.com/schema/dic/services"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
+
+ <parameters>
+ <parameter key="spy_timeline.result_builder.class">Spy\Timeline\ResultBuilder\ResultBuilder</parameter>
+ </parameters>
+
+ <services>
+ <service id="spy_timeline.result_builder" class="%spy_timeline.result_builder.class%">
+ </service>
+ </services>
+</container>

0 comments on commit 20d5498

Please sign in to comment.