This repository has been archived by the owner on Apr 22, 2022. It is now read-only.
/
SearchManager.php
executable file
·322 lines (284 loc) · 8.12 KB
/
SearchManager.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Search;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Search\SearchClientInterface;
use Doctrine\Search\Configuration;
use Doctrine\Search\Exception\UnexpectedTypeException;
use Doctrine\Search\EntityRepository;
use Doctrine\Search\UnitOfWork;
use Doctrine\Search\Query;
use Doctrine\Common\EventManager;
/**
* Interface for a Doctrine SearchManager class to implement.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Mike Lohmann <mike.h.lohmann@googlemail.com>
*/
class SearchManager implements ObjectManager
{
/**
* @var SearchClientInterface
*/
private $client;
/**
* @var Configuration $configuration
*/
private $configuration;
/**
* @var Doctrine\Search\Mapping\ClassMetadataFactory
*/
private $metadataFactory;
/**
* @var SerializerInterface
*/
private $serializer;
/**
* @var ObjectManager
*/
private $entityManager;
/**
* The event manager that is the central point of the event system.
*
* @var \Doctrine\Common\EventManager
*/
private $eventManager;
/**
* The EntityRepository instances.
*
* @var array
*/
private $repositories = array();
/**
* The UnitOfWork used to coordinate object-level transactions.
*
* @var \Doctrine\Search\UnitOfWork
*/
private $unitOfWork;
/**
* Constructor
*
* @param Configuration $config
* @param SearchClientInterface $client
*/
public function __construct(Configuration $config, SearchClientInterface $client, EventManager $eventManager)
{
$this->configuration = $config;
$this->client = $client;
$this->eventManager = $eventManager;
$this->metadataFactory = $this->configuration->getClassMetadataFactory();
$this->metadataFactory->setSearchManager($this);
$this->metadataFactory->setConfiguration($this->configuration);
$this->metadataFactory->setCacheDriver($this->configuration->getMetadataCacheImpl());
$this->serializer = $this->configuration->getEntitySerializer();
$this->entityManager = $this->configuration->getEntityManager();
$this->unitOfWork = new UnitOfWork($this);
}
/**
* Inject a Doctrine 2 object manager
*
* @param ObjectManager $em
*/
public function setEntityManager(ObjectManager $om)
{
$this->entityManager = $om;
}
/**
* @return EntityManager
*/
public function getEntityManager()
{
return $this->entityManager;
}
/**
* @return Configuration
*/
public function getConfiguration()
{
return $this->configuration;
}
/**
* Gets the UnitOfWork used by the SearchManager to coordinate operations.
*
* @return \Doctrine\Search\UnitOfWork
*/
public function getUnitOfWork()
{
return $this->unitOfWork;
}
/**
* Gets the EventManager used by the SearchManager.
*
* @return \Doctrine\Common\EventManager
*/
public function getEventManager()
{
return $this->eventManager;
}
/**
* Loads class metadata for the given class
*
* @param string $className
*
* @return Doctrine\Search\Mapping\ClassMetadata
*/
public function getClassMetadata($className)
{
return $this->metadataFactory->getMetadataFor($className);
}
/**
* @return SearchClientInterface
*/
public function getClient()
{
return $this->client;
}
/**
* @return SerializerInterface
*/
public function getSerializer()
{
return $this->serializer;
}
/**
* @return Doctrine\Search\Mapping\ClassMetadataFactory
*/
public function getMetadataFactory()
{
return $this->metadataFactory;
}
/**
* {@inheritDoc}
*/
public function find($entityName, $id)
{
$class = $this->getClassMetadata($entityName);
return $this->unitOfWork->load($class, $id);
}
/**
* Adds the object to the index
*
* @param array|object $objects
*
* @throws UnexpectedTypeException
*/
public function persist($objects)
{
if(!is_array($objects) && !$objects instanceof \Traversable) {
$objects = array($objects);
}
foreach($objects as $object) {
if (!is_object($object)) {
throw new UnexpectedTypeException($object, 'object');
}
$this->unitOfWork->persist($object);
}
}
/**
* Remove the object from the index
*
* @param array|object $objects
*
* @throws UnexpectedTypeException
*/
public function remove($objects)
{
if(!is_array($objects) && !$objects instanceof \Traversable) {
$objects = array($objects);
}
foreach($objects as $object) {
if (!is_object($object)) {
throw new UnexpectedTypeException($object, 'object');
}
$this->unitOfWork->remove($object);
}
}
/**
* Commit all changes
*/
public function flush($object = null)
{
$this->unitOfWork->commit($object);
}
/**
* Gets the repository for an entity class.
*
* @param string $entityName The name of the entity.
* @return EntityRepository The repository class.
*/
public function getRepository($entityName)
{
if (isset($this->repositories[$entityName])) {
return $this->repositories[$entityName];
}
$metadata = $this->getClassMetadata($entityName);
$repository = new EntityRepository($this, $metadata);
$this->repositories[$entityName] = $repository;
return $repository;
}
/**
* Gets a collection of entity repositories.
*
* @param array $entityNames The names of the entities.
* @return EntityRepositoryCollection The repository class.
*/
public function getRepositories(array $entityNames)
{
$repositoryCollection = new EntityRepositoryCollection($this);
foreach($entityNames as $entityName) {
$repositoryCollection->addRepository($this->getRepository($entityName));
}
return $repositoryCollection;
}
/**
* Returns a search engine Query wrapper which can be executed
* to retrieve results.
*
* @return Query
*/
public function createQuery()
{
return new Query($this);
}
public function initializeObject($obj)
{
}
public function contains($object)
{
}
public function merge($object)
{
}
/**
* Clears the SearchManager. All entities that are currently managed
* by this EntityManager become detached.
*
* @param string $objectName if given, only entities of this type will get detached
*/
public function clear($objectName = null)
{
$this->unitOfWork->clear($objectName);
}
public function detach($object)
{
}
public function refresh($object)
{
}
}