-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathEnqueueExtension.php
152 lines (123 loc) · 5.8 KB
/
EnqueueExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace Enqueue\Bundle\DependencyInjection;
use Enqueue\AsyncCommand\DependencyInjection\AsyncCommandExtension;
use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncEventDispatcherExtension;
use Enqueue\Client\CommandSubscriberInterface;
use Enqueue\Client\TopicSubscriberInterface;
use Enqueue\JobQueue\Job;
use Enqueue\Symfony\Client\DependencyInjection\ClientFactory;
use Enqueue\Symfony\DependencyInjection\TransportFactory;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
final class EnqueueExtension extends Extension implements PrependExtensionInterface
{
public function load(array $configs, ContainerBuilder $container): void
{
$config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$transportFactory = (new TransportFactory('default'));
$transportFactory->buildConnectionFactory($container, $config['transport']);
$transportFactory->buildContext($container, []);
$transportFactory->buildQueueConsumer($container, $config['consumption']);
$transportFactory->buildRpcClient($container, []);
if (isset($config['client'])) {
$this->setupAutowiringForProcessors($container);
$loader->load('client.yml');
$clientConfig = $config['client'];
// todo
$clientConfig['transport'] = $config['transport'];
$clientConfig['consumption'] = $config['consumption'];
$clientFactory = new ClientFactory('default');
$clientFactory->build($container, $clientConfig);
$clientFactory->createDriver($container, $config['transport']);
}
if ($config['job']) {
if (!class_exists(Job::class)) {
throw new \LogicException('Seems "enqueue/job-queue" is not installed. Please fix this issue.');
}
$loader->load('job.yml');
}
if ($config['async_events']['enabled']) {
if (false == class_exists(AsyncEventDispatcherExtension::class)) {
throw new \LogicException('The "enqueue/async-event-dispatcher" package has to be installed.');
}
$extension = new AsyncEventDispatcherExtension();
$extension->load([[
'context_service' => 'enqueue.transport.default.context',
]], $container);
}
if ($config['async_commands']['enabled']) {
if (false == class_exists(AsyncCommandExtension::class)) {
throw new \LogicException('The "enqueue/async-command" package has to be installed.');
}
$extension = new AsyncCommandExtension();
$extension->load([[]], $container);
}
if ($config['extensions']['doctrine_ping_connection_extension']) {
$loader->load('extensions/doctrine_ping_connection_extension.yml');
}
if ($config['extensions']['doctrine_clear_identity_map_extension']) {
$loader->load('extensions/doctrine_clear_identity_map_extension.yml');
}
if ($config['extensions']['signal_extension']) {
$loader->load('extensions/signal_extension.yml');
}
if ($config['extensions']['reply_extension']) {
$loader->load('extensions/reply_extension.yml');
}
}
public function getConfiguration(array $config, ContainerBuilder $container): Configuration
{
$rc = new \ReflectionClass(Configuration::class);
$container->addResource(new FileResource($rc->getFileName()));
return new Configuration($container->getParameter('kernel.debug'));
}
public function prepend(ContainerBuilder $container): void
{
$this->registerJobQueueDoctrineEntityMapping($container);
}
private function registerJobQueueDoctrineEntityMapping(ContainerBuilder $container)
{
if (!class_exists(Job::class)) {
return;
}
$bundles = $container->getParameter('kernel.bundles');
if (!isset($bundles['DoctrineBundle'])) {
return;
}
foreach ($container->getExtensionConfig('doctrine') as $config) {
// do not register mappings if dbal not configured.
if (!empty($config['dbal'])) {
$rc = new \ReflectionClass(Job::class);
$jobQueueRootDir = dirname($rc->getFileName());
$container->prependExtensionConfig('doctrine', [
'orm' => [
'mappings' => [
'enqueue_job_queue' => [
'is_bundle' => false,
'type' => 'xml',
'dir' => $jobQueueRootDir.'/Doctrine/mapping',
'prefix' => 'Enqueue\JobQueue\Doctrine\Entity',
],
],
],
]);
break;
}
}
}
private function setupAutowiringForProcessors(ContainerBuilder $container)
{
$container->registerForAutoconfiguration(TopicSubscriberInterface::class)
->setPublic(true)
->addTag('enqueue.topic_subscriber', ['client' => 'default']);
$container->registerForAutoconfiguration(CommandSubscriberInterface::class)
->setPublic(true)
->addTag('enqueue.command_subscriber', ['client' => 'default']);
}
}