|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alcalyn\PayplugBundle\DependencyInjection\Compiler; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; |
|
9
|
|
|
|
|
10
|
|
|
class RegisterMappingsPass implements CompilerPassInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Definition|Reference |
|
14
|
|
|
*/ |
|
15
|
|
|
private $driver; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $driverPattern; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $namespaces; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private $managerParameters; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var mixed false or parameter name |
|
34
|
|
|
*/ |
|
35
|
|
|
private $enabledParameter; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Definition $driver |
|
39
|
|
|
* @param string $driverPattern |
|
40
|
|
|
* @param array $namespaces |
|
41
|
|
|
* @param string[] $managerParameters |
|
42
|
|
|
* @param mixed $enabledParameter |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct($driver, $driverPattern, $namespaces, $managerParameters, $enabledParameter = false) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->driver = $driver; |
|
47
|
|
|
$this->driverPattern = $driverPattern; |
|
48
|
|
|
$this->namespaces = $namespaces; |
|
49
|
|
|
$this->managerParameters = $managerParameters; |
|
50
|
|
|
$this->enabledParameter = $enabledParameter; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param ContainerBuilder $container |
|
55
|
|
|
*/ |
|
56
|
|
|
public function process(ContainerBuilder $container) |
|
57
|
|
|
{ |
|
58
|
|
|
if ($this->enabledParameter && !$container->hasParameter($this->enabledParameter)) { |
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$chainDriverDefService = $this->getChainDriverServiceName($container); |
|
63
|
|
|
$chainDriverDef = $container->getDefinition($chainDriverDefService); |
|
64
|
|
|
foreach ($this->namespaces as $namespace) { |
|
65
|
|
|
$chainDriverDef->addMethodCall('addDriver', array($this->driver, $namespace)); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param ContainerBuilder $container |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
* |
|
74
|
|
|
* @throws ParameterNotFoundException |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function getChainDriverServiceName(ContainerBuilder $container) |
|
77
|
|
|
{ |
|
78
|
|
|
foreach ($this->managerParameters as $param) { |
|
79
|
|
|
if ($container->hasParameter($param)) { |
|
80
|
|
|
$name = $container->getParameter($param); |
|
81
|
|
|
if ($name) { |
|
82
|
|
|
return sprintf($this->driverPattern, $name); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
throw new ParameterNotFoundException('None of the managerParameters resulted in a valid name'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param array $mappings |
|
92
|
|
|
* |
|
93
|
|
|
* @return \Alcalyn\PayplugBundle\DependencyInjection\Compiler\RegisterMappingsPass |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function createOrmMappingDriver(array $mappings) |
|
96
|
|
|
{ |
|
97
|
|
|
$arguments = array($mappings, '.orm.yml'); |
|
98
|
|
|
$locator = new Definition('Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator', $arguments); |
|
99
|
|
|
$driver = new Definition('Doctrine\ORM\Mapping\Driver\YamlDriver', array($locator)); |
|
100
|
|
|
$driverPattern = 'doctrine.orm.%s_metadata_driver'; |
|
101
|
|
|
$managerParameters = array('doctrine.default_entity_manager'); |
|
102
|
|
|
|
|
103
|
|
|
return new RegisterMappingsPass($driver, $driverPattern, $mappings, $managerParameters); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|