first implementation of timeline

refs #224

Some parts have evolved from issue, we should rethink some parts of the design
This commit is contained in:
Julien Fastré 2015-02-11 00:37:05 +01:00
parent 3f4132e23d
commit 799893316d
6 changed files with 284 additions and 0 deletions

View File

@ -6,6 +6,7 @@ use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Chill\MainBundle\DependencyInjection\SearchableServicesCompilerPass;
use Chill\MainBundle\DependencyInjection\ConfigConsistencyCompilerPass;
use Chill\MainBundle\DependencyInjection\TimelineCompilerClass;
class ChillMainBundle extends Bundle
{
@ -14,5 +15,6 @@ class ChillMainBundle extends Bundle
parent::build($container);
$container->addCompilerPass(new SearchableServicesCompilerPass());
$container->addCompilerPass(new ConfigConsistencyCompilerPass());
$container->addCompilerPass(new TimelineCompilerClass());
}
}

View File

@ -0,0 +1,66 @@
<?php
/*
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
/**
* Add services taggued with `name: chill.timeline` to
* timeline_builder service definition
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class TimelineCompilerClass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('chill.main.timeline_builder')) {
throw new \LogicException('service chill.main.timeline_builder '
. 'is not defined.');
}
$definition = $container->getDefinition(
'chill.main.timeline_builder'
);
$taggedServices = $container->findTaggedServiceIds(
'chill.timeline'
);
foreach ($taggedServices as $id => $tagAttributes) {
foreach ($tagAttributes as $attributes) {
if (!isset($attributes["context"])) {
throw new \LogicException("the 'context' attribute is missing in your ".
"service '$id' definition");
}
$definition->addMethodCall(
'addProvider',
array($attributes["context"], $id)
);
}
}
}
}

View File

@ -79,3 +79,10 @@ services:
chill.main.search_provider:
class: Chill\MainBundle\Search\SearchProvider
chill.main.timeline_builder:
class: Chill\MainBundle\Timeline\TimelineBuilder
arguments:
- "@doctrine.orm.entity_manager"
calls:
- [ setContainer, ["@service_container"]]

View File

@ -0,0 +1,8 @@
<div class="timeline">
{% for result in results %}
<div>
{% include result.template with result.templateArgs %}
</div>
{% endfor %}
</div>

View File

@ -0,0 +1,150 @@
<?php
/*
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Timeline;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
/**
* Build timeline
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class TimelineBuilder implements ContainerAwareInterface
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
/**
*
* @var \Doctrine\ORM\EntityManagerInterface
*/
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
*
* @var string references to providers services
*/
private $providers = array();
public function getTimeline($context, array $args, $page = 0, $number = 20)
{
$query = $this->buildQuery($context, $args, $page, $number);
$ids = $this->runQuery($query);
$entitiesByKey = $this->getEntities($ids, $context);
return $this->render($ids, $entitiesByKey);
}
public function addProvider($context, $id)
{
$this->providers[$context][] = [$id];
}
private function buildQuery($context, array $args, $page, $number)
{
if (!array_key_exists($context, $this->providers)) {
throw new \LogicException(sprintf('No builders have been defined for "%s"'
. ' context', $context));
}
$query = '';
foreach($this->providers[$context] as $providerIds) {
foreach ($providerIds as $providerId) {
$provider = $this->container->get($providerId);
$query .= ($query === '') ?
$provider->fetchUnion($context, $args) :
' UNION '.$provider->fetchUnion($context, $args);
}
}
$query .= sprintf(' ORDER BY date LIMIT %d OFFSET %d',
$number, $page * $number);
return $query;
}
private function runQuery($query)
{
$resultSetMapping = (new ResultSetMapping())
->addScalarResult('id', 'id')
->addScalarResult('key', 'key')
->addScalarResult('date', 'date');
return $this->em->createNativeQuery($query, $resultSetMapping)
->getArrayResult();
}
private function getEntities(array $queriedIds, $context)
{
//gather entities by key. Having all ids in the same table allow to query from providers
$idsByKey = array();
foreach($queriedIds as $result) {
$idsByKey[$result['key']][] = $result['id'];
}
//fetch entities from providers
$entitiesByKey = array();
foreach ($idsByKey as $key => $ids) {
//iterate providers for current context
foreach($this->providers[$context] as $providerIds) {
foreach ($providerIds as $providerId){
$provider = $this->container->get($providerId);
if ($provider->supportsKey($key)) {
$entitiesByKey[$key] = $provider->getEntities($ids);
}
}
}
}
return $entitiesByKey;
}
private function render(array $queriedIds, $entitiesByKey)
{
//add results to a pretty array
$timelineEntries = array();
foreach ($queriedIds as $result) {
$timelineEntry['date'] = $result['date'];
$timelineEntry['template'] = $entitiesByKey[$result['key']][$result['id']]['template'];
$timelineEntry['templateArgs'] = $entitiesByKey[$result['key']][$result['id']]['entity'];
$timelineEntries[] = $timelineEntry;
}
return $this->container->get('templating')
->render('ChillMainBundle:Timeline:index.html.twig', array(
'results' => $timelineEntries
));
}
}

View File

@ -0,0 +1,51 @@
<?php
/*
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Timeline;
/**
* Interface for service providing info to timeline
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
interface TimelineProviderInterface
{
/**
* Indicate if the result id may be handled by the service
*
* @param string $key the key present in the SELECT query
* @return boolean
*/
public function supportsKey($key);
/**
* fetch entities from db and indicate how to render them
*
* @param array $ids
* @return array[] an array of an associative array. 'template' will indicate a template name on how to render the entity, 'entity' will be the arguments of the template
*/
public function getEntities(array $ids);
/**
* provide a SQL SELECT query to fetch entities id
*
* @return string an SQL SELECT query which will fetch ID. The query must have and id (INT), a key (STRING), and a datetime to order results
*/
public function fetchUnion($context, array $args);
}