|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LinkValue\MobileNotifBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
6
|
|
|
use Symfony\Component\Config\FileLocator; |
|
7
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This is the class that loads and manages LinkValueMobileNotifBundle configuration. |
|
13
|
|
|
*/ |
|
14
|
|
|
class LinkValueMobileNotifExtension extends Extension |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* {@inheritdoc} |
|
18
|
|
|
*/ |
|
19
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
20
|
|
|
{ |
|
21
|
|
|
$configuration = new Configuration(); |
|
22
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
|
23
|
|
|
|
|
24
|
|
|
$this->registerClients($config, $container); |
|
25
|
|
|
|
|
26
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
27
|
|
|
$loader->load('services.yml'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Register each client as service, such as "link_value_mobile_notif.clients.the_client_type.my_custom_client_name" |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $config |
|
34
|
|
|
* @param ContainerBuilder $container |
|
35
|
|
|
*/ |
|
36
|
|
|
private function registerClients(array $config, ContainerBuilder $container) |
|
37
|
|
|
{ |
|
38
|
|
|
foreach ($config['clients'] as $clientType => $clients) { |
|
39
|
|
|
$clientFQCN = ($clientType == 'apns') ? 'LinkValue\MobileNotifBundle\Client\ApnsClient' : 'LinkValue\MobileNotifBundle\Client\GcmClient'; |
|
40
|
|
|
|
|
41
|
|
|
foreach ($clients as $clientName => $clientConfig) { |
|
42
|
|
|
$params = isset($clientConfig['params']) ? $clientConfig['params'] : array(); |
|
43
|
|
|
$services = isset($clientConfig['services']) ? $clientConfig['services'] : array(); |
|
44
|
|
|
|
|
45
|
|
|
// The final client name is a concatenation of the client type (apns/gcm) and the client name (defined by user) separated by a point '.' |
|
46
|
|
|
$clientName = sprintf('%s.%s', $clientType, $clientName); |
|
47
|
|
|
|
|
48
|
|
|
// Register client with required stuff |
|
49
|
|
|
$client = $container->register(sprintf('link_value_mobile_notif.clients.%s', $clientName), $clientFQCN) |
|
50
|
|
|
->addMethodCall('setUp', array($params)) |
|
51
|
|
|
->addTag('link_value_mobile_notif.client', array('name' => $clientName)) |
|
52
|
|
|
; |
|
53
|
|
|
|
|
54
|
|
|
// Set optional logger |
|
55
|
|
|
if(!empty($services['logger'])) { |
|
56
|
|
|
$client->addMethodCall('setLogger', array(new Reference($services['logger']))); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Set optional profiler |
|
60
|
|
|
if(!empty($services['profiler'])) { |
|
61
|
|
|
$client->addMethodCall('setClientProfiler', array(new Reference($services['profiler']))); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|