From 422d1c74c45fc60989b270bc3ee34b322955b1aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Thu, 25 Sep 2014 21:46:02 +0200 Subject: [PATCH] Added possibility to define tasks in extension section in config file --- Cronner/DI/CronnerExtension.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Cronner/DI/CronnerExtension.php b/Cronner/DI/CronnerExtension.php index e02ce5d..efab144 100644 --- a/Cronner/DI/CronnerExtension.php +++ b/Cronner/DI/CronnerExtension.php @@ -5,6 +5,8 @@ use Nette\Configurator; use Nette\DI\Compiler; use Nette\DI\CompilerExtension; +use Nette\DI\Statement; +use Nette\Utils\Json; use Nette\Utils\Validators; if (!class_exists('Nette\DI\CompilerExtension')) { @@ -33,6 +35,7 @@ class CronnerExtension extends CompilerExtension 'timestampStorage' => NULL, 'maxExecutionTime' => NULL, 'criticalSectionTempDir' => "%tempDir%/critical-section", + 'tasks' => array(), ); @@ -81,6 +84,22 @@ public function loadConfiguration() array_key_exists('debugMode', $config) ? !$config['debugMode'] : TRUE, )); + Validators::assert($config['tasks'], 'array'); + foreach ($config['tasks'] as $task) { + $def = $container->addDefinition($this->prefix('task.' . md5(Json::encode($task)))); + list($def->factory) = Compiler::filterArguments(array( + is_string($task) ? new Statement($task) : $task + )); + + if (class_exists($def->factory->entity)) { + $def->class = $def->factory->entity; + } + + $def->setAutowired(FALSE); + $def->setInject(FALSE); + $def->addTag(self::TASKS_TAG); + } + foreach (array_keys($container->findByTag(self::TASKS_TAG)) as $serviceName) { $runner->addSetup('addTasks', array('@' . $serviceName)); }