diff --git a/Command/DumpCommand.php b/Command/DumpCommand.php index 105f592..2e53f55 100644 --- a/Command/DumpCommand.php +++ b/Command/DumpCommand.php @@ -15,7 +15,7 @@ class DumpCommand extends ConfigurationAwareCommand protected function configure() { parent::configure(); - + $this ->setName('cron:dump') ->setDescription('Dumps jobs to a crontab file') @@ -29,12 +29,20 @@ protected function execute(InputInterface $input, OutputInterface $output) { parent::execute($input, $output); - $group = $input->getOption('group'); + $this->dump($input); + } + /** + * @param InputInterface $input + * + * @return string + */ + protected function dump(InputInterface $input) + { $reader = new AnnotationReader(); $helper = new Helper($this->getApplication(), $reader); - $tab = $helper->read($input, $group); + $tab = $helper->read($input, $input->getOption('group'), $this->getConfiguration()); $path = strtolower( sprintf( @@ -43,5 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output) ) ); file_put_contents($path, (string) $tab); + + return $path; } } diff --git a/Command/ImportCommand.php b/Command/ImportCommand.php index d66b2d1..e601522 100644 --- a/Command/ImportCommand.php +++ b/Command/ImportCommand.php @@ -9,7 +9,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Process\Process; -class ImportCommand extends ConfigurationAwareCommand +class ImportCommand extends DumpCommand { /** * {@inheritdoc} @@ -32,22 +32,9 @@ protected function execute(InputInterface $input, OutputInterface $output) { parent::execute($input, $output); - $user = $input->getOption('user'); - $group = $input->getOption('group'); - - $reader = new AnnotationReader(); - $helper = new Helper($this->getApplication(), $reader); + $path = $this->dump($input); - $path = strtolower( - sprintf( - '%s/%s-%s.crontab', - sys_get_temp_dir(), - $this->getApplication()->getName(), - time() - ) - ); - $content = $helper->read($input, $group); - file_put_contents($path, (string) $content); + $user = $input->getOption('user'); $command = sprintf( 'crontab%s %s', @@ -63,6 +50,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln($process->getErrorOutput()); } + unlink($path); + return $process->getExitCode(); } } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 2e19f42..62f9aa0 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -25,6 +25,10 @@ public function getConfigTreeBuilder() ->defaultValue('') ->info('Cron log directory') ->end() + ->scalarNode('path') + ->defaultValue(null) + ->info('Cron path variable') + ->end() ->end() ; diff --git a/Util/Helper.php b/Util/Helper.php index 5bdae6f..f83e94a 100644 --- a/Util/Helper.php +++ b/Util/Helper.php @@ -31,11 +31,12 @@ public function __construct(Application $application, AnnotationReader $annotati /** * @param InputInterface $input - * @param null $group + * @param string|null $group + * @param array|null $config * * @return Tab */ - public function read(InputInterface $input = null, $group = null) + public function read(InputInterface $input = null, $group = null, array $config = null) { $tab = new Tab(); @@ -68,6 +69,10 @@ public function read(InputInterface $input = null, $group = null) $vars['SYMFONY_ENV'] = $input->getOption('env'); $vars['MAILTO'] = $input->getOption('mailto'); + if ($config != null && isset($config['path']) && $config['path'] != null) { + $vars['PATH'] = $config['path']; + } + return $tab; } }