Rework FeedAggregate. Issue with embedded entry collection.

PHRAS-542
This commit is contained in:
Benoît Burnichon
2015-05-27 12:41:09 +02:00
parent a85f495b23
commit 2489cc6ffe
8 changed files with 336 additions and 30 deletions

View File

@@ -1836,7 +1836,7 @@ class V1Controller extends Controller
{ {
return array_map(function ($entry) use ($request) { return array_map(function ($entry) use ($request) {
return $this->listPublicationEntry($request, $entry); return $this->listPublicationEntry($request, $entry);
}, iterator_to_array($feed->getEntries($offset_start, $how_many))); }, $feed->getEntries()->slice($offset_start, $how_many));
} }
/** /**

View File

@@ -109,17 +109,11 @@ class Aggregate implements FeedInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getEntries($offset_start = null, $how_many = null) public function getEntries()
{ {
if ($this->feeds->isEmpty()) {
return new ArrayCollection();
}
$feedIds = $this->feeds->getKeys();
/** @var FeedEntryRepository $feedEntryRepository */ /** @var FeedEntryRepository $feedEntryRepository */
$feedEntryRepository = $this->em->getRepository('Phraseanet:FeedEntry'); $feedEntryRepository = $this->em->getRepository('Phraseanet:FeedEntry');
return new ArrayCollection($feedEntryRepository->findByFeeds($feedIds, $offset_start, $how_many)); return new AggregateEntryCollection($feedEntryRepository, $this->feeds);
} }
/** /**

View File

@@ -0,0 +1,319 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2015 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Feed;
use Alchemy\Phrasea\Model\Repositories\FeedEntryRepository;
use Closure;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Selectable;
class AggregateEntryCollection implements Collection, Selectable
{
/** @var FeedEntryRepository */
private $repository;
/** @var FeedInterface[] */
private $feeds;
/** @var ArrayCollection|null */
private $entries;
/**
* @param FeedEntryRepository $repository
* @param FeedInterface[] $feeds
*/
public function __construct(FeedEntryRepository $repository, $feeds)
{
$this->repository = $repository;
if ($feeds instanceof \Traversable) {
$feeds = iterator_to_array($feeds);
}
$this->feeds = $feeds;
}
public function slice($offset, $length = null)
{
return $this->repository->findByFeeds($this->feeds, $offset, $length);
}
private function __load___()
{
$this->entries = new ArrayCollection($this->repository->findByFeeds($this->feeds));
}
public function count()
{
if (null === $this->entries) {
return $this->repository->countByFeeds($this->feeds);
}
return $this->entries->count();
}
public function toArray()
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->toArray();
}
public function first()
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->first();
}
public function last()
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->last();
}
public function key()
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->key();
}
public function next()
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->next();
}
public function current()
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->current();
}
public function remove($key)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->remove($key);
}
public function removeElement($element)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->removeElement($element);
}
public function offsetExists($offset)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->offsetExists($offset);
}
public function offsetGet($offset)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->offsetGet($offset);
}
public function offsetSet($offset, $value)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->offsetSet($offset, $value);
}
public function offsetUnset($offset)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->offsetUnset($offset);
}
public function containsKey($key)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->containsKey($key);
}
public function contains($element)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->contains($element);
}
public function exists(Closure $p)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->exists($p);
}
public function indexOf($element)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->indexOf($element);
}
public function get($key)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->get($key);
}
public function getKeys()
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->getKeys();
}
public function getValues()
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->getValues();
}
public function set($key, $value)
{
if (null === $this->entries) {
$this->__load___();
}
$this->entries->set($key, $value);
}
public function add($value)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->add($value);
}
public function isEmpty()
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->isEmpty();
}
public function getIterator()
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->getIterator();
}
public function map(Closure $func)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->map($func);
}
public function filter(Closure $p)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->filter($p);
}
public function forAll(Closure $p)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->forAll($p);
}
public function partition(Closure $p)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->partition($p);
}
public function clear()
{
if (null === $this->entries) {
$this->__load___();
}
$this->entries->clear();
}
public function matching(Criteria $criteria)
{
if (null === $this->entries) {
$this->__load___();
}
return $this->entries->matching($criteria);
}
}

View File

@@ -33,12 +33,9 @@ interface FeedInterface
/** /**
* Returns a collection of FeedEntry. * Returns a collection of FeedEntry.
* *
* @param integer $offset_start
* @param integer $how_many
*
* @return FeedEntry[]|Collection * @return FeedEntry[]|Collection
*/ */
public function getEntries($offset_start = 0, $how_many = null); public function getEntries();
/** /**
* Returns an UTF-8 subtitle for the feed. * Returns an UTF-8 subtitle for the feed.

View File

@@ -254,17 +254,11 @@ class Feed implements FeedInterface
/** /**
* Get entries * Get entries
* *
* @param int $offset_start
* @param int $how_many
* @return FeedEntry[]|Collection * @return FeedEntry[]|Collection
*/ */
public function getEntries($offset_start = 0, $how_many = null) public function getEntries()
{ {
if (null === $how_many) { return $this->entries;
return $this->entries;
}
return $this->entries->slice($offset_start, $how_many);
} }
/** /**

View File

@@ -5,7 +5,7 @@
</div> </div>
<div> <div>
{% for entry in feeds.getEntries(0, 5) %} {% for entry in feeds.entries.slice(0, 5) %}
<div class="boxPubli"> <div class="boxPubli">
<div class="titlePubli"> <div class="titlePubli">
<h2 class="htitlePubli"> <h2 class="htitlePubli">

View File

@@ -58,7 +58,7 @@
{% set doctype_display = user_setting('doctype_display') %} {% set doctype_display = user_setting('doctype_display') %}
{% set offset_start = ((page - 1) * 5 )%} {% set offset_start = ((page - 1) * 5 )%}
{% set entries = feed.getEntries(offset_start,5) %} {% set entries = feed.entries.slice(offset_start,5) %}
{% for entry in entries %} {% for entry in entries %}
{% block content_item %} {% block content_item %}

View File

@@ -272,14 +272,16 @@ class RSSFeedTest extends \PhraseanetWebTestCase
$count = 0; $count = 0;
$offset_start = 0; $offset_start = 0;
$n_entries = 20; $n_entries = 20;
$entries = $feed->getEntries($offset_start, $n_entries); $entries = $feed->getEntries()->slice($offset_start, $n_entries);
foreach ($list_entries as $node) { foreach ($list_entries as $node) {
if (sizeof($entries) == 0) { if (sizeof($entries) == 0) {
$offset_start = ($offset_start++) * $n_entries; $offset_start += $n_entries;
$entries = $feed->getEntries($offset_start, $n_entries); $entries = $feed->getEntries()->slice($offset_start, $n_entries);
if (sizeof($entries) == 0) //no more if (sizeof($entries) == 0) {
//no more
break; break;
}
} }
$feed_entry = array_shift($entries); $feed_entry = array_shift($entries);
switch ($node->nodeName) { switch ($node->nodeName) {
@@ -567,12 +569,12 @@ class RSSFeedTest extends \PhraseanetWebTestCase
$count = 0; $count = 0;
$offset_start = 0; $offset_start = 0;
$n_entries = 20; $n_entries = 20;
$entries = $feed->getEntries($offset_start, $n_entries); $entries = $feed->getEntries()->slice($offset_start, $n_entries);
foreach ($entries_item as $entry) { foreach ($entries_item as $entry) {
if (sizeof($entries) == 0) { if (sizeof($entries) == 0) {
$offset_start = ($offset_start++) * $n_entries; $offset_start += $n_entries;
$entries = $feed->getEntries($offset_start, $n_entries); $entries = $feed->getEntries()->slice($offset_start, $n_entries);
if (sizeof($entries) == 0) //no more if (sizeof($entries) == 0) //no more
break; break;
} }