1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Mapping; |
12
|
|
|
|
13
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory as BaseClassMetadataFactory; |
14
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
15
|
|
|
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Events; |
16
|
|
|
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Event\PreLoadClassMetadataEventArgs; |
17
|
|
|
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* ClassMetadata Factory Class. |
21
|
|
|
* |
22
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class ClassMetadataFactory extends BaseClassMetadataFactory |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ClassMetadata[] |
28
|
|
|
*/ |
29
|
|
|
private $innerLoadedMetadata = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var DocumentManager |
33
|
|
|
*/ |
34
|
|
|
private $documentManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function getMetadataFor($className) |
40
|
|
|
{ |
41
|
|
|
if (!isset($this->innerLoadedMetadata[$className])) { |
42
|
|
|
$evm = $this->documentManager->getEventManager(); |
43
|
|
View Code Duplication |
if ($evm->hasListeners(Events::PRE_LOAD_CLASSMETADATA)) { |
|
|
|
|
44
|
|
|
$eventArgs = new PreLoadClassMetadataEventArgs($className, $this->documentManager); |
45
|
|
|
$evm->dispatchEvent(Events::PRE_LOAD_CLASSMETADATA, $eventArgs); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$classMetadata = parent::getMetadataFor($className); |
49
|
|
|
|
50
|
|
View Code Duplication |
if ($evm->hasListeners(Events::POST_LOAD_CLASS_METADATA)) { |
|
|
|
|
51
|
|
|
$eventArgs = new LoadClassMetadataEventArgs($classMetadata, $this->documentManager); |
52
|
|
|
$evm->dispatchEvent(Events::POST_LOAD_CLASS_METADATA, $eventArgs); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->innerLoadedMetadata[$className] = $classMetadata; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->innerLoadedMetadata[$className]; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function setDocumentManager(DocumentManager $dm) |
65
|
|
|
{ |
66
|
|
|
parent::setDocumentManager($dm); |
67
|
|
|
$this->documentManager = $dm; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.