|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MediaMonks\RestApiBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Config\FileLocator; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
9
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* This is the class that loads and manages your bundle configuration. |
|
14
|
|
|
* |
|
15
|
|
|
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html |
|
16
|
|
|
*/ |
|
17
|
|
|
class MediaMonksRestApiExtension extends Extension implements ExtensionInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
5 |
|
public function load(array $configs, ContainerBuilder $container) |
|
23
|
|
|
{ |
|
24
|
5 |
|
$config = $this->processConfiguration(new Configuration(), $configs); |
|
25
|
|
|
|
|
26
|
5 |
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
27
|
5 |
|
$loader->load('services.xml'); |
|
28
|
|
|
|
|
29
|
5 |
|
if (!empty($config['request_matcher']['path'])) { |
|
30
|
1 |
|
$this->usePathRequestMatcher($container, $config); |
|
31
|
|
|
} |
|
32
|
4 |
|
elseif (!empty($config['request_matcher']['whitelist'])) { |
|
33
|
4 |
|
$this->useRegexRequestMatcher($container, $config); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
5 |
|
$container->getDefinition('mediamonks_rest_api.response_transformer') |
|
37
|
5 |
|
->replaceArgument( |
|
38
|
5 |
|
2, |
|
39
|
|
|
[ |
|
40
|
5 |
|
'debug' => $this->getDebug($config, $container), |
|
41
|
5 |
|
'post_message_origin' => $config['post_message_origin'], |
|
42
|
|
|
] |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
5 |
|
if (!empty($config['response_model'])) { |
|
46
|
1 |
|
$this->replaceResponseModel($container, $config); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
5 |
|
$this->loadSerializer($container, $config); |
|
50
|
5 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param ContainerBuilder $container |
|
54
|
|
|
* @param array $config |
|
55
|
|
|
*/ |
|
56
|
1 |
|
protected function usePathRequestMatcher(ContainerBuilder $container, array $config) |
|
57
|
|
|
{ |
|
58
|
1 |
|
$container->getDefinition('mediamonks_rest_api.path_request_matcher') |
|
59
|
1 |
|
->replaceArgument(0, $config['request_matcher']['path']); |
|
60
|
|
|
|
|
61
|
1 |
|
$container->getDefinition('mediamonks_rest_api.rest_api_event_subscriber') |
|
62
|
1 |
|
->replaceArgument(0, new Reference('mediamonks_rest_api.path_request_matcher')); |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param ContainerBuilder $container |
|
67
|
|
|
* @param array $config |
|
68
|
|
|
*/ |
|
69
|
4 |
|
protected function useRegexRequestMatcher(ContainerBuilder $container, array $config) |
|
70
|
|
|
{ |
|
71
|
4 |
|
$container->getDefinition('mediamonks_rest_api.regex_request_matcher') |
|
72
|
4 |
|
->replaceArgument(0, $config['request_matcher']['whitelist']); |
|
73
|
4 |
|
$container->getDefinition('mediamonks_rest_api.regex_request_matcher') |
|
74
|
4 |
|
->replaceArgument(1, $config['request_matcher']['blacklist']); |
|
75
|
4 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param ContainerBuilder $container |
|
79
|
|
|
* @param array $config |
|
80
|
|
|
*/ |
|
81
|
5 |
|
protected function loadSerializer(ContainerBuilder $container, array $config) |
|
82
|
|
|
{ |
|
83
|
5 |
|
if (!$container->has($config['serializer'])) { |
|
84
|
5 |
|
$config['serializer'] = 'mediamonks_rest_api.serializer.'.$config['serializer']; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
5 |
|
$container->getDefinition('mediamonks_rest_api.request_transformer') |
|
88
|
5 |
|
->replaceArgument(0, new Reference($config['serializer'])); |
|
89
|
5 |
|
$container->getDefinition('mediamonks_rest_api.response_transformer') |
|
90
|
5 |
|
->replaceArgument(0, new Reference($config['serializer'])); |
|
91
|
5 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param ContainerBuilder $container |
|
95
|
|
|
* @param array $config |
|
96
|
|
|
*/ |
|
97
|
1 |
|
protected function replaceResponseModel(ContainerBuilder $container, array $config) |
|
98
|
|
|
{ |
|
99
|
1 |
|
$container->getDefinition('mediamonks_rest_api.response_model_factory') |
|
100
|
1 |
|
->replaceArgument(0, new Reference($config['response_model'])); |
|
101
|
1 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
7 |
|
public function getAlias() |
|
107
|
|
|
{ |
|
108
|
7 |
|
return 'mediamonks_rest_api'; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param array $config |
|
113
|
|
|
* @param ContainerBuilder $container |
|
114
|
|
|
* @return bool |
|
115
|
|
|
*/ |
|
116
|
7 |
|
public function getDebug(array $config, ContainerBuilder $container) |
|
117
|
|
|
{ |
|
118
|
7 |
|
if (isset($config['debug'])) { |
|
119
|
1 |
|
return $config['debug']; |
|
120
|
|
|
} |
|
121
|
6 |
|
if ($container->hasParameter('kernel.debug')) { |
|
122
|
1 |
|
return $container->getParameter('kernel.debug'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
5 |
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|