forked from Sylius/SyliusResourceBundle
/
AbstractResourceBundle.php
160 lines (145 loc) · 4.97 KB
/
AbstractResourceBundle.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\ResourceBundle;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\ResolveDoctrineTargetEntitiesPass;
use Sylius\Component\Resource\Exception\Driver\UnknownDriverException;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* Abstract resource bundle.
*
* @author Arnaud Langlade <arn0d.dev@gmail.com>
* @author Gustavo Perdomo <gperdomor@gmail.com>
*/
abstract class AbstractResourceBundle extends Bundle implements ResourceBundleInterface
{
const MAPPING_XML = 'xml';
const MAPPING_YAML = 'yml';
/**
* Configure format of mapping files.
* @var string
*/
protected $mappingFormat = self::MAPPING_XML;
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
{
$interfaces = $this->getModelInterfaces();
if (!empty($interfaces)) {
$container->addCompilerPass(
new ResolveDoctrineTargetEntitiesPass(
$this->getBundlePrefix(),
$interfaces
)
);
}
if (null !== $this->getModelNamespace()) {
$className = get_class($this);
foreach ($className::getSupportedDrivers() as $driver) {
list($mappingsPassClassName, $manager) = $this->getMappingDriverInfo($driver);
if (class_exists($mappingsPassClassName)) {
if (self::MAPPING_XML === $this->mappingFormat) {
$container->addCompilerPass($mappingsPassClassName::createXmlMappingDriver(
array($this->getConfigFilesPath() => $this->getModelNamespace()),
$manager,
sprintf('%s.driver.%s', $this->getBundlePrefix(), $driver)
));
} elseif (self::MAPPING_YAML === $this->mappingFormat) {
$container->addCompilerPass($mappingsPassClassName::createYamlMappingDriver(
array($this->getConfigFilesPath() => $this->getModelNamespace()),
$manager,
sprintf('%s.driver.%s', $this->getBundlePrefix(), $driver)
));
} else {
throw new InvalidConfigurationException("The 'mappingFormat' value is invalid, must be 'xml' or 'yml'.");
}
}
}
}
}
/**
* Return the prefix of the bundle
*
* @return string
*/
abstract protected function getBundlePrefix();
/**
* Target entities resolver configuration (Interface - Model)
*
* @return array
*/
protected function getModelInterfaces()
{
return array();
}
/**
* Return the directory where are stored the doctrine mapping
*
* @return string
*/
protected function getDoctrineMappingDirectory()
{
return 'model';
}
/**
* Return the entity namespace
*
* @return string
*/
protected function getModelNamespace()
{
return null;
}
/**
* Return informations used to initialize mapping driver
*
* @param string $driverType
*
* @return array
*
* @throws UnknownDriverException
*/
protected function getMappingDriverInfo($driverType)
{
switch ($driverType) {
case SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM:
return array(
'Doctrine\\Bundle\\MongoDBBundle\\DependencyInjection\\Compiler\\DoctrineMongoDBMappingsPass',
array('doctrine_mongodb.odm.document_manager'),
);
case SyliusResourceBundle::DRIVER_DOCTRINE_ORM:
return array(
'Doctrine\\Bundle\\DoctrineBundle\\DependencyInjection\\Compiler\\DoctrineOrmMappingsPass',
array('doctrine.orm.entity_manager'),
);
case SyliusResourceBundle::DRIVER_DOCTRINE_PHPCR_ODM:
return array(
'Doctrine\\Bundle\\PHPCRBundle\\DependencyInjection\\Compiler\\DoctrinePhpcrMappingsPass',
array('doctrine_phpcr.odm.document_manager'),
);
}
throw new UnknownDriverException($driverType);
}
/**
* Return the absolute path where are stored the doctrine mapping
*
* @return string
*/
protected function getConfigFilesPath()
{
return sprintf(
'%s/Resources/config/doctrine/%s',
$this->getPath(),
strtolower($this->getDoctrineMappingDirectory())
);
}
}