Completed
Push — master ( 707688...6da863 )
by Mike
04:47 queued 02:08
created

lib/Doctrine/ODM/CouchDB/DocumentManager.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ODM\CouchDB;
21
22
use Doctrine\Common\EventManager;
23
use Doctrine\Common\Persistence\ObjectManager;
24
use Doctrine\CouchDB\CouchDBClient;
25
use Doctrine\CouchDB\View;
26
use Doctrine\CouchDB\View\Query;
27
use Doctrine\ODM\CouchDB\Mapping\ClassMetadataFactory;
28
use Doctrine\ODM\CouchDB\View\ODMQuery;
29
use Doctrine\ODM\CouchDB\View\ODMLuceneQuery;
30
use Doctrine\ODM\CouchDB\CouchDBException;
31
32
class DocumentManager implements ObjectManager
33
{
34
    /**
35
     * @var Configuration
36
     */
37
    private $config;
38
39
    /**
40
     * @var Mapping\ClassMetadataFactory
41
     */
42
    private $metadataFactory;
43
44
    /**
45
     * @var UnitOfWork
46
     */
47
    private $unitOfWork = null;
48
49
    /**
50
     * @var \Doctrine\ODM\CouchDB\Proxy\ProxyFactory
51
     */
52
    private $proxyFactory = null;
53
54
    /**
55
     * @var array
56
     */
57
    private $repositories = array();
58
59
    /**
60
     * @var CouchDBClient
61
     */
62
    private $couchDBClient = null;
63
64
    /**
65
     * @var EventManager
66
     */
67
    private $evm;
68
69
    /**
70
     * @param CouchDBClient $couchClient
71
     * @param Configuration $config
72
     * @param EventManager $evm
73
     */
74
    public function __construct(CouchDBClient $couchClient, Configuration $config = null, EventManager $evm = null)
75
    {
76
        $this->couchDBClient = $couchClient;
77
        $this->config = $config ?: new Configuration();
78
        $this->evm = $evm ?: new EventManager();
79
        $this->metadataFactory = new ClassMetadataFactory($this);
80
        $this->unitOfWork = new UnitOfWork($this);
81
        $this->proxyFactory = new Proxy\ProxyFactory($this, $this->config->getProxyDir(), $this->config->getProxyNamespace(), $this->config->getAutoGenerateProxyClasses());
82
    }
83
84
    /**
85
     * @return EventManager
86
     */
87
    public function getEventManager()
88
    {
89
        return $this->evm;
90
    }
91
92
    /**
93
     * @return CouchDBClient
94
     */
95
    public function getCouchDBClient()
96
    {
97
        return $this->couchDBClient;
98
    }
99
100
    /**
101
     * Factory method for a Document Manager.
102
     *
103
     * @param array|CouchDBClient $couchParams
104
     * @param Configuration       $config
105
     * @param EventManager        $evm
106
     * @return DocumentManager
107
     * @throws \InvalidArgumentException
108
     */
109
    public static function create($couchParams, Configuration $config = null, EventManager $evm = null)
110
    {
111
        if (is_array($couchParams)) {
112
            $couchClient = CouchDBClient::create($couchParams);
113
        } else if ($couchParams instanceof CouchDBClient) {
114
            $couchClient = $couchParams;
115
        } else {
116
            throw new \InvalidArgumentException("Expecting array of instance of CouchDBClient as first argument to DocumentManager::create().");
117
        }
118
119
        return new DocumentManager($couchClient, $config, $evm);
120
    }
121
122
    /**
123
     * @return ClassMetadataFactory
124
     */
125
    public function getMetadataFactory()
126
    {
127
        return $this->metadataFactory;
128
    }
129
130
    /**
131
     * @return Proxy\ProxyFactory
132
     */
133
    public function getProxyFactory()
134
    {
135
        return $this->proxyFactory;
136
    }
137
138
    public function getHttpClient()
139
    {
140
        return $this->couchDBClient->getHttpClient();
141
    }
142
143
    public function getDatabase()
144
    {
145
        return $this->couchDBClient->getDatabase();
146
    }
147
148
    /**
149
     * @return Configuration
150
     */
151
    public function getConfiguration()
152
    {
153
        return $this->config;
154
    }
155
156
    /**
157
     * @return ClassMetadataFactory
158
     */
159
    public function getClassMetadataFactory()
160
    {
161
        return $this->metadataFactory;
162
    }
163
164
    /**
165
     * @param  string $class
166
     * @return \Doctrine\ODM\CouchDB\Mapping\ClassMetadata
167
     */
168
    public function getClassMetadata($class)
169
    {
170
        return $this->metadataFactory->getMetadataFor($class);
171
    }
172
173
    /**
174
     * Find the Document with the given id.
175
     *
176
     * Will return null if the document wasn't found.
177
     *
178
     * @param string $documentName
179
     * @param string $id
180
     * @return object
181
     */
182
    public function find($documentName, $id)
183
    {
184
        return $this->getRepository($documentName)->find($id);
185
    }
186
187
    /**
188
     * @param  string $documentName
189
     * @return \Doctrine\ODM\CouchDB\DocumentRepository
190
     */
191
    public function getRepository($documentName)
192
    {
193
        $documentName  = ltrim($documentName, '\\');
194
        if (!isset($this->repositories[$documentName])) {
195
            $class = $this->getClassMetadata($documentName);
196
            if ($class->customRepositoryClassName) {
197
                $repositoryClass = $class->customRepositoryClassName;
198
            } else {
199
                $repositoryClass = 'Doctrine\ODM\CouchDB\DocumentRepository';
200
            }
201
            $this->repositories[$documentName] = new $repositoryClass($this, $class);
202
        }
203
        return $this->repositories[$documentName];
204
    }
205
206
    /**
207
     * Create a Query for the view in the specified design document.
208
     *
209
     * @param  string $designDocName
210
     * @param  string $viewName
211
     * @return \Doctrine\ODM\CouchDB\View\ODMQuery
212
     */
213 View Code Duplication
    public function createQuery($designDocName, $viewName)
214
    {
215
        $designDoc = $this->config->getDesignDocument($designDocName);
216
        if ($designDoc) {
217
            $designDoc = new $designDoc['className']($designDoc['options']);
218
        }
219
        $query = new ODMQuery($this->couchDBClient->getHttpClient(), $this->couchDBClient->getDatabase(), $designDocName, $viewName, $designDoc);
220
        $query->setDocumentManager($this);
221
        return $query;
222
    }
223
224
    /**
225
     * Create a Native query for the view of the specified design document.
226
     *
227
     * A native query will return an array of data from the &include_docs=true parameter.
228
     *
229
     * @param  string $designDocName
230
     * @param  string $viewName
231
     * @return \Doctrine\CouchDB\View\Query
232
     */
233 View Code Duplication
    public function createNativeQuery($designDocName, $viewName)
234
    {
235
        $designDoc = $this->config->getDesignDocument($designDocName);
236
        if ($designDoc) {
237
            $designDoc = new $designDoc['className']($designDoc['options']);
238
        }
239
        $query = new Query($this->couchDBClient->getHttpClient(), $this->couchDBClient->getDatabase(), $designDocName, $viewName, $designDoc);
240
        return $query;
241
    }
242
243
    /**
244
     * Create a CouchDB-Lucene Query.
245
     *
246
     * @param string $designDocName
247
     * @param string $viewName
248
     * @return \Doctrine\ODM\CouchDB\View\ODMLuceneQuery
249
     */
250 View Code Duplication
    public function createLuceneQuery($designDocName, $viewName)
251
    {
252
        $luceneHandlerName = $this->config->getLuceneHandlerName();
253
        $designDoc = $this->config->getDesignDocument($designDocName);
254
        if ($designDoc) {
255
            $designDoc = new $designDoc['className']($designDoc['options']);
256
        }
257
        $query = new ODMLuceneQuery($this->couchDBClient->getHttpClient(),
258
            $this->couchDBClient->getDatabase(), $luceneHandlerName, $designDocName,
259
            $viewName, $designDoc
260
        );
261
        $query->setDocumentManager($this);
262
        return $query;
263
    }
264
265
    public function persist($object)
266
    {
267
        $this->unitOfWork->scheduleInsert($object);
268
    }
269
270
    public function remove($object)
271
    {
272
        $this->unitOfWork->scheduleRemove($object);
273
    }
274
275
    /**
276
     * Refresh the given document by querying the CouchDB to get the current state.
277
     *
278
     * @param object $document
279
     */
280
    public function refresh($document)
281
    {
282
        $this->getUnitOfWork()->refresh($document);
283
    }
284
285
    public function merge($document)
286
    {
287
        return $this->getUnitOfWork()->merge($document);
288
    }
289
290
    public function detach($document)
291
    {
292
        $this->getUnitOfWork()->detach($document);
293
    }
294
295
    /**
296
     * Gets a reference to the entity identified by the given type and identifier
297
     * without actually loading it, if the entity is not yet loaded.
298
     *
299
     * @param string $documentName The name of the entity type.
300
     * @param mixed $identifier The entity identifier.
301
     * @return object The entity reference.
302
     */
303
    public function getReference($documentName, $identifier)
304
    {
305
        $class = $this->metadataFactory->getMetadataFor(ltrim($documentName, '\\'));
306
307
        // Check identity map first, if its already in there just return it.
308
        if ($document = $this->unitOfWork->tryGetById($identifier)) {
309
            return $document;
310
        }
311
        $document = $this->proxyFactory->getProxy($class->name, $identifier);
312
        $this->unitOfWork->registerManaged($document, $identifier, null);
313
314
        return $document;
315
    }
316
317
    public function flush()
318
    {
319
        $this->unitOfWork->flush(); // todo: rename commit
320
    }
321
322
    /**
323
     * @param  object $document
324
     * @return bool
325
     */
326
    public function contains($document)
327
    {
328
        return $this->unitOfWork->contains($document);
329
    }
330
331
    /**
332
     * @return UnitOfWork
333
     */
334
    public function getUnitOfWork()
335
    {
336
        return $this->unitOfWork;
337
    }
338
339
    /**
340
     * Clears the ObjectManager. All objects that are currently managed
341
     * by this ObjectManager become detached.
342
     *
343
     * @param string $objectName if given, only objects of this type will get detached
344
     * @throws CouchDBException
345
     */
346
    public function clear($objectName = null)
347
    {
348
        if ($objectName === null) {
349
            // Todo: Do a real delegated clear?
350
            $this->unitOfWork = new UnitOfWork($this);
351
        } else {
352
            //TODO
353
            throw new CouchDBException("DocumentManager#clear(\$objectName) not yet implemented.");
354
        }
355
    }
356
357
    /**
358
     * Initialize an object that is a lazy load proxy, or do nothing.
359
     *
360
     * @param object $obj
361
     */
362
    public function initializeObject($obj)
363
    {
364
        if ($obj instanceof PersistentCollection) {
365
            $obj->initialize();
366
        } else if ($obj instanceof Proxy\Proxy) {
367
            $obj->__doctrineLoad__();
0 ignored issues
show
The method __doctrineLoad__() does not seem to exist on object<Doctrine\ODM\CouchDB\Proxy\Proxy>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
368
        }
369
    }
370
}
371