|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ozean12\GooglePubSubBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Ozean12\GooglePubSubBundle\Service\Publisher\Publisher; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
7
|
|
|
use Symfony\Component\Config\FileLocator; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
9
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* {@inheritdoc} |
|
14
|
|
|
*/ |
|
15
|
|
|
class Ozean12GooglePubSubExtension extends Extension |
|
16
|
|
|
{ |
|
17
|
|
|
const PUBSUB_CLIENT_SERVICE_DEFINITION = 'ozean12_google_pubsub.pubsub_client.service'; |
|
18
|
|
|
const CLIENT_SERVICE_DEFINITION = 'ozean12_google_pubsub.client.service'; |
|
19
|
|
|
const PUBLISHER_SERVICE_DEFINITION = 'ozean12_google_pubsub.publisher.'; |
|
20
|
|
|
const SUBSCRIBER_MANAGER_SERVICE_DEFINITION = 'ozean12_google_pubsub.push_subscriber_manager.service'; |
|
21
|
|
|
const TAG_NAME = 'ozean12_pub_sub_service'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
27
|
|
|
{ |
|
28
|
|
|
$configuration = new Configuration(); |
|
29
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
|
30
|
|
|
|
|
31
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
32
|
|
|
$loader->load('services.yml'); |
|
33
|
|
|
|
|
34
|
|
|
$definitions = []; |
|
35
|
|
|
$clientConfig = [ |
|
36
|
|
|
'projectId' => $config['project_id'], |
|
37
|
|
|
'keyFilePath' => $config['key_file_path'], |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
$pubSubDefinition = $container |
|
41
|
|
|
->getDefinition(self::PUBSUB_CLIENT_SERVICE_DEFINITION) |
|
42
|
|
|
->replaceArgument(0, $clientConfig) |
|
43
|
|
|
; |
|
44
|
|
|
|
|
45
|
|
|
$baseDefinition = $container->getDefinition(self::CLIENT_SERVICE_DEFINITION); |
|
46
|
|
|
|
|
47
|
|
|
foreach ($config['topics'] as $topic) { |
|
48
|
|
|
$definitions[self::PUBLISHER_SERVICE_DEFINITION.$topic] = (clone $baseDefinition) |
|
49
|
|
|
->replaceArgument(0, $topic) |
|
50
|
|
|
->replaceArgument(1, $pubSubDefinition) |
|
51
|
|
|
->setClass(Publisher::class) |
|
52
|
|
|
->setPublic(true) |
|
53
|
|
|
->addTag(self::TAG_NAME) |
|
54
|
|
|
; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$subscriberManager = $container |
|
58
|
|
|
->getDefinition(self::SUBSCRIBER_MANAGER_SERVICE_DEFINITION) |
|
59
|
|
|
->replaceArgument(0, $config['project_id']) |
|
60
|
|
|
->addTag(self::TAG_NAME) |
|
61
|
|
|
; |
|
62
|
|
|
|
|
63
|
|
|
foreach ($config['push_subscriptions'] as $subscriptionName => $subscriberServiceName) { |
|
64
|
|
|
$subscriberManager |
|
65
|
|
|
->addMethodCall('addSubscriber', [$subscriptionName, new Reference($subscriberServiceName)]) |
|
66
|
|
|
; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$container->addDefinitions($definitions); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|