|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Subscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\EventSubscriber; |
|
6
|
|
|
use Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs; |
|
7
|
|
|
use Doctrine\ODM\PHPCR\Event; |
|
8
|
|
|
use Metadata\MetadataFactory; |
|
9
|
|
|
use Psi\Bridge\ContentType\Doctrine\PhpcrOdm\FieldMapper; |
|
10
|
|
|
use Psi\Component\ContentType\FieldLoader; |
|
11
|
|
|
|
|
12
|
|
|
class MetadataSubscriber implements EventSubscriber |
|
13
|
|
|
{ |
|
14
|
|
|
private $metadataFactory; |
|
15
|
|
|
private $fieldLoader; |
|
16
|
|
|
private $mapper; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct( |
|
19
|
|
|
MetadataFactory $metadataFactory, |
|
20
|
|
|
FieldLoader $fieldLoader, |
|
21
|
|
|
FieldMapper $mapper |
|
22
|
|
|
) { |
|
23
|
|
|
$this->metadataFactory = $metadataFactory; |
|
24
|
|
|
$this->fieldLoader = $fieldLoader; |
|
25
|
|
|
$this->mapper = $mapper; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getSubscribedEvents() |
|
29
|
|
|
{ |
|
30
|
|
|
return [ |
|
31
|
|
|
Event::loadClassMetadata, |
|
32
|
|
|
]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function loadClassMetadata(LoadClassMetadataEventArgs $args) |
|
36
|
|
|
{ |
|
37
|
|
|
$metadata = $args->getClassMetadata(); |
|
38
|
|
|
|
|
39
|
|
|
if (null === $metadata = $this->metadataFactory->getMetadataForClass($metadata->getName())) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$odmMetadata = $args->getClassMetadata(); |
|
44
|
|
|
|
|
45
|
|
|
foreach ($metadata->getPropertyMetadata() as $property) { |
|
46
|
|
|
try { |
|
47
|
|
|
$field = $this->fieldLoader->load($property->getType(), $property->getOptions()); |
|
48
|
|
|
$this->mapper->__invoke($property->getName(), $field, $odmMetadata); |
|
|
|
|
|
|
49
|
|
|
} catch (\Exception $e) { |
|
50
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
51
|
|
|
'Could not map field type "%s" on property "%s#%s"', |
|
52
|
|
|
$property->getType(), $property->getClass(), $property->getName() |
|
53
|
|
|
), null, $e); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
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.