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\Doctrine\ODM\MongoDB\Metadata\Factory; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Metadata\Driver\ChainDriver; |
15
|
|
|
use Cubiche\Core\Metadata\Driver\DriverInterface; |
16
|
|
|
use Cubiche\Core\Metadata\Driver\MergeableDriver; |
17
|
|
|
use Cubiche\Core\Metadata\Factory\DriverFactory as BaseDriverFactory; |
18
|
|
|
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Locator\DoctrineAdapterLocator; |
19
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver; |
20
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\FileDriver; |
21
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
22
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain; |
23
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* DriverFactory class. |
27
|
|
|
* |
28
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class DriverFactory extends BaseDriverFactory |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Create a driver from a Doctrine object manager. |
34
|
|
|
* |
35
|
|
|
* @param ObjectManager $om |
36
|
|
|
* |
37
|
|
|
* @return DriverInterface |
38
|
|
|
*/ |
39
|
|
|
public static function driversFromManager(ObjectManager $om) |
40
|
|
|
{ |
41
|
|
|
return self::driversFromMetadataDriver($om->getConfiguration()->getMetadataDriverImpl()); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a driver from a Doctrine metadata driver. |
46
|
|
|
* |
47
|
|
|
* @param MappingDriver $omDriver |
48
|
|
|
* |
49
|
|
|
* @return DriverInterface |
50
|
|
|
*/ |
51
|
|
|
public static function driversFromMetadataDriver(MappingDriver $omDriver) |
52
|
|
|
{ |
53
|
|
|
return static::instance()->fromMetadataDriver($omDriver); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Create the cubiche drivers from the Doctrine metadata drivers. |
58
|
|
|
* |
59
|
|
|
* @param MappingDriver $omDriver |
60
|
|
|
* |
61
|
|
|
* @return DriverInterface |
62
|
|
|
*/ |
63
|
|
|
protected function fromMetadataDriver(MappingDriver $omDriver) |
64
|
|
|
{ |
65
|
|
|
if ($omDriver instanceof MappingDriverChain) { |
66
|
|
|
$drivers = array(); |
67
|
|
|
foreach ($omDriver->getDrivers() as $nestedOmDriver) { |
68
|
|
|
$drivers[] = $this->fromMetadataDriver($nestedOmDriver); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return new ChainDriver($drivers); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($omDriver instanceof AnnotationDriver) { |
75
|
|
|
return new MergeableDriver( |
76
|
|
|
$this->createAnnotationDriver($omDriver->getReader()) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($omDriver instanceof FileDriver) { |
81
|
|
|
$reflClass = new \ReflectionClass($omDriver); |
82
|
|
|
|
83
|
|
|
$driverName = $reflClass->getShortName(); |
84
|
|
|
if (strpos($driverName, 'Simplified') !== false) { |
85
|
|
|
$driverName = str_replace('Simplified', '', $driverName); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return new MergeableDriver( |
89
|
|
|
call_user_func( |
90
|
|
|
array(static::class, 'create'.$driverName), |
91
|
|
|
new DoctrineAdapterLocator($omDriver->getLocator()) |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
throw new \InvalidArgumentException('Cannot adapt Doctrine driver of class '.get_class($omDriver)); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: