Skip to content
This repository was archived by the owner on Jun 19, 2022. It is now read-only.

Commit d7efd44

Browse files
author
fd6130
committed
create new adapter for doctrine result cache
1 parent 404dab9 commit d7efd44

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Fd\HslBundle\Pagination\Adapter;
4+
5+
use Doctrine\ORM\Query;
6+
use Doctrine\ORM\QueryBuilder;
7+
use Doctrine\ORM\Tools\Pagination\Paginator;
8+
use Pagerfanta\Adapter\AdapterInterface;
9+
10+
/**
11+
* Adapter which calculates pagination from a Doctrine ORM Query or QueryBuilder.
12+
*/
13+
class DoctrineAdapter implements AdapterInterface
14+
{
15+
private Paginator $paginator;
16+
private ?int $lifetime;
17+
private ?string $resultCacheId;
18+
19+
/**
20+
* @param Query|QueryBuilder $query
21+
* @param bool $fetchJoinCollection Whether the query joins a collection (true by default)
22+
* @param bool|null $useOutputWalkers Flag indicating whether output walkers are used in the paginator
23+
*/
24+
public function __construct($query, bool $fetchJoinCollection = true, ?bool $useOutputWalkers = null, ?int $lifetime = null, ?string $resultCacheId = null)
25+
{
26+
$this->paginator = new Paginator($query, $fetchJoinCollection);
27+
$this->paginator->setUseOutputWalkers($useOutputWalkers);
28+
$this->lifetime = $lifetime;
29+
$this->resultCacheId = $resultCacheId;
30+
}
31+
32+
public function getQuery(): Query
33+
{
34+
return $this->paginator->getQuery();
35+
}
36+
37+
public function getFetchJoinCollection(): bool
38+
{
39+
return $this->paginator->getFetchJoinCollection();
40+
}
41+
42+
public function getNbResults(): int
43+
{
44+
return \count($this->paginator);
45+
}
46+
47+
public function getSlice(int $offset, int $length): iterable
48+
{
49+
$paginator = $this->paginator->getQuery()
50+
->setFirstResult($offset)
51+
->setMaxResults($length);
52+
53+
if($this->lifetime == null && $this->resultCacheId == null)
54+
{
55+
return $this->paginator->getIterator();
56+
}
57+
58+
$paginator->enableResultCache($this->lifetime, $this->resultCacheId);
59+
60+
return $this->paginator->getIterator();
61+
}
62+
}

src/Pagination/Paginator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Fd\HslBundle\Pagination;
44

5+
use Doctrine\ORM\Query;
56
use Doctrine\ORM\QueryBuilder;
7+
use Fd\HslBundle\Pagination\Adapter\DoctrineAdapter;
68
use League\Fractal\Manager;
79
use League\Fractal\Pagination\PagerfantaPaginatorAdapter;
810
use League\Fractal\Resource\Collection;
@@ -30,15 +32,15 @@ public function __construct(RouterInterface $router, RequestStack $requestStack,
3032
/**
3133
* {@inheritdoc}
3234
*/
33-
public function paginate($result, $transformer): ResourceAbstract
35+
public function paginate($result, $transformer, ?int $lifetime = null, ?string $resultCacheId = null): ResourceAbstract
3436
{
3537
$request = $this->requestStack->getCurrentRequest();
3638
$page = $request->query->getInt('page', 1);
3739
$limitPerPage = $request->query->getInt('limit', $this->limit);
3840

39-
if($result instanceof QueryBuilder)
41+
if($result instanceof QueryBuilder || $result instanceof Query)
4042
{
41-
$adapter = new QueryAdapter($result);
43+
$adapter = new DoctrineAdapter($result, true, null, $lifetime, $resultCacheId);
4244
}
4345
else
4446
{

src/Pagination/PaginatorInterface.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Fd\HslBundle\Pagination;
44

5+
use Doctrine\Common\Collections\Collection;
6+
use Doctrine\ORM\Query;
7+
use Doctrine\ORM\QueryBuilder;
58
use League\Fractal\Resource\ResourceAbstract;
69

710
interface PaginatorInterface
@@ -11,12 +14,14 @@ interface PaginatorInterface
1114
/**
1215
* Paginate the result and transform the value using transformer.
1316
*
14-
* @param QueryBuilder|array|Collection $result result to be paginate
15-
* @param string $transformer transformer service id
17+
* @param Query|QueryBuilder|array|Collection $result Result to be paginate
18+
* @param string $transformer Service id or FQCN
19+
* @param int|null $lifetime For doctrine cache only - cache lifetime
20+
* @param string|null $resultCacheId For doctrine cache only - unique cache id
1621
*
1722
* @return ResourceAbstract
1823
*/
19-
public function paginate($result, $transformer): ResourceAbstract;
24+
public function paginate($result, $transformer, ?int $lifetime = null, ?string $resultCacheId = null): ResourceAbstract;
2025

2126
public function setLimitPerPage(int $max);
2227
}

0 commit comments

Comments
 (0)