-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedDqlConditionGenerator.php
More file actions
121 lines (98 loc) · 4.06 KB
/
CachedDqlConditionGenerator.php
File metadata and controls
121 lines (98 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <s.stok@rollerscapes.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Rollerworks\Component\Search\Doctrine\Orm;
use Doctrine\ORM\QueryBuilder;
use Psr\SimpleCache\CacheInterface as Cache;
use Rollerworks\Component\Search\Doctrine\Dbal\AbstractCachedConditionGenerator;
use Rollerworks\Component\Search\Exception\BadMethodCallException;
use Rollerworks\Component\Search\SearchCondition;
/**
* SearchCondition Doctrine ORM DQL CachedConditionGenerator.
*
* Note: this class should not be relied upon as interface,
* use the ConditionGenerator interface instead for type hinting
*
* The cache-key is a hashed (sha256) combination of the SearchCondition
* (root ValuesGroup and FieldSet name) and configured field mappings.
*
* Caution: Any noticeable changes to your (FieldSet) configuration
* should purge all cached entries.
*
* After the fields are configured call `apply()` to apply
* the condition on the QueryBuilder.
*
* @final
*/
class CachedDqlConditionGenerator extends AbstractCachedConditionGenerator implements ConditionGenerator
{
private FieldConfigBuilder $fieldsConfig;
private QueryBuilder $qb;
public function __construct(QueryBuilder $query, SearchCondition $searchCondition, Cache $cacheDriver, \DateInterval | int | null $ttl = null)
{
parent::__construct($cacheDriver, $searchCondition, $ttl);
$this->fieldsConfig = new FieldConfigBuilder($query->getEntityManager(), $searchCondition->getFieldSet());
$this->qb = $query;
}
public function setDefaultEntity(string $entity, string $alias)
{
$this->guardNotGenerated();
$this->fieldsConfig->setDefaultEntity($entity, $alias);
return $this;
}
/**
* @throws BadMethodCallException When the where-clause is already generated
*/
protected function guardNotGenerated(): void
{
if ($this->isApplied) {
throw new BadMethodCallException('ConditionGenerator configuration cannot be changed once the query is applied.');
}
}
public function setField(string $fieldName, string $property, ?string $alias = null, ?string $entity = null, ?string $dbType = null)
{
$this->guardNotGenerated();
$this->fieldsConfig->setField($fieldName, $property, $alias, $entity, $dbType);
return $this;
}
public function apply(): void
{
if ($this->isApplied) {
trigger_error('SearchCondition was already applied. Ignoring operation.', \E_USER_WARNING);
return;
}
$this->isApplied = true;
$cacheKey = $this->getCacheKey($this->fieldsConfig->getFields(), 'dql');
$cached = $this->getFromCache($cacheKey);
// Note that ordering is not part of the cache as this only applies at the root level
// And is handled by Doctrine DQL itself, making it possible to reuse the same condition
// with a different ordering.
if ($cached !== null) {
[$whereClause, $parameters] = $cached;
} else {
$generator = new DqlConditionGenerator($this->qb->getEntityManager(), $this->searchCondition, $this->fieldsConfig);
$whereClause = $generator->getWhereClause();
$parameters = $generator->getParameters();
$this->storeInCache($whereClause, $cacheKey, $parameters);
}
DqlConditionGenerator::applySortingTo($this->searchCondition->getPrimaryCondition()?->getOrder(), $this->qb, $this->fieldsConfig);
DqlConditionGenerator::applySortingTo($this->searchCondition->getOrder(), $this->qb, $this->fieldsConfig);
if ($whereClause !== '') {
$this->qb->andWhere($whereClause);
foreach ($parameters as $name => [$value, $type]) {
$this->qb->setParameter($name, $value, $type);
}
}
}
public function getQueryBuilder(): QueryBuilder
{
return $this->qb;
}
}