|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Sulu. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sulu\Component\DocumentManager\Query; |
|
13
|
|
|
|
|
14
|
|
|
use PHPCR\Query\QueryInterface; |
|
15
|
|
|
use Sulu\Component\DocumentManager\Event\QueryExecuteEvent; |
|
16
|
|
|
use Sulu\Component\DocumentManager\Events; |
|
17
|
|
|
use Sulu\Component\DocumentManager\Exception\DocumentManagerException; |
|
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Based heavily on the PHPCR-ODM Query object. |
|
22
|
|
|
* |
|
23
|
|
|
* If we can break the phpcrQuery builder from the PHPCR-ODM we should |
|
24
|
|
|
* also be able to break-out the PhpcrQuery object too: |
|
25
|
|
|
* |
|
26
|
|
|
* https://github.com/doctrine/phpcr-odm/issues/627 |
|
27
|
|
|
*/ |
|
28
|
|
|
class Query |
|
29
|
|
|
{ |
|
30
|
|
|
const HYDRATE_DOCUMENT = 'document'; |
|
31
|
|
|
const HYDRATE_PHPCR = 'phpcr_node'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var QueryInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $phpcrQuery; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var EventDispatcherInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $dispatcher; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var null|string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $primarySelector; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var null|string |
|
50
|
|
|
*/ |
|
51
|
|
|
private $locale; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
private $options; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var int |
|
60
|
|
|
*/ |
|
61
|
|
|
private $maxResults; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var int |
|
65
|
|
|
*/ |
|
66
|
|
|
private $firstResult; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param QueryInterface $phpcrQuery |
|
70
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
71
|
|
|
* @param null|string $locale |
|
72
|
|
|
* @param array $options |
|
73
|
|
|
* @param null|string $primarySelector |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __construct( |
|
76
|
|
|
QueryInterface $phpcrQuery, |
|
77
|
|
|
EventDispatcherInterface $dispatcher, |
|
78
|
|
|
$locale = null, |
|
79
|
|
|
array $options = [], |
|
80
|
|
|
$primarySelector = null |
|
81
|
|
|
) { |
|
82
|
|
|
$this->phpcrQuery = $phpcrQuery; |
|
83
|
|
|
$this->dispatcher = $dispatcher; |
|
84
|
|
|
$this->locale = $locale; |
|
85
|
|
|
$this->options = $options; |
|
86
|
|
|
$this->primarySelector = $primarySelector; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param array $parameters |
|
91
|
|
|
* @param string $hydrationMode |
|
92
|
|
|
* |
|
93
|
|
|
* @return mixed|\PHPCR\Query\QueryResultInterface |
|
94
|
|
|
* |
|
95
|
|
|
* @throws DocumentManagerException |
|
96
|
|
|
*/ |
|
97
|
|
|
public function execute(array $parameters = [], $hydrationMode = self::HYDRATE_DOCUMENT) |
|
98
|
|
|
{ |
|
99
|
|
|
if (null !== $this->maxResults) { |
|
100
|
|
|
$this->phpcrQuery->setLimit($this->maxResults); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (null !== $this->firstResult) { |
|
104
|
|
|
$this->phpcrQuery->setOffset($this->firstResult); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
foreach ($parameters as $key => $value) { |
|
108
|
|
|
$this->phpcrQuery->bindValue($key, $value); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if ($hydrationMode === self::HYDRATE_PHPCR) { |
|
112
|
|
|
return $this->phpcrQuery->execute(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if ($hydrationMode !== self::HYDRATE_DOCUMENT) { |
|
116
|
|
|
throw new DocumentManagerException(sprintf( |
|
117
|
|
|
'Unknown hydration mode "%s", should be either "document" or "phpcr_node"', |
|
118
|
|
|
$hydrationMode |
|
119
|
|
|
)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$event = new QueryExecuteEvent($this, $this->options); |
|
123
|
|
|
$this->dispatcher->dispatch(Events::QUERY_EXECUTE, $event); |
|
124
|
|
|
|
|
125
|
|
|
return $event->getResult(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return int |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getMaxResults() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->maxResults; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param int $maxResults |
|
138
|
|
|
*/ |
|
139
|
|
|
public function setMaxResults($maxResults) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->maxResults = $maxResults; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return int |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getFirstResult() |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->firstResult; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param int $firstResult |
|
154
|
|
|
*/ |
|
155
|
|
|
public function setFirstResult($firstResult) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->firstResult = $firstResult; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return null|string |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getLocale() |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->locale; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string $locale |
|
170
|
|
|
*/ |
|
171
|
|
|
public function setLocale($locale) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->locale = $locale; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return null|string |
|
178
|
|
|
*/ |
|
179
|
|
|
public function getPrimarySelector() |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->primarySelector; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param string $primarySelector |
|
186
|
|
|
*/ |
|
187
|
|
|
public function setPrimarySelector($primarySelector) |
|
188
|
|
|
{ |
|
189
|
|
|
$this->primarySelector = $primarySelector; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @return QueryInterface |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getPhpcrQuery() |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->phpcrQuery; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|