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\Subscriber\Phpcr; |
13
|
|
|
|
14
|
|
|
use PHPCR\Query\QueryInterface; |
15
|
|
|
use PHPCR\Query\QueryManagerInterface; |
16
|
|
|
use PHPCR\SessionInterface; |
17
|
|
|
use Sulu\Component\DocumentManager\Collection\QueryResultCollection; |
18
|
|
|
use Sulu\Component\DocumentManager\Event\QueryCreateBuilderEvent; |
19
|
|
|
use Sulu\Component\DocumentManager\Event\QueryCreateEvent; |
20
|
|
|
use Sulu\Component\DocumentManager\Event\QueryExecuteEvent; |
21
|
|
|
use Sulu\Component\DocumentManager\Events; |
22
|
|
|
use Sulu\Component\DocumentManager\Query\Query; |
23
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
24
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Handles creation of query and query builder objects. |
28
|
|
|
*/ |
29
|
|
|
class QuerySubscriber implements EventSubscriberInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var SessionInterface |
33
|
|
|
*/ |
34
|
|
|
private $session; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var EventDispatcherInterface |
38
|
|
|
*/ |
39
|
|
|
private $eventDispatcher; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param SessionInterface $session |
43
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
44
|
|
|
*/ |
45
|
|
|
public function __construct(SessionInterface $session, EventDispatcherInterface $eventDispatcher) |
46
|
|
|
{ |
47
|
|
|
$this->session = $session; |
48
|
|
|
$this->eventDispatcher = $eventDispatcher; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public static function getSubscribedEvents() |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
Events::QUERY_CREATE => ['handleCreate', 500], |
58
|
|
|
Events::QUERY_CREATE_BUILDER => ['handleCreateBuilder', 500], |
59
|
|
|
Events::QUERY_EXECUTE => ['handleQueryExecute', 500], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create a new Sulu Query object. |
65
|
|
|
* |
66
|
|
|
* @param QueryCreateEvent $event |
67
|
|
|
*/ |
68
|
|
|
public function handleCreate(QueryCreateEvent $event) |
69
|
|
|
{ |
70
|
|
|
$innerQuery = $event->getInnerQuery(); |
71
|
|
|
|
72
|
|
|
if (is_string($innerQuery)) { |
73
|
|
|
$phpcrQuery = $this->getQueryManager()->createQuery($innerQuery, QueryInterface::JCR_SQL2); |
74
|
|
|
} elseif ($innerQuery instanceof QueryInterface) { |
75
|
|
|
$phpcrQuery = $innerQuery; |
76
|
|
View Code Duplication |
} else { |
77
|
|
|
throw new \InvalidArgumentException(sprintf( |
78
|
|
|
'Expected inner query to be either a string or a PHPCR query object, got: "%s"', |
79
|
|
|
is_object($innerQuery) ? get_class($innerQuery) : gettype($innerQuery) |
80
|
|
|
)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$event->setQuery( |
84
|
|
|
new Query( |
85
|
|
|
$phpcrQuery, |
86
|
|
|
$this->eventDispatcher, |
87
|
|
|
$event->getLocale(), |
88
|
|
|
$event->getOptions(), |
|
|
|
|
89
|
|
|
$event->getPrimarySelector() |
90
|
|
|
) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* TODO: We should reuse the PHPCR-ODM query builder here, see: |
96
|
|
|
* https://github.com/doctrine/phpcr-odm/issues/627. |
97
|
|
|
* |
98
|
|
|
* @param QueryCreateBuilderEvent $event |
99
|
|
|
* |
100
|
|
|
* @throws \Exception |
101
|
|
|
*/ |
102
|
|
|
public function handleCreateBuilder(QueryCreateBuilderEvent $event) |
103
|
|
|
{ |
104
|
|
|
throw new \Exception('Not implemented'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Handle query execution. |
109
|
|
|
* |
110
|
|
|
* @param QueryExecuteEvent $event |
111
|
|
|
*/ |
112
|
|
|
public function handleQueryExecute(QueryExecuteEvent $event) |
113
|
|
|
{ |
114
|
|
|
$query = $event->getQuery(); |
115
|
|
|
$locale = $query->getLocale(); |
116
|
|
|
$phpcrResult = $query->getPhpcrQuery()->execute(); |
117
|
|
|
|
118
|
|
|
$event->setResult( |
119
|
|
|
new QueryResultCollection($phpcrResult, $this->eventDispatcher, $locale, $event->getOptions()) |
|
|
|
|
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return QueryManagerInterface |
125
|
|
|
*/ |
126
|
|
|
private function getQueryManager() |
127
|
|
|
{ |
128
|
|
|
return $this->session->getWorkspace()->getQueryManager(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: