1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoS\SMSBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use DoS\ResourceBundle\DependencyInjection\AbstractResourceExtension; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
9
|
|
|
|
10
|
|
|
class DoSSMSExtension extends AbstractResourceExtension |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
|
|
protected function getBundleConfiguration() |
16
|
|
|
{ |
17
|
|
|
return new Configuration(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function load(array $configs, ContainerBuilder $container) |
24
|
|
|
{ |
25
|
|
|
$config = parent::load($configs, $container); |
26
|
|
|
|
27
|
|
|
// fix the sms.http_adapter definition to point to the right adapter |
28
|
|
|
$container->setAlias('dos.sms.http_adapter', sprintf('dos.sms.http_adapter.%s', $config['http_adapter'])); |
29
|
|
|
$container->getAlias('dos.sms.http_adapter')->setPublic(false); |
30
|
|
|
|
31
|
|
|
// define an alias to the real pooling service (will be used by the compiler pass) |
32
|
|
|
$container->setAlias('dos.sms.pool', sprintf('dos.sms.pool.%s', $config['pool'])); |
33
|
|
|
|
34
|
|
|
$container->getDefinition('dos.sms.sender.delayed')->setArguments(array( |
35
|
|
|
new Reference('dos.sms.sender.storable'), |
36
|
|
|
new Reference('dos.sms.pool'), |
37
|
|
|
)); |
38
|
|
|
|
39
|
|
|
$container->setAlias('dos.sms.sender', $config['sender']); |
40
|
|
|
$container->setParameter('dos.sms.testing_number', $config['testing_number']); |
41
|
|
|
|
42
|
|
|
// set default provider |
43
|
|
|
$container->getDefinition('dos.sms.sender.default')->setArguments(array( |
44
|
|
|
new Reference('dos.sms.sender.provider.'.$config['provider']), |
45
|
|
|
)); |
46
|
|
|
|
47
|
|
|
$container->getDefinition('dos.sms.provider.provider')->addMethodCall('setDefaultProvider', array( |
48
|
|
|
$config['provider'], |
49
|
|
|
)); |
50
|
|
|
|
51
|
|
|
foreach ($config['providers'] as $name => $options) { |
52
|
|
|
$provider = $container->getDefinition('dos.sms.sender.provider.'.$name); |
53
|
|
|
$provider->addTag('dos.sms.provider', array('alias' => $name)); |
54
|
|
|
|
55
|
|
|
$this->refactorArguments($container, $provider, (array) $options); |
56
|
|
|
|
57
|
|
|
// TODO: add compile pass (taged service) to allow to add none 'dos.sms.sender.provider.xx' pattern service. |
58
|
|
|
// to add others provider you just naming it with 'dos.sms.sender.provider.xxx' and then configurate |
59
|
|
|
// under ... |
60
|
|
|
// dos_sms: |
61
|
|
|
// providers: |
62
|
|
|
// xxx: ... |
63
|
|
|
$container->getDefinition('dos.sms.sender.default') |
64
|
|
|
->addMethodCall('registerProvider', array(new Reference('dos.sms.sender.provider.'.$name))) |
65
|
|
|
; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function refactorArguments(ContainerBuilder $container, Definition $definition, array $options) |
70
|
|
|
{ |
71
|
|
|
$class = new \ReflectionClass($container->getParameter(str_replace('%', '', $definition->getClass()))); |
72
|
|
|
|
73
|
|
|
if ($constructor = $class->getConstructor()) { |
74
|
|
|
$arguments = array(); |
75
|
|
|
foreach ($constructor->getParameters() as $parameter) { |
76
|
|
|
if ($class = $parameter->getClass()) { |
77
|
|
|
if ($class->getName() === 'SmsSender\HttpAdapter\HttpAdapterInterface') { |
78
|
|
|
$arguments[] = new Reference('dos.sms.http_adapter'); |
79
|
|
|
} |
80
|
|
|
} elseif (array_key_exists($parameter->getName(), $options)) { |
81
|
|
|
$arguments[] = $options[$parameter->getName()]; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$definition->setArguments($arguments); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|