1313
1414use Symfony \Component \Config \FileLocator ;
1515use Symfony \Component \DependencyInjection \ContainerBuilder ;
16+ use Symfony \Component \DependencyInjection \Definition ;
1617use Symfony \Component \DependencyInjection \Loader ;
1718use Symfony \Component \DependencyInjection \Reference ;
1819use Symfony \Component \HttpKernel \DependencyInjection \Extension ;
20+ use Task \Event \Events ;
21+ use Task \TaskBundle \EventListener \TaskExecutionListener ;
1922
2023/**
2124 * Container extension for php-task library.
@@ -40,12 +43,24 @@ public function load(array $configs, ContainerBuilder $container)
4043 $ loader ->load ('listener.xml ' );
4144 }
4245
43- if ('doctrine ' === $ config ['storage ' ]) {
44- // FIXME move to compiler pass
45- $ container ->getDefinition ('task.command.schedule_task ' )
46- ->addArgument (new Reference ('doctrine.orm.entity_manager ' ));
47- $ container ->getDefinition ('task.command.run ' )
48- ->addArgument (new Reference ('doctrine.orm.entity_manager ' ));
46+ $ this ->loadDoctrineAdapter ($ config ['adapters ' ]['doctrine ' ], $ container );
47+ }
48+
49+ /**
50+ * Load doctrine adapter.
51+ *
52+ * @param array $config
53+ * @param ContainerBuilder $container
54+ */
55+ private function loadDoctrineAdapter (array $ config , ContainerBuilder $ container )
56+ {
57+ if ($ config ['clear ' ]) {
58+ $ definition = new Definition (TaskExecutionListener::class, [new Reference ('doctrine.orm.entity_manager ' )]);
59+ $ definition ->addTag (
60+ 'kernel.event_dispatcher ' ,
61+ ['event ' => Events::TASK_AFTER , 'method ' => 'clearEntityManagerAfterTask ' ]
62+ );
63+ $ container ->setDefinition ('task.adapter.doctrine.execution_listener ' , $ definition );
4964 }
5065 }
5166}
0 commit comments