|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Config\FileLocator; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
10
|
|
|
use Symfony\Component\HttpClient\HttpClient; |
|
11
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; |
|
12
|
|
|
use Symfony\Component\Lock\LockFactory; |
|
13
|
|
|
use Symfony\Component\Process\Process; |
|
14
|
|
|
use Zenstruck\ScheduleBundle\EventListener\ScheduleTimezoneSubscriber; |
|
15
|
|
|
use Zenstruck\ScheduleBundle\EventListener\TaskConfigurationSubscriber; |
|
16
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension\EmailExtension; |
|
17
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension\EnvironmentExtension; |
|
18
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension\ExtensionHandler; |
|
19
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension\Handler\EmailHandler; |
|
20
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension\Handler\PingHandler; |
|
21
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension\Handler\SingleServerHandler; |
|
22
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension\Handler\WithoutOverlappingHandler; |
|
23
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension\PingExtension; |
|
24
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension\SingleServerExtension; |
|
25
|
|
|
use Zenstruck\ScheduleBundle\Schedule\ScheduleBuilder; |
|
26
|
|
|
use Zenstruck\ScheduleBundle\Schedule\SelfSchedulingCommand; |
|
27
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\Runner\MessageTaskRunner; |
|
28
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\Runner\PingTaskRunner; |
|
29
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\TaskRunner; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @author Kevin Bond <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
final class ZenstruckScheduleExtension extends ConfigurableExtension |
|
35
|
|
|
{ |
|
36
|
26 |
|
protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void |
|
37
|
|
|
{ |
|
38
|
26 |
|
$container->registerForAutoconfiguration(ScheduleBuilder::class) |
|
39
|
26 |
|
->addTag('schedule.builder') |
|
40
|
|
|
; |
|
41
|
|
|
|
|
42
|
26 |
|
$container->registerForAutoconfiguration(TaskRunner::class) |
|
43
|
26 |
|
->addTag('schedule.task_runner') |
|
44
|
|
|
; |
|
45
|
|
|
|
|
46
|
26 |
|
$container->registerForAutoconfiguration(SelfSchedulingCommand::class) |
|
47
|
26 |
|
->addTag('schedule.self_scheduling_command') |
|
48
|
|
|
; |
|
49
|
|
|
|
|
50
|
26 |
|
$container->registerForAutoconfiguration(ExtensionHandler::class) |
|
51
|
26 |
|
->addTag('schedule.extension_handler') |
|
52
|
|
|
; |
|
53
|
|
|
|
|
54
|
26 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
55
|
26 |
|
$loader->load('services.xml'); |
|
56
|
|
|
|
|
57
|
26 |
|
if (\class_exists(Process::class)) { |
|
58
|
26 |
|
$loader->load('process.xml'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$container |
|
62
|
26 |
|
->getDefinition(TaskConfigurationSubscriber::class) |
|
63
|
26 |
|
->setArgument(0, $mergedConfig['tasks']) |
|
64
|
|
|
; |
|
65
|
|
|
|
|
66
|
26 |
|
if ($mergedConfig['without_overlapping_lock_factory'] || \class_exists(LockFactory::class)) { |
|
67
|
26 |
|
$loader->load('without_overlapping.xml'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
26 |
|
if ($mergedConfig['without_overlapping_lock_factory']) { |
|
71
|
|
|
$container |
|
72
|
1 |
|
->getDefinition(WithoutOverlappingHandler::class) |
|
73
|
1 |
|
->setArgument(0, new Reference($mergedConfig['without_overlapping_lock_factory'])) |
|
74
|
|
|
; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
26 |
|
if ($mergedConfig['single_server_lock_factory']) { |
|
78
|
1 |
|
$loader->load('single_server.xml'); |
|
79
|
|
|
|
|
80
|
|
|
$container |
|
81
|
1 |
|
->getDefinition(SingleServerHandler::class) |
|
82
|
1 |
|
->setArgument(0, new Reference($mergedConfig['single_server_lock_factory'])) |
|
83
|
|
|
; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
26 |
|
if ($mergedConfig['http_client'] || \class_exists(HttpClient::class)) { |
|
87
|
26 |
|
$loader->load('http.xml'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
26 |
|
if ($mergedConfig['http_client']) { |
|
91
|
|
|
$container |
|
92
|
1 |
|
->getDefinition(PingHandler::class) |
|
93
|
1 |
|
->setArgument(0, new Reference($mergedConfig['http_client'])) |
|
94
|
|
|
; |
|
95
|
|
|
|
|
96
|
|
|
$container |
|
97
|
1 |
|
->getDefinition(PingTaskRunner::class) |
|
98
|
1 |
|
->setArgument(0, new Reference($mergedConfig['http_client'])) |
|
99
|
|
|
; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
26 |
|
if ($mergedConfig['timezone']) { |
|
103
|
1 |
|
$loader->load('timezone.xml'); |
|
104
|
|
|
$container |
|
105
|
1 |
|
->getDefinition(ScheduleTimezoneSubscriber::class) |
|
106
|
1 |
|
->setArgument(0, $mergedConfig['timezone']) |
|
107
|
|
|
; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
26 |
|
if ($mergedConfig['messenger']['enabled']) { |
|
111
|
2 |
|
$loader->load('messenger.xml'); |
|
112
|
|
|
|
|
113
|
|
|
$container |
|
114
|
2 |
|
->getDefinition(MessageTaskRunner::class) |
|
115
|
2 |
|
->setArgument(0, new Reference($mergedConfig['messenger']['message_bus'])) |
|
116
|
|
|
; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
26 |
|
if ($mergedConfig['mailer']['enabled']) { |
|
120
|
2 |
|
$loader->load('mailer.xml'); |
|
121
|
|
|
|
|
122
|
|
|
$container |
|
123
|
2 |
|
->getDefinition(EmailHandler::class) |
|
124
|
2 |
|
->setArguments([ |
|
125
|
2 |
|
new Reference($mergedConfig['mailer']['service']), |
|
126
|
2 |
|
$mergedConfig['mailer']['default_from'], |
|
127
|
2 |
|
$mergedConfig['mailer']['default_to'], |
|
128
|
2 |
|
$mergedConfig['mailer']['subject_prefix'], |
|
129
|
|
|
]) |
|
130
|
|
|
; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
26 |
|
$this->registerScheduleExtensions($mergedConfig, $container); |
|
134
|
26 |
|
} |
|
135
|
|
|
|
|
136
|
26 |
|
private function registerScheduleExtensions(array $config, ContainerBuilder $container): void |
|
137
|
|
|
{ |
|
138
|
|
|
/** @var Definition[] $definitions */ |
|
139
|
26 |
|
$definitions = []; |
|
140
|
26 |
|
$idPrefix = 'zenstruck_schedule.extension.'; |
|
141
|
|
|
|
|
142
|
26 |
|
if (!empty($config['schedule_extensions']['environments'])) { |
|
143
|
2 |
|
$definitions[$idPrefix.'environments'] = new Definition( |
|
144
|
2 |
|
EnvironmentExtension::class, |
|
145
|
2 |
|
[$config['schedule_extensions']['environments']] |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
26 |
|
if ($config['schedule_extensions']['on_single_server']['enabled']) { |
|
150
|
1 |
|
$definitions[$idPrefix.'on_single_server'] = new Definition( |
|
151
|
1 |
|
SingleServerExtension::class, |
|
152
|
1 |
|
[$config['schedule_extensions']['on_single_server']['ttl']] |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
26 |
|
if ($config['schedule_extensions']['email_on_failure']['enabled']) { |
|
157
|
1 |
|
$definition = new Definition(EmailExtension::class); |
|
158
|
1 |
|
$definition->setFactory([EmailExtension::class, 'scheduleFailure']); |
|
159
|
1 |
|
$definition->setArguments([ |
|
160
|
1 |
|
$config['schedule_extensions']['email_on_failure']['to'], |
|
161
|
1 |
|
$config['schedule_extensions']['email_on_failure']['subject'], |
|
162
|
|
|
]); |
|
163
|
|
|
|
|
164
|
1 |
|
$definitions[$idPrefix.'email_on_failure'] = $definition; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$pingMap = [ |
|
168
|
26 |
|
'ping_before' => 'scheduleBefore', |
|
169
|
|
|
'ping_after' => 'scheduleAfter', |
|
170
|
|
|
'ping_on_success' => 'scheduleSuccess', |
|
171
|
|
|
'ping_on_failure' => 'scheduleFailure', |
|
172
|
|
|
]; |
|
173
|
|
|
|
|
174
|
26 |
|
foreach ($pingMap as $key => $method) { |
|
175
|
26 |
|
if ($config['schedule_extensions'][$key]['enabled']) { |
|
176
|
4 |
|
$definition = new Definition(PingExtension::class); |
|
177
|
4 |
|
$definition->setFactory([PingExtension::class, $method]); |
|
178
|
4 |
|
$definition->setArguments([ |
|
179
|
4 |
|
$config['schedule_extensions'][$key]['url'], |
|
180
|
4 |
|
$config['schedule_extensions'][$key]['method'], |
|
181
|
4 |
|
$config['schedule_extensions'][$key]['options'], |
|
182
|
|
|
]); |
|
183
|
|
|
|
|
184
|
4 |
|
$definitions[$idPrefix.$key] = $definition; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
26 |
|
foreach ($definitions as $definition) { |
|
189
|
8 |
|
$definition->addTag('schedule.extension'); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
26 |
|
$container->addDefinitions($definitions); |
|
193
|
26 |
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|