1 | <?php |
||
29 | class TaskExtension extends Extension |
||
30 | { |
||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | 2 | public function load(array $configs, ContainerBuilder $container) |
|
35 | { |
||
36 | 2 | $configuration = $this->getConfiguration($configs, $container); |
|
37 | 2 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|||
38 | |||
39 | 2 | $container->setParameter('task.system_tasks', $config['system_tasks']); |
|
40 | |||
41 | 2 | $container->setAlias('task.lock.storage', $configuration->getLockingStorageId($config['locking']['storage'])); |
|
42 | 2 | foreach (array_keys($config['locking']['storages']) as $key) { |
|
43 | 2 | $container->setParameter('task.lock.storages.' . $key, $config['locking']['storages'][$key]); |
|
44 | } |
||
45 | |||
46 | 2 | $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
|
47 | 2 | $loader->load(sprintf('storage/%s.xml', $config['storage'])); |
|
48 | 2 | $loader->load('task_event_listener.xml'); |
|
49 | 2 | $loader->load('scheduler.xml'); |
|
50 | 2 | $loader->load('command.xml'); |
|
51 | 2 | $loader->load('locking/services.xml'); |
|
52 | |||
53 | 2 | if ($config['run']['mode'] === 'listener') { |
|
54 | $loader->load('listener.xml'); |
||
55 | } |
||
56 | |||
57 | 2 | $this->loadDoctrineAdapter($config['adapters']['doctrine'], $container); |
|
58 | 2 | $this->loadLockingComponent($config['locking'], $container, $loader); |
|
59 | 2 | $this->loadExecutorComponent($config['executor'], $container, $loader); |
|
60 | 2 | } |
|
61 | |||
62 | /** |
||
63 | * Load doctrine adapter. |
||
64 | * |
||
65 | * @param array $config |
||
66 | * @param ContainerBuilder $container |
||
67 | */ |
||
68 | 2 | private function loadDoctrineAdapter(array $config, ContainerBuilder $container) |
|
69 | { |
||
70 | 2 | if ($config['clear']) { |
|
71 | 2 | $definition = new Definition( |
|
72 | 2 | DoctrineTaskExecutionListener::class, |
|
73 | 2 | [new Reference('doctrine.orm.entity_manager', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)] |
|
74 | ); |
||
75 | 2 | $definition->addTag( |
|
76 | 2 | 'kernel.event_listener', |
|
77 | 2 | ['event' => Events::TASK_AFTER, 'method' => 'clearEntityManagerAfterTask'] |
|
78 | ); |
||
79 | 2 | $container->setDefinition('task.adapter.doctrine.execution_listener', $definition); |
|
80 | } |
||
81 | 2 | } |
|
82 | |||
83 | /** |
||
84 | * Load services for locking component. |
||
85 | * |
||
86 | * @param array $config |
||
87 | * @param LoaderInterface $loader |
||
88 | * @param ContainerBuilder $container |
||
89 | */ |
||
90 | 2 | private function loadLockingComponent(array $config, ContainerBuilder $container, LoaderInterface $loader) |
|
91 | { |
||
92 | 2 | if (!$config['enabled'] || 'null' === $config['storage']) { |
|
93 | return $loader->load('locking/null.xml'); |
||
94 | } |
||
95 | |||
96 | 2 | $loader->load('locking/services.xml'); |
|
97 | 2 | $container->setParameter('task.lock.ttl', $config['ttl']); |
|
98 | 2 | } |
|
99 | |||
100 | /** |
||
101 | * Load services for executor component. |
||
102 | * |
||
103 | * @param array $config |
||
104 | * @param LoaderInterface $loader |
||
105 | * @param ContainerBuilder $container |
||
106 | */ |
||
107 | 2 | private function loadExecutorComponent(array $config, ContainerBuilder $container, LoaderInterface $loader) |
|
108 | { |
||
109 | 2 | $loader->load('executor/' . $config['type'] . '.xml'); |
|
110 | 2 | $container->setAlias('task.executor', 'task.executor.' . $config['type']); |
|
111 | |||
112 | 2 | if (!array_key_exists($config['type'], $config)) { |
|
113 | return; |
||
114 | } |
||
115 | |||
116 | 2 | foreach ($config[$config['type']] as $key => $value) { |
|
117 | 2 | $container->setParameter('task.executor.' . $key, $value); |
|
118 | } |
||
119 | |||
120 | 2 | if (!file_exists($container->getParameter('task.executor.console_path'))) { |
|
121 | throw new InvalidConfigurationException( |
||
122 | 'Console file does not exists! Given in "task.executor.seperate.console".' |
||
123 | ); |
||
124 | } |
||
125 | 2 | } |
|
126 | |||
127 | /** |
||
128 | * Find storage aliases and related ids. |
||
129 | * |
||
130 | * @param ContainerBuilder $container |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | 2 | private function getLockingStorageAliases(ContainerBuilder $container) |
|
135 | { |
||
136 | 2 | $taggedServices = $container->findTaggedServiceIds('task.lock.storage'); |
|
137 | |||
138 | 2 | $result = ['null']; |
|
139 | 2 | foreach ($taggedServices as $id => $tags) { |
|
140 | 2 | foreach ($tags as $tag) { |
|
141 | 2 | $result[$tag['alias']] = $id; |
|
142 | } |
||
143 | } |
||
144 | |||
145 | 2 | return $result; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | 2 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
158 | } |
||
159 |
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: