1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Sylius\Bundle\ResourceBundle; |
15
|
|
|
|
16
|
|
|
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass; |
17
|
|
|
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass; |
18
|
|
|
use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass; |
19
|
|
|
use Sylius\Bundle\ResourceBundle\DependencyInjection\Driver\Exception\UnknownDriverException; |
20
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
21
|
|
|
use Symfony\Component\DependencyInjection\Container; |
22
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
23
|
|
|
use Symfony\Component\HttpKernel\Bundle\Bundle; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Arnaud Langlade <[email protected]> |
27
|
|
|
* @author Gustavo Perdomo <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
abstract class AbstractResourceBundle extends Bundle implements ResourceBundleInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Configure format of mapping files. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $mappingFormat = ResourceBundleInterface::MAPPING_XML; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function build(ContainerBuilder $container): void |
42
|
|
|
{ |
43
|
|
|
if (null !== $this->getModelNamespace()) { |
44
|
|
|
foreach ($this->getSupportedDrivers() as $driver) { |
45
|
|
|
[$compilerPassClassName, $compilerPassMethod] = $this->getMappingCompilerPassInfo($driver); |
|
|
|
|
46
|
|
|
|
47
|
|
|
if (class_exists($compilerPassClassName)) { |
48
|
|
|
if (!method_exists($compilerPassClassName, $compilerPassMethod)) { |
49
|
|
|
throw new InvalidConfigurationException( |
50
|
|
|
"The 'mappingFormat' value is invalid, must be 'xml', 'yml' or 'annotation'." |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
switch ($this->mappingFormat) { |
55
|
|
|
case ResourceBundleInterface::MAPPING_XML: |
56
|
|
|
case ResourceBundleInterface::MAPPING_YAML: |
57
|
|
|
$container->addCompilerPass($compilerPassClassName::$compilerPassMethod( |
58
|
|
|
[$this->getConfigFilesPath() => $this->getModelNamespace()], |
59
|
|
|
[$this->getObjectManagerParameter()], |
60
|
|
|
sprintf('%s.driver.%s', $this->getBundlePrefix(), $driver) |
61
|
|
|
)); |
62
|
|
|
|
63
|
|
|
break; |
64
|
|
|
case ResourceBundleInterface::MAPPING_ANNOTATION: |
65
|
|
|
$container->addCompilerPass($compilerPassClassName::$compilerPassMethod( |
66
|
|
|
[$this->getModelNamespace()], |
67
|
|
|
[$this->getConfigFilesPath()], |
68
|
|
|
[sprintf('%s.object_manager', $this->getBundlePrefix())], |
69
|
|
|
sprintf('%s.driver.%s', $this->getBundlePrefix(), $driver) |
70
|
|
|
)); |
71
|
|
|
|
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return the prefix of the bundle. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
protected function getBundlePrefix(): string |
85
|
|
|
{ |
86
|
|
|
return Container::underscore(substr(strrchr(get_class($this), '\\'), 1, -6)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Return the directory where are stored the doctrine mapping. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
protected function getDoctrineMappingDirectory(): string |
95
|
|
|
{ |
96
|
|
|
return 'model'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return the entity namespace. |
101
|
|
|
* |
102
|
|
|
* @return string |
|
|
|
|
103
|
|
|
*/ |
104
|
|
|
protected function getModelNamespace(): ?string |
105
|
|
|
{ |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return mapping compiler pass class depending on driver. |
111
|
|
|
* |
112
|
|
|
* @param string $driverType |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
* |
116
|
|
|
* @throws UnknownDriverException |
117
|
|
|
*/ |
118
|
|
|
protected function getMappingCompilerPassInfo(string $driverType): array |
119
|
|
|
{ |
120
|
|
|
switch ($driverType) { |
121
|
|
|
case SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM: |
122
|
|
|
$mappingsPassClassname = DoctrineMongoDBMappingsPass::class; |
123
|
|
|
|
124
|
|
|
break; |
125
|
|
|
case SyliusResourceBundle::DRIVER_DOCTRINE_ORM: |
126
|
|
|
$mappingsPassClassname = DoctrineOrmMappingsPass::class; |
127
|
|
|
|
128
|
|
|
break; |
129
|
|
|
case SyliusResourceBundle::DRIVER_DOCTRINE_PHPCR_ODM: |
130
|
|
|
$mappingsPassClassname = DoctrinePhpcrMappingsPass::class; |
131
|
|
|
|
132
|
|
|
break; |
133
|
|
|
default: |
134
|
|
|
throw new UnknownDriverException($driverType); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$compilerPassMethod = sprintf('create%sMappingDriver', ucfirst($this->mappingFormat)); |
138
|
|
|
|
139
|
|
|
return [$mappingsPassClassname, $compilerPassMethod]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Return the absolute path where are stored the doctrine mapping. |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
protected function getConfigFilesPath(): string |
148
|
|
|
{ |
149
|
|
|
return sprintf( |
150
|
|
|
'%s/Resources/config/doctrine/%s', |
151
|
|
|
$this->getPath(), |
152
|
|
|
strtolower($this->getDoctrineMappingDirectory()) |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
protected function getObjectManagerParameter(): string |
160
|
|
|
{ |
161
|
|
|
return sprintf('%s.object_manager', $this->getBundlePrefix()); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.