1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Bundle\ModlrBundle\DependencyInjection\ServiceLoader; |
4
|
|
|
|
5
|
|
|
use As3\Bundle\ModlrBundle\DependencyInjection\Utility; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Loads metadata driver services. |
12
|
|
|
* |
13
|
|
|
* @author Jacob Bare <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class MetadataDrivers implements ServiceLoaderInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Creates the file locator service definition. |
19
|
|
|
* |
20
|
|
|
* @param string $modelDir |
21
|
|
|
* @param string $mixinDir |
22
|
|
|
* @return Definition |
23
|
|
|
*/ |
24
|
|
|
private function createFileLocator($modelDir, $mixinDir) |
25
|
|
|
{ |
26
|
|
|
$definition = new Definition( |
27
|
|
|
Utility::getLibraryClass('Metadata\Driver\FileLocator'), |
28
|
|
|
[$modelDir, $mixinDir] |
29
|
|
|
); |
30
|
|
|
$definition->setPublic(false); |
31
|
|
|
return $definition; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Creates the YAML metadata driver service definition. |
36
|
|
|
* |
37
|
|
|
* @param string $driverName |
38
|
|
|
* @param array $driverConfig |
39
|
|
|
* @param ContainerBuilder $container |
40
|
|
|
* @return Definition |
41
|
|
|
*/ |
42
|
|
|
private function createYmlDriver($driverName, array $driverConfig, ContainerBuilder $container) |
43
|
|
|
{ |
44
|
|
|
// Definition directories |
45
|
|
|
list($modelDir, $mixinDir) = $this->getDefinitionDirs($driverConfig, $container); |
46
|
|
|
|
47
|
|
|
// Set the directories to the dirs container parameter. |
48
|
|
|
Utility::appendParameter('dirs', sprintf('%s.model_dir', $driverName), $modelDir, $container); |
49
|
|
|
Utility::appendParameter('dirs', sprintf('%s.mixin_dir', $driverName), $mixinDir, $container); |
50
|
|
|
|
51
|
|
|
// File locator |
52
|
|
|
$locatorName = sprintf('%s.file_locator', $driverName); |
53
|
|
|
$locatorDef = $this->createFileLocator($modelDir, $mixinDir); |
54
|
|
|
$container->setDefinition($locatorName, $locatorDef); |
55
|
|
|
|
56
|
|
|
// Driver |
57
|
|
|
return new Definition( |
58
|
|
|
Utility::getLibraryClass('Metadata\Driver\YamlFileDriver'), |
59
|
|
|
[ |
60
|
|
|
new Reference($locatorName), |
61
|
|
|
new Reference(Utility::getAliasedName('storage_manager')), |
62
|
|
|
] |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Gets the directory for a definition type. |
68
|
|
|
* |
69
|
|
|
* @param string $type |
70
|
|
|
* @param array $driverConfig |
71
|
|
|
* @param ContainerBuilder $container |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
private function getDefinitionDir($type, array $driverConfig, ContainerBuilder $container) |
75
|
|
|
{ |
76
|
|
|
$defaultDir = sprintf('%s/Resources/As3ModlrBundle', $container->getParameter('kernel.root_dir')); |
77
|
|
|
|
78
|
|
|
$folder = sprintf('%ss', $type); |
79
|
|
|
$key = sprintf('%s_dir', $type); |
80
|
|
|
|
81
|
|
|
return isset($driverConfig['parameters'][$key]) |
82
|
|
|
? Utility::locateResource($driverConfig['parameters'][$key], $container) |
83
|
|
|
: sprintf('%s/%s', $defaultDir, $folder) |
84
|
|
|
; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Gets the definition directories for models and mixins and returns as a tuple. |
89
|
|
|
* |
90
|
|
|
* @param array $driverConfig |
91
|
|
|
* @param ContainerBuilder $container |
92
|
|
|
* @return string[] |
93
|
|
|
*/ |
94
|
|
|
private function getDefinitionDirs(array $driverConfig, ContainerBuilder $container) |
95
|
|
|
{ |
96
|
|
|
$modelDir = $this->getDefinitionDir('model', $driverConfig, $container); |
97
|
|
|
$mixinDir = $this->getDefinitionDir('mixin', $driverConfig, $container); |
98
|
|
|
return [$modelDir, $mixinDir]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
* @todo Add low-level support for multiple drivers in the metadata factory. |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function load(array $config, ContainerBuilder $container) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
foreach ($config['metadata']['drivers'] as $name => $driverConfig) { |
108
|
|
|
$driverName = Utility::getAliasedName(sprintf('metadata.driver.%s', $name)); |
109
|
|
|
if (isset($driverConfig['service'])) { |
110
|
|
|
// Custom persister. |
111
|
|
|
$container->setAlias($driverName, Utility::cleanServiceName($driverConfig['service'])); |
112
|
|
|
} else { |
113
|
|
|
// Built-in driver. |
114
|
|
|
switch ($driverConfig['type']) { |
115
|
|
|
case 'yml': |
116
|
|
|
$definition = $this->createYmlDriver($driverName, $driverConfig, $container); |
117
|
|
|
break; |
118
|
|
|
default: |
119
|
|
|
throw new \RuntimeException(sprintf('Unable to create a metadata driver for type "%s"', $driverConfig['type'])); |
120
|
|
|
} |
121
|
|
|
$definition->setPublic(false); |
122
|
|
|
$container->setDefinition($driverName, $definition); |
123
|
|
|
} |
124
|
|
|
// The library currently only supports one driver. Must break and set as default alias. |
125
|
|
|
$container->setAlias(Utility::getAliasedName('metadata.default_driver'), $driverName); |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.