[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Console/ -> ExtensionInstallCommand.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\CMS\Installer\InstallerHelper;
  14  use Joomla\Console\Command\AbstractCommand;
  15  use Symfony\Component\Console\Input\InputInterface;
  16  use Symfony\Component\Console\Input\InputOption;
  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 for installing extensions
  26   *
  27   * @since  4.0.0
  28   */
  29  class ExtensionInstallCommand extends AbstractCommand
  30  {
  31      /**
  32       * The default command name
  33       *
  34       * @var    string
  35       * @since  4.0.0
  36       */
  37      protected static $defaultName = 'extension:install';
  38  
  39      /**
  40       * Stores the Input Object
  41       * @var InputInterface
  42       * @since 4.0.0
  43       */
  44      private $cliInput;
  45  
  46      /**
  47       * SymfonyStyle Object
  48       * @var SymfonyStyle
  49       * @since 4.0.0
  50       */
  51      private $ioStyle;
  52  
  53      /**
  54       * Exit Code For installation failure
  55       * @since 4.0.0
  56       */
  57      public const INSTALLATION_FAILED = 1;
  58  
  59      /**
  60       * Exit Code For installation Success
  61       * @since 4.0.0
  62       */
  63      public const INSTALLATION_SUCCESSFUL = 0;
  64  
  65      /**
  66       * Configures the IO
  67       *
  68       * @param   InputInterface   $input   Console Input
  69       * @param   OutputInterface  $output  Console Output
  70       *
  71       * @return void
  72       *
  73       * @since 4.0.0
  74       *
  75       */
  76      private function configureIO(InputInterface $input, OutputInterface $output): void
  77      {
  78          $this->cliInput = $input;
  79          $this->ioStyle = new SymfonyStyle($input, $output);
  80      }
  81  
  82      /**
  83       * Initialise the command.
  84       *
  85       * @return  void
  86       *
  87       * @since   4.0.0
  88       */
  89      protected function configure(): void
  90      {
  91          $this->addOption('path', null, InputOption::VALUE_REQUIRED, 'The path to the extension');
  92          $this->addOption('url', null, InputOption::VALUE_REQUIRED, 'The url to the extension');
  93  
  94          $help = "<info>%command.name%</info> is used to install extensions
  95          \nYou must provide one of the following options to the command:
  96          \n  --path: The path on your local filesystem to the install package
  97          \n  --url: The URL from where the install package should be downloaded
  98          \nUsage:
  99          \n  <info>php %command.full_name% --path=<path_to_file></info>
 100          \n  <info>php %command.full_name% --url=<url_to_file></info>";
 101  
 102          $this->setDescription('Install an extension from a URL or from a path');
 103          $this->setHelp($help);
 104      }
 105  
 106      /**
 107       * Used for installing extension from a path
 108       *
 109       * @param   string  $path  Path to the extension zip file
 110       *
 111       * @return boolean
 112       *
 113       * @since 4.0.0
 114       *
 115       * @throws \Exception
 116       */
 117      public function processPathInstallation($path): bool
 118      {
 119          if (!file_exists($path)) {
 120              $this->ioStyle->warning('The file path specified does not exist.');
 121  
 122              return false;
 123          }
 124  
 125          $tmpPath = $this->getApplication()->get('tmp_path');
 126          $tmpPath = $tmpPath . '/' . basename($path);
 127          $package  = InstallerHelper::unpack($path, true);
 128  
 129          if ($package['type'] === false) {
 130              return false;
 131          }
 132  
 133          $jInstaller = Installer::getInstance();
 134          $result     = $jInstaller->install($package['extractdir']);
 135          InstallerHelper::cleanupInstall($tmpPath, $package['extractdir']);
 136  
 137          return $result;
 138      }
 139  
 140  
 141      /**
 142       * Used for installing extension from a URL
 143       *
 144       * @param   string  $url  URL to the extension zip file
 145       *
 146       * @return boolean
 147       *
 148       * @since 4.0.0
 149       *
 150       * @throws \Exception
 151       */
 152      public function processUrlInstallation($url): bool
 153      {
 154          $filename = InstallerHelper::downloadPackage($url);
 155  
 156          $tmpPath = $this->getApplication()->get('tmp_path');
 157  
 158          $path     = $tmpPath . '/' . basename($filename);
 159          $package  = InstallerHelper::unpack($path, true);
 160  
 161          if ($package['type'] === false) {
 162              return false;
 163          }
 164  
 165          $jInstaller = new Installer();
 166          $result     = $jInstaller->install($package['extractdir']);
 167          InstallerHelper::cleanupInstall($path, $package['extractdir']);
 168  
 169          return $result;
 170      }
 171  
 172      /**
 173       * Internal function to execute the command.
 174       *
 175       * @param   InputInterface   $input   The input to inject into the command.
 176       * @param   OutputInterface  $output  The output to inject into the command.
 177       *
 178       * @return  integer  The command exit code
 179       *
 180       * @throws \Exception
 181       * @since   4.0.0
 182       */
 183      protected function doExecute(InputInterface $input, OutputInterface $output): int
 184      {
 185          $this->configureIO($input, $output);
 186          $this->ioStyle->title('Install Extension');
 187  
 188          if ($path = $this->cliInput->getOption('path')) {
 189              $result = $this->processPathInstallation($path);
 190  
 191              if (!$result) {
 192                  $this->ioStyle->error('Unable to install extension');
 193  
 194                  return self::INSTALLATION_FAILED;
 195              }
 196  
 197              $this->ioStyle->success('Extension installed successfully.');
 198  
 199              return self::INSTALLATION_SUCCESSFUL;
 200          } elseif ($url = $this->cliInput->getOption('url')) {
 201              $result = $this->processUrlInstallation($url);
 202  
 203              if (!$result) {
 204                  $this->ioStyle->error('Unable to install extension');
 205  
 206                  return self::INSTALLATION_FAILED;
 207              }
 208  
 209              $this->ioStyle->success('Extension installed successfully.');
 210  
 211              return self::INSTALLATION_SUCCESSFUL;
 212          }
 213  
 214          $this->ioStyle->error('Invalid argument supplied for command.');
 215  
 216          return self::INSTALLATION_FAILED;
 217      }
 218  }


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