/** * @param InputInterface $input * @param OutputInterface $output * @return int|null|void */ protected function execute(InputInterface $input, OutputInterface $output) { $em = $this->getContainer()->get('doctrine.orm.entity_manager'); $name = $input->getArgument('name'); $interval = $input->getArgument('interval'); $intervalType = $input->getArgument('interval-type'); $firstRun = $input->getArgument('first-run'); $inactive = $input->getOption('inactive'); $commands = $input->getOption('command'); // Check if the name doesn't already exists $crontasks = $em->getRepository('MeyfarthCrontaskBundle:Crontask')->findBy(array('name' => $name)); if (count($crontasks) > 0) { $output->writeln('<error>There already is a crontask with this name</error>'); exit; } $this->dialog = $this->getHelperSet()->get('dialog'); $this->output = $output; $interval = $this->checkArgument($interval, self::TYPE_ARGUMENT_INTEGER, 'Insert interval between two runs (integer only) :'); $intervalType = $this->checkArgument($intervalType, self::TYPE_ARGUMENT_STRING, 'Insert the type interval. Allowed values : [(h)ours, (min)utes or (sec)onds] :', false, array('h', 'hour', 'hours', 'm', 'min', 'minute', 'minutes', 's', 'sec', 'second', 'seconds')); $firstRun = $this->checkArgument($firstRun, self::TYPE_ARGUMENT_DATETIME, 'Insert the datetime of the first run (or \'null\' if you want it to be executed the nextime your meyfarth:crontask:run command is executed) :', true); // Add comands to the ones given with --command="" options do { $command = $this->dialog->ask($this->output, 'Enter a command to add or press Enter when finished : '); if ($command != '') { $commands[] = $command; } } while ($command != ''); // Confirm generation $output->writeln(''); if ($this->dialog->askConfirmation($this->output, sprintf('<question>Confirm generation of the "%s" crontask (%s) which will run %s commands every %d %s ? </question>', $name, $inactive ? 'inactive' : 'active', '"' . implode('", "', $commands) . '"', $interval, $intervalType), false)) { $crontask = new Crontask(); $crontask->setCommandInterval($interval)->setFirstRun(new \DateTime($firstRun))->setIsActive(!$inactive)->setName($name)->setCommands($commands)->setIntervalType(CrontaskService::convertToIntervalType($intervalType)); $output->writeln('<comment>Crontask "' . $crontask . '" successfully created</comment>'); $em->persist($crontask); $em->flush(); } else { $output->writeln('<fg=red>Crontask creation aborted</fg=red>'); } }
/** * @param InputInterface $input * @param OutputInterface $output * @return int|null|void */ protected function execute(InputInterface $input, OutputInterface $output) { $em = $this->getContainer()->get('doctrine.orm.entity_manager'); $name = $input->getArgument('name'); $nextName = $input->getOption('name'); $isActive = $input->getOption('is-active'); $intervalType = $input->getOption('interval-type'); $interval = $input->getOption('interval'); $firstRun = $input->getOption('first-run'); // Check if the name doesn't already exists $crontask = $em->getRepository('MeyfarthCrontaskBundle:Crontask')->findOneBy(array('name' => $name)); if (!$crontask) { $output->writeln('<error>No crontask found</error>'); exit; } // Check data types if ($isActive != null) { if (!in_array($isActive, array(0, 1))) { $output->writeln('<error>is-active must be either 0 (inactive) or 1 (active)'); exit; } } if ($intervalType != null) { $allowedIntervalType = array('h', 'hour', 'hours', 'm', 'min', 'minute', 'minutes', 's', 'sec', 'second', 'seconds'); if (!in_array($intervalType, $allowedIntervalType)) { $output->writeln(sprintf('<error>The interval type must be one of the following values : "%s"</error>', implode('", "', $allowedIntervalType))); exit; } } if ($interval != null) { if (!ctype_digit($interval)) { $output->writeln('<error>The interval must be an integer</error>'); exit; } } if ($firstRun != null) { if (!CrontaskService::ValidateDate($firstRun, CrontaskService::DATE_FORMAT)) { $output->writeln('<error>The first-run must use the "Y-m-d H:i" format</error>'); exit; } } $confirm = '<question>Confirm update ... </question>'; $outputConfirmation = array(); if ($nextName != null) { $outputConfirmation[] = sprintf('Name: %s => %s', $crontask->getName(), $nextName); } if ($isActive != null) { $outputConfirmation[] = sprintf('Active: %s => %s', $crontask->getIsActive() ? 'active' : 'inactive', $isActive ? 'active' : 'inactive'); } if ($intervalType != null) { $outputConfirmation[] = sprintf('Interval type: %s => %s', CrontaskService::convertFromTypeInterval($crontask->getIntervalType()), CrontaskService::convertFromTypeInterval(CrontaskService::convertToIntervalType($intervalType))); } if ($interval != null) { $outputConfirmation[] = sprintf('Interval: %s => %s', $crontask->getCommandInterval(), $interval); } if ($firstRun != null) { $outputConfirmation[] = sprintf('First run: %s => %s', $crontask->getFirstRun()->format(CrontaskService::DATE_FORMAT), $firstRun); } if (count($outputConfirmation) == 0) { $output->writeln('<error>Nothing to update</error>'); } $confirm .= implode(', ', $outputConfirmation) . ' ? '; $this->dialog = $this->getHelperSet()->get('dialog'); // Confirm generation $output->writeln(''); if ($this->dialog->askConfirmation($output, $confirm, false)) { $crontask->setName($nextName != null ? $nextName : $crontask->getName())->setIsActive($isActive != null ? $isActive : $crontask->getIsActive())->setIntervalType($intervalType != null ? CrontaskService::convertToIntervalType($intervalType) : $crontask->getIntervalType())->setCommandInterval($interval != null ? $interval : $crontask->getCommandInterval())->setFirstRun($firstRun != null ? new \DateTime($firstRun) : $crontask->getFirstRun())->setLastRun($firstRun != null ? null : $crontask->getLastRun()); $em->persist($crontask); $em->flush(); } else { $output->writeln('<fg=red>Crontask update aborted</fg=red>'); } }