1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* doctrine-mongodb-odm-repositories (https://github.com/juliangut/doctrine-mongodb-odm-repositories). |
5
|
|
|
* Doctrine2 MongoDB ODM utility entity repositories. |
6
|
|
|
* |
7
|
|
|
* @license MIT |
8
|
|
|
* @link https://github.com/juliangut/doctrine-mongodb-odm-repositories |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Jgut\Doctrine\Repository\MongoDB\ODM; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
17
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
18
|
|
|
use Doctrine\ODM\MongoDB\Repository\RepositoryFactory; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* MongoDB document repository factory. |
22
|
|
|
*/ |
23
|
|
|
class MongoDBRepositoryFactory implements RepositoryFactory |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Default repository class. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $repositoryClassName; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The list of EntityRepository instances. |
34
|
|
|
* |
35
|
|
|
* @var \Doctrine\Common\Persistence\ObjectRepository[] |
36
|
|
|
*/ |
37
|
|
|
private $repositoryList = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* RelationalRepositoryFactory constructor. |
41
|
|
|
*/ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
$this->repositoryClassName = MongoDBRepository::class; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function getRepository(DocumentManager $documentManager, $documentName): ObjectRepository |
51
|
|
|
{ |
52
|
|
|
$repositoryHash = |
53
|
|
|
$documentManager->getClassMetadata($documentName)->getName() . spl_object_hash($documentManager); |
54
|
|
|
|
55
|
|
|
if (array_key_exists($repositoryHash, $this->repositoryList)) { |
56
|
|
|
return $this->repositoryList[$repositoryHash]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->repositoryList[$repositoryHash] = $this->createRepository($documentManager, $documentName); |
60
|
|
|
|
61
|
|
|
return $this->repositoryList[$repositoryHash]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create a new repository instance for a document class. |
66
|
|
|
* |
67
|
|
|
* @param DocumentManager $documentManager |
68
|
|
|
* @param string $documentName |
69
|
|
|
* |
70
|
|
|
* @return ObjectRepository |
71
|
|
|
*/ |
72
|
|
|
private function createRepository(DocumentManager $documentManager, $documentName): ObjectRepository |
73
|
|
|
{ |
74
|
|
|
$metadata = $documentManager->getClassMetadata($documentName); |
75
|
|
|
$repositoryClassName = $metadata->customRepositoryClassName ?: $this->repositoryClassName; |
76
|
|
|
|
77
|
|
|
return new $repositoryClassName($documentManager, $documentManager->getUnitOfWork(), $metadata); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|