コード例 #1
0
 /**
  * check the argument type and format, if not correct, send null, else send back the argument
  * @param mixed $argument
  * @param string $type
  * @param array|null $allowedValues
  * @return mixed|null
  */
 private function checkTypeOrNull($argument, $type, array $allowedValues = null)
 {
     if ($type == self::TYPE_ARGUMENT_INTEGER) {
         if (!ctype_digit($argument)) {
             $argument = null;
         }
     } elseif ($type == self::TYPE_ARGUMENT_DATETIME) {
         if (CrontaskService::ValidateDate($argument, CrontaskService::DATE_FORMAT)) {
             $argument = null;
         }
     }
     if ($allowedValues != null && !in_array($argument, $allowedValues)) {
         $argument = null;
     }
     return $argument;
 }
コード例 #2
0
 /**
  * @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>');
     }
 }