|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Cubiche package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Cubiche |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Cubiche\Infrastructure\Doctrine\ODM\MongoDB\EventListener; |
|
12
|
|
|
|
|
13
|
|
|
use Cubiche\Core\Metadata\ClassMetadataFactory; |
|
14
|
|
|
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Event\RegisterDriverMetadataEventArgs; |
|
15
|
|
|
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Events; |
|
16
|
|
|
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Factory\DriverFactory; |
|
17
|
|
|
use Doctrine\Common\EventManager; |
|
18
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
19
|
|
|
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* MetadataEventListener class. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class MetadataEventListener |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ClassMetadataFactory |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $metadataFactory; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Add mapping to translatable entities. |
|
35
|
|
|
* |
|
36
|
|
|
* @param LoadClassMetadataEventArgs $eventArgs |
|
37
|
|
|
*/ |
|
38
|
|
|
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) |
|
39
|
|
|
{ |
|
40
|
|
|
$classMetadata = $eventArgs->getClassMetadata(); |
|
41
|
|
|
$reflClass = $classMetadata->reflClass; |
|
|
|
|
|
|
42
|
|
|
if (!$reflClass || $reflClass->isAbstract()) { |
|
43
|
|
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$cubicheClassMetadata = $this |
|
47
|
|
|
->getMetadataFactory( |
|
48
|
|
|
$eventArgs->getDocumentManager(), |
|
49
|
|
|
$eventArgs->getDocumentManager()->getEventManager() |
|
50
|
|
|
)->getMetadataFor($classMetadata->getName()) |
|
51
|
|
|
; |
|
52
|
|
|
|
|
53
|
|
|
foreach ($classMetadata->fieldMappings as $fieldName => &$mapping) { |
|
|
|
|
|
|
54
|
|
|
if ($cubicheClassMetadata !== null) { |
|
55
|
|
|
$propertyMetadata = $cubicheClassMetadata->propertyMetadata($fieldName); |
|
56
|
|
|
if ($propertyMetadata !== null) { |
|
57
|
|
|
$mapping['cubiche:'.$propertyMetadata->getMetadata('namespace')] = $propertyMetadata; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param ObjectManager $om |
|
65
|
|
|
* |
|
66
|
|
|
* @return ClassMetadataFactory |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function getMetadataFactory(ObjectManager $om, EventManager $evm) |
|
69
|
|
|
{ |
|
70
|
|
|
if ($this->metadataFactory === null) { |
|
71
|
|
|
$driverFactory = DriverFactory::instance(); |
|
72
|
|
|
|
|
73
|
|
|
if ($evm->hasListeners(Events::REGISTER_DRIVER_METADATA)) { |
|
74
|
|
|
$eventArgs = new RegisterDriverMetadataEventArgs($driverFactory, $om); |
|
75
|
|
|
$evm->dispatchEvent(Events::REGISTER_DRIVER_METADATA, $eventArgs); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->metadataFactory = new ClassMetadataFactory($driverFactory->driversFromManager($om)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->metadataFactory; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: