[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Console/ -> TasksStateCommand.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System.
   5   *
   6   * @copyright  (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Console;
  11  
  12  use Joomla\CMS\Application\ConsoleApplication;
  13  use Joomla\CMS\Factory;
  14  use Joomla\Component\Scheduler\Administrator\Model\TaskModel;
  15  use Joomla\Component\Scheduler\Administrator\Table\TaskTable;
  16  use Joomla\Component\Scheduler\Administrator\Task\Task;
  17  use Joomla\Console\Application;
  18  use Joomla\Console\Command\AbstractCommand;
  19  use Joomla\Utilities\ArrayHelper;
  20  use Symfony\Component\Console\Input\InputInterface;
  21  use Symfony\Component\Console\Input\InputOption;
  22  use Symfony\Component\Console\Output\OutputInterface;
  23  use Symfony\Component\Console\Style\SymfonyStyle;
  24  
  25  // phpcs:disable PSR1.Files.SideEffects
  26  \defined('JPATH_PLATFORM') or die;
  27  // phpcs:enable PSR1.Files.SideEffects
  28  
  29  /**
  30   * Console command to change the state of tasks.
  31   *
  32   * @since 4.1.0
  33   */
  34  class TasksStateCommand extends AbstractCommand
  35  {
  36      /**
  37       * The default command name
  38       *
  39       * @var    string
  40       * @since  4.1.0
  41       */
  42      protected static $defaultName = 'scheduler:state';
  43  
  44      /**
  45       * The console application object
  46       *
  47       * @var Application
  48       *
  49       * @since 4.1.0
  50       */
  51      protected $application;
  52  
  53      /**
  54       * @var SymfonyStyle
  55       *
  56       * @since  4.1.0
  57       */
  58      private $ioStyle;
  59  
  60      /**
  61       * Internal function to execute the command.
  62       *
  63       * @param   InputInterface   $input   The input to inject into the command.
  64       * @param   OutputInterface  $output  The output to inject into the command.
  65       *
  66       * @return  integer  The command exit code
  67       *
  68       * @since   4.1.0
  69       * @throws \Exception
  70       */
  71      protected function doExecute(InputInterface $input, OutputInterface $output): int
  72      {
  73          Factory::getApplication()->getLanguage()->load('joomla', JPATH_ADMINISTRATOR);
  74  
  75          $this->configureIO($input, $output);
  76          $this->ioStyle->title('Change Task State');
  77  
  78          $id = (string) $input->getOption('id');
  79          $state = (string) $input->getOption('state');
  80  
  81          // Try to validate and process ID, if passed
  82          if (\strlen($id)) {
  83              if (!Task::isValidId($id)) {
  84                  $this->ioStyle->error('Invalid id passed!');
  85  
  86                  return 2;
  87              }
  88  
  89              $id = (is_numeric($id)) ? ($id + 0) : $id;
  90          }
  91  
  92          // Try to validate and process state, if passed
  93          if (\strlen($state)) {
  94              // If we get the logical state, we try to get the enumeration (but as a string)
  95              if (!is_numeric($state)) {
  96                  $state = (string) ArrayHelper::arraySearch($state, Task::STATE_MAP);
  97              }
  98  
  99              if (!\strlen($state) || !Task::isValidState($state)) {
 100                  $this->ioStyle->error('Invalid state passed!');
 101  
 102                  return 2;
 103              }
 104          }
 105  
 106          // If we didn't get ID as a flag, ask for it interactively
 107          while (!Task::isValidId($id)) {
 108              $id = $this->ioStyle->ask('Please specify the ID of the task');
 109          }
 110  
 111          // If we didn't get state as a flag, ask for it interactively
 112          while ($state === false || !Task::isValidState($state)) {
 113              $state = (string) $this->ioStyle->ask('Should the state be "enable" (1), "disable" (0) or "trash" (-2)');
 114  
 115              // Ensure we have the enumerated value (still as a string)
 116              $state = (Task::isValidState($state)) ? $state : ArrayHelper::arraySearch($state, Task::STATE_MAP);
 117          }
 118  
 119          // Finally, the enumerated state and id in their pure form
 120          $state = (int) $state;
 121          $id = (int) $id;
 122  
 123          /** @var ConsoleApplication $app */
 124          $app = $this->getApplication();
 125  
 126          /** @var TaskModel $taskModel */
 127          $taskModel = $app->bootComponent('com_scheduler')->getMVCFactory()->createModel('Task', 'Administrator');
 128  
 129          $task = $taskModel->getItem($id);
 130  
 131          // We couldn't fetch that task :(
 132          if (empty($task->id)) {
 133              $this->ioStyle->error("Task ID '$id}' does not exist!");
 134  
 135              return 1;
 136          }
 137  
 138          // If the item is checked-out we need a check in (currently not possible through the CLI)
 139          if ($taskModel->isCheckedOut($task)) {
 140              $this->ioStyle->error("Task ID '$id}' is checked out!");
 141  
 142              return 1;
 143          }
 144  
 145          /** @var TaskTable $table */
 146          $table = $taskModel->getTable();
 147  
 148          $action = Task::STATE_MAP[$state];
 149  
 150          if (!$table->publish($id, $state)) {
 151              $this->ioStyle->error("Can't $action} Task ID '$id}'");
 152  
 153              return 3;
 154          }
 155  
 156          $this->ioStyle->success("Task ID $id} $action}.");
 157  
 158          return 0;
 159      }
 160  
 161      /**
 162       * Configure the IO.
 163       *
 164       * @param   InputInterface   $input   The input to inject into the command.
 165       * @param   OutputInterface  $output  The output to inject into the command.
 166       *
 167       * @return  void
 168       *
 169       * @since  4.1.0
 170       */
 171      private function configureIO(InputInterface $input, OutputInterface $output): void
 172      {
 173          $this->ioStyle = new SymfonyStyle($input, $output);
 174      }
 175  
 176      /**
 177       * Configure the command.
 178       *
 179       * @return  void
 180       *
 181       * @since   4.1.0
 182       */
 183      protected function configure(): void
 184      {
 185          $this->addOption('id', 'i', InputOption::VALUE_REQUIRED, 'The id of the task to change state.');
 186          $this->addOption('state', 's', InputOption::VALUE_REQUIRED, 'The new state of the task, can be 1/enable, 0/disable, or -2/trash.');
 187  
 188          $help = "<info>%command.name%</info> changes the state of a task.
 189          \nUsage: <info>php %command.full_name%</info>";
 190  
 191          $this->setDescription('Enable, disable or trash a scheduled task');
 192          $this->setHelp($help);
 193      }
 194  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer