1 | <?php |
||
16 | abstract class AbstractRepositoryFactory implements RepositoryFactory |
||
17 | { |
||
18 | /** |
||
19 | * The list of DocumentRepository instances. |
||
20 | * |
||
21 | * @var ObjectRepository[] |
||
22 | */ |
||
23 | private $repositoryList = []; |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | 356 | public function getRepository(DocumentManager $documentManager, string $documentName) : ObjectRepository |
|
29 | { |
||
30 | 356 | $metadata = $documentManager->getClassMetadata($documentName); |
|
31 | 356 | $hashKey = $metadata->getName() . spl_object_hash($documentManager); |
|
|
|||
32 | |||
33 | 356 | if (isset($this->repositoryList[$hashKey])) { |
|
34 | 168 | return $this->repositoryList[$hashKey]; |
|
35 | } |
||
36 | |||
37 | 356 | $repository = $this->createRepository($documentManager, ltrim($documentName, '\\')); |
|
38 | |||
39 | 356 | $this->repositoryList[$hashKey] = $repository; |
|
40 | |||
41 | 356 | return $repository; |
|
42 | } |
||
43 | |||
44 | /** |
||
45 | * Create a new repository instance for a document class. |
||
46 | * |
||
47 | * @return ObjectRepository|GridFSRepository |
||
48 | */ |
||
49 | 356 | protected function createRepository(DocumentManager $documentManager, string $documentName) : ObjectRepository |
|
50 | { |
||
51 | 356 | $metadata = $documentManager->getClassMetadata($documentName); |
|
52 | |||
53 | 356 | if ($metadata->customRepositoryClassName) { |
|
54 | 13 | $repositoryClassName = $metadata->customRepositoryClassName; |
|
55 | 348 | } elseif ($metadata->isFile) { |
|
56 | 10 | $repositoryClassName = $documentManager->getConfiguration()->getDefaultGridFSRepositoryClassName(); |
|
57 | } else { |
||
58 | 339 | $repositoryClassName = $documentManager->getConfiguration()->getDefaultDocumentRepositoryClassName(); |
|
59 | } |
||
60 | |||
61 | 356 | return $this->instantiateRepository($repositoryClassName, $documentManager, $metadata); |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Instantiates requested repository. |
||
66 | */ |
||
67 | abstract protected function instantiateRepository(string $repositoryClassName, DocumentManager $documentManager, ClassMetadata $metadata) : ObjectRepository; |
||
68 | } |
||
69 |