-
Notifications
You must be signed in to change notification settings - Fork 33
/
DoctrineOrmMappingsPass.php
118 lines (107 loc) · 4.72 KB
/
DoctrineOrmMappingsPass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2017 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Bundle\CoreBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\Reference;
/**
* Forward compatibility class to work with Symfony < 2.3 and/or
* Doctrine ORM bundle < 1.2.1.
*
* @author David Buchmann <mail@davidbu.ch>
*/
class DoctrineOrmMappingsPass implements CompilerPassInterface
{
private $driver;
private $driverPattern;
private $namespaces;
private $enabledParameter;
private $managerParameters;
/**
* Usually, you should not need to directly instantiate this class but use
* one of the factory methods.
*
* @param Definition|Reference $driver The driver to use
* @param array $namespaces List of namespaces this driver should handle
* @param string[] $managerParameters Ordered list of container parameters that may
* provide the name of the manager to register
* the mappings for. The first non-empty name
* is used, the others skipped
* @param bool $enabledParameter if specified, the compiler pass only executes
* if this parameter exists in the service container
*/
public function __construct($driver, $namespaces, array $managerParameters, $enabledParameter = false)
{
$managerParameters[] = 'doctrine.default_entity_manager';
$this->driver = $driver;
$this->namespaces = $namespaces;
$this->enabledParameter = $enabledParameter;
$this->driverPattern = 'doctrine.orm.%s_metadata_driver';
$this->managerParameters = $managerParameters;
}
/**
* Register mappings with the metadata drivers.
*
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
if (!$this->enabledParameter
|| !$container->hasParameter($this->enabledParameter)
) {
return;
}
$chainDriverDefService = $this->getChainDriverServiceName($container);
$chainDriverDef = $container->getDefinition($chainDriverDefService);
foreach ($this->namespaces as $namespace) {
$chainDriverDef->addMethodCall('addDriver', [$this->driver, $namespace]);
}
}
/**
* @param ContainerBuilder $container
*
* @return string
*
* @throws ParameterNotFoundException
*/
protected function getChainDriverServiceName(ContainerBuilder $container)
{
foreach ($this->managerParameters as $param) {
if ($container->hasParameter($param)) {
$name = $container->getParameter($param);
if ($name) {
return sprintf($this->driverPattern, $name);
}
}
}
throw new ParameterNotFoundException('None of the managerParameters resulted in a valid name');
}
/**
* Create a mapping with the bundle namespace aware SymfonyFileLocator.
*
* @param array $mappings Hashmap of directory path to namespace
* @param string[] $managerParameters List of parameters that could tell which object manager name
* your bundle uses. This compiler pass will automatically
* append the parameter name for the default entity manager
* to this list
* @param bool|string $enabledParameter Service container parameter that must be present to
* enable the mapping. Set to false to not do any check,
* optional
*/
public static function createXmlMappingDriver(array $mappings, array $managerParameters = [], $enabledParameter = false)
{
$arguments = [$mappings, '.orm.xml'];
$locator = new Definition('Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator', $arguments);
$driver = new Definition('Doctrine\ORM\Mapping\Driver\XmlDriver', [$locator]);
return new self($driver, $mappings, $managerParameters, $enabledParameter);
}
}