forked from oroinc/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryHintResolver.php
166 lines (154 loc) · 5.24 KB
/
QueryHintResolver.php
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace Oro\Component\DoctrineUtils\ORM;
use Doctrine\ORM\Query;
class QueryHintResolver implements QueryHintResolverInterface
{
/** @var array */
protected $walkers = [];
/** @var string */
protected $aliases = [];
/**
* Maps a query hint to a tree walker
*
* @param string $hint The name of the query hint
* @param string $walkerClass The FQCN of the walker
* @param QueryWalkerHintProviderInterface|null $walkerHintProvider The provider of the walker parameters
* @param string|null $alias The alias of the query hint
*/
public function addTreeWalker(
$hint,
$walkerClass,
QueryWalkerHintProviderInterface $walkerHintProvider = null,
$alias = null
) {
$this->walkers[$hint] = [
'class' => $walkerClass,
'output' => false,
'hint_provider' => $walkerHintProvider
];
if ($alias) {
$this->aliases[$alias] = $hint;
}
}
/**
* Maps a query hint to an output walker
*
* @param string $hint The name of the query hint
* @param string $walkerClass The FQCN of the walker
* @param QueryWalkerHintProviderInterface|null $walkerHintProvider The provider of the walker parameters
* @param string|null $alias The alias of the query hint
*/
public function addOutputWalker(
$hint,
$walkerClass,
QueryWalkerHintProviderInterface $walkerHintProvider = null,
$alias = null
) {
$this->walkers[$hint] = [
'class' => $walkerClass,
'output' => true,
'hint_provider' => $walkerHintProvider
];
if ($alias) {
$this->aliases[$alias] = $hint;
}
}
/**
* Resolves query hints
*
* @param Query $query
* @param array $hints
*/
public function resolveHints(Query $query, array $hints = [])
{
if (!empty($hints)) {
$this->addHints($query, $hints);
}
foreach ($query->getHints() as $hintName => $hintVal) {
if (false !== $hintVal && isset($this->walkers[$hintName])) {
$walker = $this->walkers[$hintName];
$added = $this->addHint(
$query,
$walker['output'] ? Query::HINT_CUSTOM_OUTPUT_WALKER : Query::HINT_CUSTOM_TREE_WALKERS,
$walker['class']
);
if ($added && isset($walker['hint_provider'])) {
/** @var QueryWalkerHintProviderInterface $walkerHintProvider */
$walkerHintProvider = $walker['hint_provider'];
foreach ($walkerHintProvider->getHints($hintVal) as $walkerHint => $walkerHintVal) {
$this->addHint($query, $walkerHint, $walkerHintVal);
}
}
}
}
}
/**
* Adds a hint to a query object
*
* @param Query $query
* @param string $name
* @param mixed $value
*
* @return bool TRUE if the hint is added; otherwise, FALSE
*/
public function addHint(Query $query, $name, $value)
{
$result = false;
if ($name === Query::HINT_CUSTOM_TREE_WALKERS) {
$walkers = $query->getHint(Query::HINT_CUSTOM_TREE_WALKERS);
if (false === $walkers) {
$walkers = [$value];
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, $walkers);
$result = true;
} elseif (!in_array($value, $walkers, true)) {
$walkers[] = $value;
$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, $walkers);
$result = true;
}
} elseif ($name === Query::HINT_CUSTOM_OUTPUT_WALKER) {
if ($query->getHint($name) !== $value) {
$query->setHint($name, $value);
$result = true;
}
} else {
$query->setHint($name, $value);
$result = true;
}
return $result;
}
/**
* Adds hints to a query object
*
* @param Query $query
* @param array $hints
*/
public function addHints(Query $query, array $hints)
{
foreach ($hints as $hint) {
if (is_array($hint)) {
$this->addHint(
$query,
$this->resolveHintName($hint['name']),
isset($hint['value']) ? $hint['value'] : true
);
} elseif (is_string($hint)) {
$this->addHint($query, $this->resolveHintName($hint), true);
}
}
}
/**
* @param string $name
*
* @return string
*/
protected function resolveHintName($name)
{
if (isset($this->aliases[$name])) {
return $this->aliases[$name];
}
if (defined("Doctrine\\ORM\\Query::$name")) {
return constant("Doctrine\\ORM\\Query::$name");
}
return $name;
}
}