1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Symplify |
5
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Symplify\SimpleBundle\DependencyInjection\Extension; |
9
|
|
|
|
10
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
11
|
|
|
use Symfony\Component\Config\Definition\Processor; |
12
|
|
|
use Symfony\Component\Config\FileLocator; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
14
|
|
|
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
16
|
|
|
|
17
|
|
|
final class ServicesAndConfigurationAwareExtension implements ExtensionInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $serviceDirectory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $serviceFileName; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string[] |
31
|
|
|
*/ |
32
|
|
|
private $parametersAliases = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ConfigurationInterface |
36
|
|
|
*/ |
37
|
|
|
private $configuration; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function load(array $configs, ContainerBuilder $containerBuilder) |
43
|
|
|
{ |
44
|
|
|
if ($this->serviceDirectory && $this->serviceFileName) { |
45
|
|
|
$yamlFileLoader = new YamlFileLoader($containerBuilder, new FileLocator($this->serviceDirectory)); |
46
|
|
|
$yamlFileLoader->load($this->serviceFileName); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!$this->configuration) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$config = (new Processor())->processConfiguration($this->configuration, $configs); |
54
|
|
|
foreach ($this->parametersAliases as $parameterName => $parameterAlias) { |
55
|
|
|
$containerBuilder->setParameter($parameterAlias, $config[$parameterName]); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setServicesFilePath(string $servicesFilePath) |
60
|
|
|
{ |
61
|
|
|
$this->prepareAbsoluteDirAndRelativeFilePath($servicesFilePath); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function setConfiguration(ConfigurationInterface $configuration) |
65
|
|
|
{ |
66
|
|
|
$this->configuration = $configuration; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setParametersAliases(array $parametersAliases) |
70
|
|
|
{ |
71
|
|
|
$this->parametersAliases = $parametersAliases; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function getAlias() |
78
|
|
|
{ |
79
|
|
|
return md5($this->serviceDirectory); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getNamespace() |
83
|
|
|
{ |
84
|
|
|
// unsupported |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getXsdValidationBasePath() |
88
|
|
|
{ |
89
|
|
|
// unsupported |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function prepareAbsoluteDirAndRelativeFilePath(string $absolutePath) |
93
|
|
|
{ |
94
|
|
|
$this->serviceDirectory = dirname($absolutePath); |
95
|
|
|
$this->serviceFileName = substr($absolutePath, strlen($this->serviceDirectory) + 1); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|