1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015 Fabian Grutschus. All rights reserved. |
5
|
|
|
* |
6
|
|
|
* Redistribution and use in source and binary forms, with or without modification, |
7
|
|
|
* are permitted provided that the following conditions are met: |
8
|
|
|
* |
9
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this |
10
|
|
|
* list of conditions and the following disclaimer. |
11
|
|
|
* |
12
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, |
13
|
|
|
* this list of conditions and the following disclaimer in the documentation |
14
|
|
|
* and/or other materials provided with the distribution. |
15
|
|
|
* |
16
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
17
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
20
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
22
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
23
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
25
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26
|
|
|
* |
27
|
|
|
* The views and conclusions contained in the software and documentation are those |
28
|
|
|
* of the authors and should not be interpreted as representing official policies, |
29
|
|
|
* either expressed or implied, of the copyright holders. |
30
|
|
|
* |
31
|
|
|
* @author Fabian Grutschus <[email protected]> |
32
|
|
|
* @copyright 2015 Fabian Grutschus. All rights reserved. |
33
|
|
|
* @license BSD-2-Clause |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
namespace Fabiang\DoctrineDynamic; |
37
|
|
|
|
38
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
39
|
|
|
use Traversable; |
40
|
|
|
use Fabiang\DoctrineDynamic\Exception\InvalidArgumentException; |
41
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain; |
42
|
|
|
use Fabiang\DoctrineDynamic\Exception\RuntimeException; |
43
|
|
|
use Fabiang\DoctrineDynamic\Mapper\MetadataMapper; |
44
|
|
|
|
45
|
|
|
class ProxyDriverFactory |
46
|
|
|
{ |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var ConfigurationFactory |
50
|
|
|
*/ |
51
|
|
|
private $configurationFactory; |
52
|
|
|
|
53
|
1 |
|
public function __construct() |
54
|
|
|
{ |
55
|
1 |
|
$this->configurationFactory = new ConfigurationFactory; |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param EntityManagerInterface $entityManager |
60
|
|
|
* @param array|Traversable|Configuration $configuration |
61
|
|
|
* @return ProxyDriver[] |
62
|
|
|
* @throws InvalidArgumentException |
63
|
|
|
* @throws RuntimeException |
64
|
|
|
*/ |
65
|
4 |
|
public function factory(EntityManagerInterface $entityManager, $configuration) |
66
|
|
|
{ |
67
|
4 |
|
if (is_array($configuration) || $configuration instanceof Traversable) { |
68
|
1 |
|
$configuration = $this->configurationFactory->factory($configuration); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
4 |
|
if (!($configuration instanceof Configuration)) { |
72
|
1 |
|
throw new InvalidArgumentException(sprintf( |
73
|
1 |
|
'Argument #1 passed to "%s" must be an instance of "%s", "%s" or an array, "%s" given', |
74
|
1 |
|
__METHOD__, |
75
|
1 |
|
Configuration::class, |
76
|
1 |
|
Traversable::class, |
77
|
1 |
|
is_object($configuration) ? get_class($configuration) : gettype($configuration) |
78
|
1 |
|
)); |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
$doctrineConfig = $entityManager->getConfiguration(); |
82
|
|
|
|
83
|
3 |
|
$driverImplementation = $doctrineConfig->getMetadataDriverImpl(); |
84
|
3 |
|
if (!($driverImplementation instanceof MappingDriverChain)) { |
85
|
1 |
|
throw new RuntimeException(sprintf( |
86
|
1 |
|
'Driver implementation is no instance of "%s", instance of "%s" given', |
87
|
1 |
|
MappingDriverChain::class, |
88
|
1 |
|
get_class($driverImplementation) |
89
|
1 |
|
)); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
$metadataMapper = new MetadataMapper(); |
93
|
|
|
|
94
|
2 |
|
$drivers = $driverImplementation->getDrivers(); |
95
|
2 |
|
$proxyDrivers = []; |
96
|
2 |
|
foreach ($drivers as $namespace => $originalDriver) { |
97
|
2 |
|
$proxyDriver = new ProxyDriver( |
98
|
2 |
|
$originalDriver, |
99
|
2 |
|
$configuration, |
100
|
|
|
$metadataMapper |
101
|
2 |
|
); |
102
|
2 |
|
$driverImplementation->addDriver($proxyDriver, $namespace); |
103
|
2 |
|
$proxyDrivers[$namespace] = $proxyDriver; |
104
|
2 |
|
} |
105
|
2 |
|
return $proxyDrivers; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|