[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Console/ -> TasksListCommand.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\Factory;
  13  use Joomla\Component\Scheduler\Administrator\Scheduler\Scheduler;
  14  use Joomla\Console\Application;
  15  use Joomla\Console\Command\AbstractCommand;
  16  use Symfony\Component\Console\Input\InputInterface;
  17  use Symfony\Component\Console\Output\OutputInterface;
  18  use Symfony\Component\Console\Style\SymfonyStyle;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('JPATH_PLATFORM') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Console command to list scheduled tasks.
  26   *
  27   * @since 4.1.0
  28   */
  29  class TasksListCommand extends AbstractCommand
  30  {
  31      /**
  32       * The default command name
  33       *
  34       * @var    string
  35       * @since  4.1.0
  36       */
  37      protected static $defaultName = 'scheduler:list';
  38  
  39      /**
  40       * The console application object
  41       *
  42       * @var Application
  43       * @since 4.1.0
  44       */
  45      protected $application;
  46  
  47      /**
  48       * @var SymfonyStyle
  49       * @since  4.1.0
  50       */
  51      private $ioStyle;
  52  
  53  
  54      /**
  55       * Internal function to execute the command.
  56       *
  57       * @param   InputInterface   $input   The input to inject into the command.
  58       * @param   OutputInterface  $output  The output to inject into the command.
  59       *
  60       * @return  integer  The command exit code
  61       *
  62       * @since   4.1.0
  63       * @throws \Exception
  64       */
  65      protected function doExecute(InputInterface $input, OutputInterface $output): int
  66      {
  67          Factory::getApplication()->getLanguage()->load('joomla', JPATH_ADMINISTRATOR);
  68  
  69          $this->configureIO($input, $output);
  70          $this->ioStyle->title('List Scheduled Tasks');
  71  
  72          $tasks = array_map(
  73              function (\stdClass $task): array {
  74                  $enabled  = $task->state === 1;
  75                  $nextExec = Factory::getDate($task->next_execution, 'UTC');
  76                  $due      = $enabled && $task->taskOption && Factory::getDate('now', 'UTC') > $nextExec;
  77  
  78                  return [
  79                      'id'             => $task->id,
  80                      'title'          => $task->title,
  81                      'type'           => $task->safeTypeTitle,
  82                      'state'          => $task->state === 1 ? 'Enabled' : ($task->state === 0 ? 'Disabled' : 'Trashed'),
  83                      'next_execution' => $due ? 'DUE!' : $nextExec->toRFC822(),
  84                  ];
  85              },
  86              $this->getTasks()
  87          );
  88  
  89          $this->ioStyle->table(['ID', 'Title', 'Type', 'State', 'Next Run'], $tasks);
  90  
  91          return 0;
  92      }
  93  
  94      /**
  95       * Returns a stdClass object array of scheduled tasks.
  96       *
  97       * @return array
  98       *
  99       * @since 4.1.0
 100       * @throws \RunTimeException
 101       */
 102      private function getTasks(): array
 103      {
 104          $scheduler = new Scheduler();
 105  
 106          return $scheduler->fetchTaskRecords(
 107              ['state' => '*'],
 108              ['ordering' => 'a.title', 'select' => 'a.id, a.title, a.type, a.state, a.next_execution']
 109          );
 110      }
 111  
 112      /**
 113       * Configure the IO.
 114       *
 115       * @param   InputInterface   $input   The input to inject into the command.
 116       * @param   OutputInterface  $output  The output to inject into the command.
 117       *
 118       * @return  void
 119       *
 120       * @since  4.1.0
 121       */
 122      private function configureIO(InputInterface $input, OutputInterface $output)
 123      {
 124          $this->ioStyle = new SymfonyStyle($input, $output);
 125      }
 126  
 127      /**
 128       * Configure the command.
 129       *
 130       * @return  void
 131       *
 132       * @since   4.1.0
 133       */
 134      protected function configure(): void
 135      {
 136          $help = "<info>%command.name%</info> lists all scheduled tasks.
 137          \nUsage: <info>php %command.full_name%</info>";
 138  
 139          $this->setDescription('List all scheduled tasks');
 140          $this->setHelp($help);
 141      }
 142  }


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