[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/session/src/Command/ -> CreateSessionTableCommand.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Session Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Session\Command;
  10  
  11  use Joomla\Console\Command\AbstractCommand;
  12  use Joomla\Database\DatabaseInterface;
  13  use Joomla\Session\Exception\CreateSessionTableException;
  14  use Joomla\Session\Exception\UnsupportedDatabaseDriverException;
  15  use Joomla\Session\Handler\DatabaseHandler;
  16  use Symfony\Component\Console\Input\InputInterface;
  17  use Symfony\Component\Console\Output\OutputInterface;
  18  use Symfony\Component\Console\Style\SymfonyStyle;
  19  
  20  /**
  21   * Command used to create the session database table.
  22   *
  23   * @since  2.0.0
  24   */
  25  class CreateSessionTableCommand extends AbstractCommand
  26  {
  27      /**
  28       * The default command name
  29       *
  30       * @var    string
  31       * @since  2.0.0
  32       */
  33      protected static $defaultName = 'session:create-table';
  34  
  35      /**
  36       * Database connector
  37       *
  38       * @var    DatabaseInterface
  39       * @since  2.0.0
  40       */
  41      private $db;
  42  
  43      /**
  44       * Instantiate the command.
  45       *
  46       * @param   DatabaseInterface  $db  Database connector
  47       *
  48       * @since   2.0.0
  49       */
  50  	public function __construct(DatabaseInterface $db)
  51      {
  52          $this->db = $db;
  53  
  54          parent::__construct();
  55      }
  56  
  57      /**
  58       * Configure the command.
  59       *
  60       * @return  void
  61       *
  62       * @since   2.0.0
  63       */
  64  	protected function configure(): void
  65      {
  66          $this->setDescription('Creates the session database table if not already present');
  67      }
  68  
  69      /**
  70       * Internal function to execute the command.
  71       *
  72       * @param   InputInterface   $input   The input to inject into the command.
  73       * @param   OutputInterface  $output  The output to inject into the command.
  74       *
  75       * @return  integer  The command exit code
  76       *
  77       * @since   2.0.0
  78       */
  79  	protected function doExecute(InputInterface $input, OutputInterface $output): int
  80      {
  81          $io = new SymfonyStyle($input, $output);
  82  
  83          $io->title('Create Session Table');
  84  
  85          // Check if the table exists
  86          if (\in_array($this->db->replacePrefix('#__session'), $this->db->getTableList()))
  87          {
  88              $io->success('The session table already exists.');
  89  
  90              return 0;
  91          }
  92  
  93          try
  94          {
  95              (new DatabaseHandler($this->db))->createDatabaseTable();
  96          }
  97          catch (UnsupportedDatabaseDriverException $exception)
  98          {
  99              $io->error($exception->getMessage());
 100  
 101              return 1;
 102          }
 103          catch (CreateSessionTableException $exception)
 104          {
 105              $io->error(\sprintf('The session table could not be created: %s', $exception->getMessage()));
 106  
 107              return 1;
 108          }
 109  
 110          $io->success('The session table has been created.');
 111  
 112          return 0;
 113      }
 114  }


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