|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of php-task library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) php-task |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Task\TaskBundle\DependencyInjection; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Config\FileLocator; |
|
15
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
21
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
22
|
|
|
use Task\Event\Events; |
|
23
|
|
|
use Task\TaskBundle\EventListener\DoctrineTaskExecutionListener; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Container extension for php-task library. |
|
27
|
|
|
*/ |
|
28
|
|
|
class TaskExtension extends Extension |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
30 |
|
public function load(array $configs, ContainerBuilder $container) |
|
34
|
|
|
{ |
|
35
|
30 |
|
$configuration = $this->getConfiguration($configs, $container); |
|
36
|
30 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
30 |
|
$container->setParameter('task.system_tasks', $config['system_tasks']); |
|
39
|
|
|
|
|
40
|
30 |
|
$container->setAlias('task.lock.storage', $configuration->getLockingStorageId($config['locking']['storage'])); |
|
41
|
30 |
|
foreach (array_keys($config['locking']['storages']) as $key) { |
|
42
|
30 |
|
$container->setParameter('task.lock.storages.' . $key, $config['locking']['storages'][$key]); |
|
43
|
30 |
|
} |
|
44
|
|
|
|
|
45
|
30 |
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
|
46
|
30 |
|
$loader->load(sprintf('storage/%s.xml', $config['storage'])); |
|
47
|
30 |
|
$loader->load('task_event_listener.xml'); |
|
48
|
30 |
|
$loader->load('scheduler.xml'); |
|
49
|
30 |
|
$loader->load('command.xml'); |
|
50
|
30 |
|
$loader->load('locking/services.xml'); |
|
51
|
|
|
|
|
52
|
30 |
|
if ($config['run']['mode'] === 'listener') { |
|
53
|
|
|
$loader->load('listener.xml'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
30 |
|
$this->loadDoctrineAdapter($config['adapters']['doctrine'], $container); |
|
57
|
30 |
|
$this->loadLockingComponent($config['locking'], $container, $loader); |
|
58
|
30 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Load doctrine adapter. |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $config |
|
64
|
|
|
* @param ContainerBuilder $container |
|
65
|
|
|
*/ |
|
66
|
30 |
|
private function loadDoctrineAdapter(array $config, ContainerBuilder $container) |
|
67
|
|
|
{ |
|
68
|
30 |
|
if ($config['clear']) { |
|
69
|
30 |
|
$definition = new Definition( |
|
70
|
30 |
|
DoctrineTaskExecutionListener::class, |
|
71
|
30 |
|
[new Reference('doctrine.orm.entity_manager', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)] |
|
72
|
30 |
|
); |
|
73
|
30 |
|
$definition->addTag( |
|
74
|
30 |
|
'kernel.event_listener', |
|
75
|
30 |
|
['event' => Events::TASK_AFTER, 'method' => 'clearEntityManagerAfterTask'] |
|
76
|
30 |
|
); |
|
77
|
30 |
|
$container->setDefinition('task.adapter.doctrine.execution_listener', $definition); |
|
78
|
30 |
|
} |
|
79
|
30 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Load services for locking component. |
|
83
|
|
|
* |
|
84
|
|
|
* @param array $config |
|
85
|
|
|
* @param LoaderInterface $loader |
|
86
|
|
|
* @param ContainerBuilder $container |
|
87
|
|
|
*/ |
|
88
|
30 |
|
private function loadLockingComponent(array $config, ContainerBuilder $container, LoaderInterface $loader) |
|
89
|
|
|
{ |
|
90
|
30 |
|
if (!$config['enabled']) { |
|
91
|
|
|
return $loader->load('locking/null.xml'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
30 |
|
$loader->load('locking/services.xml'); |
|
95
|
30 |
|
$container->setParameter('task.lock.ttl', $config['ttl']); |
|
96
|
30 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Find storage aliases and related ids. |
|
100
|
|
|
* |
|
101
|
|
|
* @param ContainerBuilder $container |
|
102
|
|
|
* |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
30 |
|
private function getLockingStorageAliases(ContainerBuilder $container) |
|
106
|
|
|
{ |
|
107
|
30 |
|
$taggedServices = $container->findTaggedServiceIds('task.lock.storage'); |
|
108
|
|
|
|
|
109
|
30 |
|
$result = []; |
|
110
|
30 |
|
foreach ($taggedServices as $id => $tags) { |
|
111
|
30 |
|
foreach ($tags as $tag) { |
|
112
|
30 |
|
$result[$tag['alias']] = $id; |
|
113
|
30 |
|
} |
|
114
|
30 |
|
} |
|
115
|
|
|
|
|
116
|
30 |
|
return $result; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritdoc} |
|
121
|
|
|
*/ |
|
122
|
30 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
|
123
|
|
|
{ |
|
124
|
30 |
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
|
125
|
30 |
|
$loader->load('locking/storages.xml'); |
|
126
|
|
|
|
|
127
|
30 |
|
return new Configuration($this->getLockingStorageAliases($container)); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: