[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2020 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\Installer\Installer;
  13  use Joomla\Console\Command\AbstractCommand;
  14  use Joomla\Database\DatabaseAwareTrait;
  15  use Joomla\Database\DatabaseInterface;
  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 discovering extensions
  28   *
  29   * @since  4.0.0
  30   */
  31  class ExtensionDiscoverInstallCommand extends AbstractCommand
  32  {
  33      use DatabaseAwareTrait;
  34  
  35      /**
  36       * The default command name
  37       *
  38       * @var    string
  39       * @since  4.0.0
  40       */
  41      protected static $defaultName = 'extension:discover:install';
  42  
  43      /**
  44       * Stores the Input Object
  45       *
  46       * @var    InputInterface
  47       * @since  4.0.0
  48       */
  49      private $cliInput;
  50  
  51      /**
  52       * SymfonyStyle Object
  53       *
  54       * @var    SymfonyStyle
  55       * @since  4.0.0
  56       */
  57      private $ioStyle;
  58  
  59      /**
  60       * Instantiate the command.
  61       *
  62       * @param   DatabaseInterface  $db  Database connector
  63       *
  64       * @since   4.0.0
  65       */
  66      public function __construct(DatabaseInterface $db)
  67      {
  68          parent::__construct();
  69  
  70          $this->setDatabase($db);
  71      }
  72  
  73      /**
  74       * Configures the IO
  75       *
  76       * @param   InputInterface   $input   Console Input
  77       * @param   OutputInterface  $output  Console Output
  78       *
  79       * @return  void
  80       *
  81       * @since   4.0.0
  82       *
  83       */
  84      private function configureIO(InputInterface $input, OutputInterface $output): void
  85      {
  86          $this->cliInput = $input;
  87          $this->ioStyle = new SymfonyStyle($input, $output);
  88      }
  89  
  90      /**
  91       * Initialise the command.
  92       *
  93       * @return  void
  94       *
  95       * @since   4.0.0
  96       */
  97      protected function configure(): void
  98      {
  99          $this->addOption('eid', null, InputOption::VALUE_REQUIRED, 'The ID of the extension to discover');
 100  
 101          $help = "<info>%command.name%</info> is used to discover extensions
 102          \nYou can provide the following option to the command:
 103          \n  --eid: The ID of the extension
 104          \n  If you do not provide a ID all discovered extensions are installed.
 105          \nUsage:
 106          \n  <info>php %command.full_name% --eid=<id_of_the_extension></info>";
 107  
 108          $this->setDescription('Install discovered extensions');
 109          $this->setHelp($help);
 110      }
 111  
 112      /**
 113       * Used for discovering extensions
 114       *
 115       * @param   string  $eid  Id of the extension
 116       *
 117       * @return  integer  The count of installed extensions
 118       *
 119       * @throws  \Exception
 120       * @since   4.0.0
 121       */
 122      public function processDiscover($eid): int
 123      {
 124          $jInstaller = new Installer();
 125          $jInstaller->setDatabase($this->getDatabase());
 126          $count = 0;
 127  
 128          if ($eid === -1) {
 129              $db = $this->getDatabase();
 130              $query = $db->getQuery(true)
 131                  ->select($db->quoteName(['extension_id']))
 132                  ->from($db->quoteName('#__extensions'))
 133                  ->where($db->quoteName('state') . ' = -1');
 134              $db->setQuery($query);
 135              $eidsToDiscover = $db->loadObjectList();
 136  
 137              foreach ($eidsToDiscover as $eidToDiscover) {
 138                  if (!$jInstaller->discover_install($eidToDiscover->extension_id)) {
 139                      return -1;
 140                  }
 141  
 142                  $count++;
 143              }
 144  
 145              if (empty($eidsToDiscover)) {
 146                  return 0;
 147              }
 148          } else {
 149              if ($jInstaller->discover_install($eid)) {
 150                  return 1;
 151              } else {
 152                  return -1;
 153              }
 154          }
 155  
 156          return $count;
 157      }
 158  
 159      /**
 160       * Used for finding the text for the note
 161       *
 162       * @param   int  $count   Number of extensions to install
 163       * @param   int  $eid     ID of the extension or -1 if no special
 164       *
 165       * @return  string  The text for the note
 166       *
 167       * @since   4.0.0
 168       */
 169      public function getNote(int $count, int $eid): string
 170      {
 171          if ($count < 0 && $eid >= 0) {
 172              return 'Unable to install the extension with ID ' . $eid;
 173          } elseif ($count < 0 && $eid < 0) {
 174              return 'Unable to install discovered extensions.';
 175          } elseif ($count === 0) {
 176              return 'There are no pending discovered extensions for install. Perhaps you need to run extension:discover first?';
 177          } elseif ($count === 1 && $eid > 0) {
 178              return 'Extension with ID ' . $eid . ' installed successfully.';
 179          } elseif ($count === 1 && $eid < 0) {
 180              return $count . ' discovered extension has been installed.';
 181          } elseif ($count > 1 && $eid < 0) {
 182              return $count . ' discovered extensions have been installed.';
 183          } else {
 184              return 'The return value is not possible and has to be checked.';
 185          }
 186      }
 187  
 188      /**
 189       * Internal function to execute the command.
 190       *
 191       * @param   InputInterface   $input   The input to inject into the command.
 192       * @param   OutputInterface  $output  The output to inject into the command.
 193       *
 194       * @return  integer  The command exit code
 195       *
 196       * @since   4.0.0
 197       */
 198      protected function doExecute(InputInterface $input, OutputInterface $output): int
 199      {
 200          $this->configureIO($input, $output);
 201          $this->ioStyle->title('Install Discovered Extensions');
 202  
 203          if ($eid = $this->cliInput->getOption('eid')) {
 204              $result = $this->processDiscover($eid);
 205  
 206              if ($result === -1) {
 207                  $this->ioStyle->error($this->getNote($result, $eid));
 208  
 209                  return Command::FAILURE;
 210              } else {
 211                  $this->ioStyle->success($this->getNote($result, $eid));
 212  
 213                  return Command::SUCCESS;
 214              }
 215          } else {
 216              $result = $this->processDiscover(-1);
 217  
 218              if ($result < 0) {
 219                  $this->ioStyle->error($this->getNote($result, -1));
 220  
 221                  return Command::FAILURE;
 222              } elseif ($result === 0) {
 223                  $this->ioStyle->note($this->getNote($result, -1));
 224  
 225                  return Command::SUCCESS;
 226              } else {
 227                  $this->ioStyle->note($this->getNote($result, -1));
 228  
 229                  return Command::SUCCESS;
 230              }
 231          }
 232      }
 233  }


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