[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2018 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\Console\Command\AbstractCommand;
  13  use Joomla\DI\ContainerAwareInterface;
  14  use Joomla\DI\ContainerAwareTrait;
  15  use Joomla\Session\SessionInterface;
  16  use Symfony\Component\Console\Command\Command;
  17  use Symfony\Component\Console\Input\InputInterface;
  18  use Symfony\Component\Console\Input\InputOption;
  19  use Symfony\Component\Console\Output\OutputInterface;
  20  use Symfony\Component\Console\Style\SymfonyStyle;
  21  
  22  // phpcs:disable PSR1.Files.SideEffects
  23  \defined('JPATH_PLATFORM') or die;
  24  // phpcs:enable PSR1.Files.SideEffects
  25  
  26  /**
  27   * Console command for performing session garbage collection
  28   *
  29   * @since  4.0.0
  30   */
  31  class SessionGcCommand extends AbstractCommand implements ContainerAwareInterface
  32  {
  33      use ContainerAwareTrait;
  34  
  35      /**
  36       * The default command name
  37       *
  38       * @var    string
  39       * @since  4.0.0
  40       */
  41      protected static $defaultName = 'session:gc';
  42  
  43      /**
  44       * Internal function to execute the command.
  45       *
  46       * @param   InputInterface   $input   The input to inject into the command.
  47       * @param   OutputInterface  $output  The output to inject into the command.
  48       *
  49       * @return  integer  The command exit code
  50       *
  51       * @since   4.0.0
  52       */
  53      protected function doExecute(InputInterface $input, OutputInterface $output): int
  54      {
  55          $symfonyStyle = new SymfonyStyle($input, $output);
  56  
  57          $symfonyStyle->title('Running Session Garbage Collection');
  58  
  59          $session = $this->getSessionService($input->getOption('application'));
  60  
  61          $gcResult = $session->gc();
  62  
  63          // Destroy the session started for this process
  64          $session->destroy();
  65  
  66          if ($gcResult === false) {
  67              $symfonyStyle->error('Garbage collection was not completed. Either the operation failed or it is not supported on your platform.');
  68  
  69              return Command::FAILURE;
  70          }
  71  
  72          $symfonyStyle->success('Garbage collection completed.');
  73  
  74          return Command::SUCCESS;
  75      }
  76  
  77      /**
  78       * Configure the command.
  79       *
  80       * @return  void
  81       *
  82       * @since   4.0.0
  83       */
  84      protected function configure(): void
  85      {
  86          $help = "<info>%command.name%</info> runs PHP's garbage collection operation for session data
  87          \nUsage: <info>php %command.full_name%</info>
  88          \nThis command defaults to performing garbage collection for the frontend (site) application.
  89          \nTo run garbage collection for another application, you can specify it with the <info>--application</info> option.
  90          \nUsage: <info>php %command.full_name% --application=[APPLICATION]</info>";
  91  
  92          $this->setDescription('Perform session garbage collection');
  93          $this->addOption('application', 'app', InputOption::VALUE_OPTIONAL, 'The application to perform garbage collection for.', 'site');
  94          $this->setHelp($help);
  95      }
  96  
  97      /**
  98       * Get the session service for the requested application.
  99       *
 100       * @param   string  $application  The application session service to retrieve
 101       *
 102       * @return  SessionInterface
 103       *
 104       * @since   4.0.0
 105       */
 106      private function getSessionService(string $application): SessionInterface
 107      {
 108          if (!$this->getContainer()->has("session.web.$application")) {
 109              throw new \InvalidArgumentException(
 110                  sprintf(
 111                      'The `%s` application is not a valid option.',
 112                      $application
 113                  )
 114              );
 115          }
 116  
 117          return $this->getContainer()->get("session.web.$application");
 118      }
 119  }


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