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
|
|
|
|
12
|
|
|
namespace Cubiche\Infrastructure\Identity\Doctrine\ODM\MongoDB\EventListener; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Metadata\PropertyMetadata; |
15
|
|
|
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Event\RegisterDriverMetadataEventArgs; |
16
|
|
|
use Cubiche\Infrastructure\Identity\Doctrine\ODM\MongoDB\Metadata\Driver\XmlDriver; |
17
|
|
|
use Cubiche\Infrastructure\Identity\Doctrine\ODM\MongoDB\Types\DynamicIdType; |
18
|
|
|
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs; |
19
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
20
|
|
|
use Doctrine\ODM\MongoDB\Mapping\MappingException; |
21
|
|
|
use Doctrine\ODM\MongoDB\Types\Type; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Event Listener Class. |
25
|
|
|
* |
26
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
27
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class EventListener |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @param RegisterDriverMetadataEventArgs $eventArgs |
33
|
|
|
*/ |
34
|
|
|
public function registerDriverMetadata(RegisterDriverMetadataEventArgs $eventArgs) |
35
|
|
|
{ |
36
|
|
|
$eventArgs->driverFactory()->registerXmlDriver(XmlDriver::class); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param LoadClassMetadataEventArgs $eventArgs |
41
|
|
|
*/ |
42
|
|
|
public function postLoadClassMetadata(LoadClassMetadataEventArgs $eventArgs) |
43
|
|
|
{ |
44
|
|
|
$this->checkIdType($eventArgs->getClassMetadata()); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param ClassMetadata $classMetadata |
49
|
|
|
* |
50
|
|
|
* @throws MappingException |
51
|
|
|
*/ |
52
|
|
|
protected function checkIdType(ClassMetadata $classMetadata) |
53
|
|
|
{ |
54
|
|
|
foreach ($classMetadata->fieldMappings as $fieldName => $mapping) { |
55
|
|
|
if (isset($mapping['cubiche:id'])) { |
56
|
|
|
/** @var PropertyMetadata $propertyMetadata */ |
57
|
|
|
$propertyMetadata = $mapping['cubiche:id']; |
58
|
|
|
|
59
|
|
|
$type = str_replace('\\', '.', $propertyMetadata->getMetadata('type')); |
60
|
|
|
if (!Type::hasType($type)) { |
61
|
|
|
Type::registerType($type, DynamicIdType::class); |
62
|
|
|
Type::getType($type)->setTargetClass($propertyMetadata->getMetadata('type')); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$classMetadata->fieldMappings[$fieldName]['type'] = $type; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.