1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MovingImage\Bundle\VMProApiBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use Symfony\Component\Config\FileLocator; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
9
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class VMProApiExtension. |
13
|
|
|
* |
14
|
|
|
* @author Ruben Knol <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class VMProApiExtension extends ConfigurableExtension |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Flatten multi-dimensional Symfony configuration array into a |
20
|
|
|
* one-dimensional parameters array. |
21
|
|
|
* |
22
|
|
|
* @param ContainerBuilder $container |
23
|
|
|
* @param array $configs |
24
|
|
|
* @param string $root |
25
|
|
|
* |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
6 |
|
private function flattenParametersFromConfig(ContainerBuilder $container, $configs, $root) |
29
|
|
|
{ |
30
|
6 |
|
$parameters = []; |
31
|
|
|
|
32
|
6 |
|
foreach ($configs as $key => $value) { |
33
|
6 |
|
$parameterKey = sprintf('%s_%s', $root, $key); |
34
|
6 |
|
if (is_array($value)) { |
35
|
6 |
|
$parameters = array_merge( |
36
|
6 |
|
$parameters, |
37
|
6 |
|
$this->flattenParametersFromConfig($container, $value, $parameterKey) |
38
|
6 |
|
); |
39
|
|
|
|
40
|
6 |
|
continue; |
41
|
|
|
} |
42
|
|
|
|
43
|
6 |
|
$parameters[$parameterKey] = $value; |
44
|
6 |
|
} |
45
|
|
|
|
46
|
6 |
|
return $parameters; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Load the appropriate service container configurations based on which |
51
|
|
|
* GuzzleHttp library is present in the project. |
52
|
|
|
* |
53
|
|
|
* @param array $configs |
54
|
|
|
* @param ContainerBuilder $container |
55
|
|
|
*/ |
56
|
6 |
|
protected function loadInternal(array $configs, ContainerBuilder $container) |
57
|
|
|
{ |
58
|
6 |
|
$parameters = $this->flattenParametersFromConfig($container, $configs, 'vm_pro_api'); |
59
|
6 |
|
foreach ($parameters as $key => $value) { |
60
|
6 |
|
$container->setParameter($key, $value); |
61
|
6 |
|
} |
62
|
|
|
|
63
|
6 |
|
$loader = new YamlFileLoader( |
64
|
6 |
|
$container, |
65
|
6 |
|
new FileLocator(__DIR__.'/../Resources/config') |
66
|
6 |
|
); |
67
|
|
|
|
68
|
|
|
// Always load the services that will always be the same |
69
|
6 |
|
$loader->load('services/main.yml'); |
70
|
|
|
|
71
|
|
|
// only load ratings service if the meta data fields are configured |
72
|
6 |
|
if (array_key_exists('vm_pro_api_rating_meta_data_fields_average', $parameters) |
73
|
6 |
|
&& array_key_exists('vm_pro_api_rating_meta_data_fields_count', $parameters)) { |
74
|
|
|
$loader->load('services/ratings.yml'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Dynamically load service configurations that are specific |
78
|
|
|
// to which Guzzle version is installed |
79
|
6 |
|
if (version_compare(ClientInterface::VERSION, '6.0', '>=')) { |
80
|
3 |
|
$loader->load('services/guzzle6.yml'); |
81
|
3 |
|
} else { |
82
|
3 |
|
$loader->load('services/guzzle5.yml'); |
83
|
|
|
} |
84
|
6 |
|
} |
85
|
|
|
} |
86
|
|
|
|