1212namespace Task \TaskBundle \DependencyInjection ;
1313
1414use Symfony \Component \Config \FileLocator ;
15+ use Symfony \Component \Config \Loader \LoaderInterface ;
1516use Symfony \Component \DependencyInjection \ContainerBuilder ;
1617use Symfony \Component \DependencyInjection \ContainerInterface ;
1718use Symfony \Component \DependencyInjection \Definition ;
@@ -31,23 +32,29 @@ class TaskExtension extends Extension
3132 */
3233 public function load (array $ configs , ContainerBuilder $ container )
3334 {
34- $ configuration = new Configuration ( );
35+ $ configuration = $ this -> getConfiguration ( $ configs , $ container );
3536 $ config = $ this ->processConfiguration ($ configuration , $ configs );
3637
3738 $ container ->setParameter ('task.system_tasks ' , $ config ['system_tasks ' ]);
38- $ container ->setParameter ('task.storage ' , $ config ['storage ' ]);
39+
40+ $ container ->setAlias ('task.lock.storage ' , $ configuration ->getLockingStorageId ($ config ['locking ' ]['storage ' ]));
41+ foreach (array_keys ($ config ['locking ' ]['storages ' ]) as $ key ) {
42+ $ container ->setParameter ('task.lock.storages. ' . $ key , $ config ['locking ' ]['storages ' ][$ key ]);
43+ }
3944
4045 $ loader = new Loader \XmlFileLoader ($ container , new FileLocator (__DIR__ . '/../Resources/config ' ));
4146 $ loader ->load (sprintf ('storage/%s.xml ' , $ config ['storage ' ]));
4247 $ loader ->load ('task_event_listener.xml ' );
4348 $ loader ->load ('scheduler.xml ' );
4449 $ loader ->load ('command.xml ' );
50+ $ loader ->load ('locking/services.xml ' );
4551
4652 if ($ config ['run ' ]['mode ' ] === 'listener ' ) {
4753 $ loader ->load ('listener.xml ' );
4854 }
4955
5056 $ this ->loadDoctrineAdapter ($ config ['adapters ' ]['doctrine ' ], $ container );
57+ $ this ->loadLockingComponent ($ config ['locking ' ], $ container , $ loader );
5158 }
5259
5360 /**
@@ -70,4 +77,53 @@ private function loadDoctrineAdapter(array $config, ContainerBuilder $container)
7077 $ container ->setDefinition ('task.adapter.doctrine.execution_listener ' , $ definition );
7178 }
7279 }
80+
81+ /**
82+ * Load services for locking component.
83+ *
84+ * @param array $config
85+ * @param LoaderInterface $loader
86+ * @param ContainerBuilder $container
87+ */
88+ private function loadLockingComponent (array $ config , ContainerBuilder $ container , LoaderInterface $ loader )
89+ {
90+ if (!$ config ['enabled ' ]) {
91+ return $ loader ->load ('locking/null.xml ' );
92+ }
93+
94+ $ loader ->load ('locking/services.xml ' );
95+ $ container ->setParameter ('task.lock.ttl ' , $ config ['ttl ' ]);
96+ }
97+
98+ /**
99+ * Find storage aliases and related ids.
100+ *
101+ * @param ContainerBuilder $container
102+ *
103+ * @return array
104+ */
105+ private function getLockingStorageAliases (ContainerBuilder $ container )
106+ {
107+ $ taggedServices = $ container ->findTaggedServiceIds ('task.lock.storage ' );
108+
109+ $ result = [];
110+ foreach ($ taggedServices as $ id => $ tags ) {
111+ foreach ($ tags as $ tag ) {
112+ $ result [$ tag ['alias ' ]] = $ id ;
113+ }
114+ }
115+
116+ return $ result ;
117+ }
118+
119+ /**
120+ * {@inheritdoc}
121+ */
122+ public function getConfiguration (array $ config , ContainerBuilder $ container )
123+ {
124+ $ loader = new Loader \XmlFileLoader ($ container , new FileLocator (__DIR__ . '/../Resources/config ' ));
125+ $ loader ->load ('locking/storages.xml ' );
126+
127+ return new Configuration ($ this ->getLockingStorageAliases ($ container ));
128+ }
73129}
0 commit comments