diff --git a/lib/Alchemy/Phrasea/Collection/CachedCollectionRepository.php b/lib/Alchemy/Phrasea/Collection/CachedCollectionRepository.php index fd918a53cd..7dcf00aae4 100644 --- a/lib/Alchemy/Phrasea/Collection/CachedCollectionRepository.php +++ b/lib/Alchemy/Phrasea/Collection/CachedCollectionRepository.php @@ -9,20 +9,41 @@ */ namespace Alchemy\Phrasea\Collection; +use Alchemy\Phrasea\Application; use Doctrine\Common\Cache\Cache; final class CachedCollectionRepository implements CollectionRepository { - /** @var CollectionRepository */ + /** + * @var Application + */ + private $app; + + /** + * @var CollectionRepository + */ private $repository; - /** @var Cache */ + + /** + * @var Cache + */ private $cache; - /** @var string */ + + /** + * @var string + */ private $cacheKey; - public function __construct(CollectionRepository $repository, Cache $cache, $cacheKey) + /** + * @param Application $application + * @param CollectionRepository $repository + * @param Cache $cache + * @param $cacheKey + */ + public function __construct(Application $application, CollectionRepository $repository, Cache $cache, $cacheKey) { + $this->app = $application; $this->repository = $repository; $this->cache = $cache; $this->cacheKey = $cacheKey; @@ -40,6 +61,10 @@ final class CachedCollectionRepository implements CollectionRepository if ($collections === false) { $collections = $this->repository->findAllByDatabox($databoxId); $this->save($cacheKey, $collections); + } else { + foreach ($collections as $collection) { + $collection->hydrate($this->app); + } } return $collections; @@ -57,6 +82,8 @@ final class CachedCollectionRepository implements CollectionRepository if ($collection === false) { $collection = $this->repository->find($baseId); $this->save($cacheKey, $collection); + } else { + $collection->hydrate($this->app); } return $collection; @@ -75,6 +102,8 @@ final class CachedCollectionRepository implements CollectionRepository if ($collection === false) { $collection = $this->repository->findByCollectionId($databoxId, $collectionId); $this->save($cacheKey, $collection); + } else { + $collection->hydrate($this->app); } return $collection; diff --git a/lib/Alchemy/Phrasea/Collection/CollectionFactory.php b/lib/Alchemy/Phrasea/Collection/CollectionFactory.php index bd3963fc76..18303e47df 100644 --- a/lib/Alchemy/Phrasea/Collection/CollectionFactory.php +++ b/lib/Alchemy/Phrasea/Collection/CollectionFactory.php @@ -21,21 +21,27 @@ class CollectionFactory } /** + * @param int $databoxId * @param CollectionReference $reference * @param array $row * @return \collection */ - public function create(CollectionReference $reference, array $row) + public function create($databoxId, CollectionReference $reference, array $row) { + if ($databoxId != $reference->getDataboxId()) { + throw new \InvalidArgumentException('Reference does not belong to given databoxId.'); + } + return new \collection($this->app, $reference->getBaseId(), $reference, $row); } /** + * @param int $databoxId * @param CollectionReference[] $collectionReferences * @param array $rows * @return array */ - public function createMany($collectionReferences, array $rows) + public function createMany($databoxId, $collectionReferences, array $rows) { Assertion::allIsInstanceOf($collectionReferences, CollectionReference::class); @@ -47,7 +53,7 @@ class CollectionFactory } foreach ($rows as $row) { - $collections[$row['coll_id']] = $this->create($indexedReferences[$row['coll_id']], $row); + $collections[$row['coll_id']] = $this->create($databoxId, $indexedReferences[$row['coll_id']], $row); } return $collections; diff --git a/lib/Alchemy/Phrasea/Collection/DbalCollectionRepository.php b/lib/Alchemy/Phrasea/Collection/DbalCollectionRepository.php index 826894dcd3..729f47703c 100644 --- a/lib/Alchemy/Phrasea/Collection/DbalCollectionRepository.php +++ b/lib/Alchemy/Phrasea/Collection/DbalCollectionRepository.php @@ -53,7 +53,7 @@ class DbalCollectionRepository implements CollectionRepository $query = self::$query . sprintf(' WHERE coll_id IN (%s)', implode(', ', array_keys($params))); $rows = $connection->fetchAll($query, $params); - return $this->collectionFactory->createMany($references, $rows); + return $this->collectionFactory->createMany($databoxId, $references, $rows); } /** @@ -74,7 +74,7 @@ class DbalCollectionRepository implements CollectionRepository $row = $connection->fetchAssoc($query, [ ':collectionId' => $reference->getCollectionId() ]); if ($row !== false) { - return $this->collectionFactory->create($reference, $row); + return $this->collectionFactory->create($reference->getDataboxId(), $reference, $row); } return null; @@ -99,7 +99,7 @@ class DbalCollectionRepository implements CollectionRepository $row = $connection->fetchAssoc($query, [ ':collectionId' => $reference->getCollectionId() ]); if ($row !== false) { - return $this->collectionFactory->create($row['baseId'], $reference, $row); + return $this->collectionFactory->create($databoxId, $row['baseId'], $reference, $row); } return null; diff --git a/lib/Alchemy/Phrasea/Core/Provider/RepositoriesServiceProvider.php b/lib/Alchemy/Phrasea/Core/Provider/RepositoriesServiceProvider.php index ba279d2867..10611af301 100644 --- a/lib/Alchemy/Phrasea/Core/Provider/RepositoriesServiceProvider.php +++ b/lib/Alchemy/Phrasea/Core/Provider/RepositoriesServiceProvider.php @@ -158,7 +158,7 @@ class RepositoriesServiceProvider implements ServiceProviderInterface $factory ); - return new CachedCollectionRepository($repository, $app['cache'], 'collection_'); + return new CachedCollectionRepository($app, $repository, $app['cache'], 'collection_'); }); } diff --git a/lib/classes/collection.php b/lib/classes/collection.php index cefde0d517..8fff1a3d64 100644 --- a/lib/classes/collection.php +++ b/lib/classes/collection.php @@ -236,6 +236,16 @@ EOT; return $collection; } + /** + * @var Application + */ + protected $app; + + /** + * @var databox + */ + protected $databox; + /** * @var CollectionReference */ @@ -261,21 +271,17 @@ EOT; */ protected $labels = []; - /** - * @var databox - */ - protected $databox; - /** * @var int[]|string */ protected $binary_logo; /** - * @var Application + * @param Application $app + * @param $baseId + * @param CollectionReference $reference + * @param array $row */ - protected $app; - public function __construct(Application $app, $baseId, CollectionReference $reference, array $row) { $this->app = $app; @@ -295,6 +301,24 @@ EOT; ]; } + public function __sleep() + { + return array( + 'reference', + 'name', + 'preferences', + 'pub_wm', + 'labels', + 'binary_logo' + ); + } + + public function hydrate(Application $app) + { + $this->app = $app; + $this->databox = $app->getApplicationBox()->get_databox($this->reference->getDataboxId()); + } + private function dispatch($eventName, CollectionEvent $event) { $this->app['dispatcher']->dispatch($eventName, $event);