[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Console/ -> ExtensionRemoveCommand.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\Factory;
  13  use Joomla\CMS\Installer\Installer;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\Table\Extension;
  16  use Joomla\Console\Command\AbstractCommand;
  17  use Joomla\Database\DatabaseAwareTrait;
  18  use Joomla\Database\DatabaseInterface;
  19  use Symfony\Component\Console\Input\InputArgument;
  20  use Symfony\Component\Console\Input\InputInterface;
  21  use Symfony\Component\Console\Output\OutputInterface;
  22  use Symfony\Component\Console\Style\SymfonyStyle;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('JPATH_PLATFORM') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Console command for removing extensions
  30   *
  31   * @since  4.0.0
  32   */
  33  class ExtensionRemoveCommand extends AbstractCommand
  34  {
  35      use DatabaseAwareTrait;
  36  
  37      /**
  38       * The default command name
  39       *
  40       * @var    string
  41       * @since  4.0.0
  42       */
  43      protected static $defaultName = 'extension:remove';
  44  
  45      /**
  46       * @var InputInterface
  47       * @since 4.0.0
  48       */
  49      private $cliInput;
  50  
  51      /**
  52       * @var SymfonyStyle
  53       * @since 4.0.0
  54       */
  55      private $ioStyle;
  56  
  57      /**
  58       * Exit Code for extensions remove abort
  59       * @since 4.0.0
  60       */
  61      public const REMOVE_ABORT = 3;
  62  
  63      /**
  64       * Exit Code for extensions remove failure
  65       * @since 4.0.0
  66       */
  67      public const REMOVE_FAILED = 1;
  68  
  69      /**
  70       * Exit Code for invalid response
  71       * @since 4.0.0
  72       */
  73      public const REMOVE_INVALID_RESPONSE = 5;
  74  
  75      /**
  76       * Exit Code for invalid type
  77       * @since 4.0.0
  78       */
  79      public const REMOVE_INVALID_TYPE = 6;
  80  
  81      /**
  82       * Exit Code for extensions locked remove failure
  83       * @since 4.0.0
  84       */
  85      public const REMOVE_LOCKED = 4;
  86  
  87      /**
  88       * Exit Code for extensions not found
  89       * @since 4.0.0
  90       */
  91      public const REMOVE_NOT_FOUND = 2;
  92  
  93      /**
  94       * Exit Code for extensions remove success
  95       * @since 4.0.0
  96       */
  97      public const REMOVE_SUCCESSFUL = 0;
  98  
  99      /**
 100       * Command constructor.
 101       *
 102       * @param   DatabaseInterface  $db  The database
 103       *
 104       * @since   4.2.0
 105       */
 106      public function __construct(DatabaseInterface $db)
 107      {
 108          parent::__construct();
 109  
 110          $this->setDatabase($db);
 111      }
 112  
 113      /**
 114       * Configures the IO
 115       *
 116       * @param   InputInterface   $input   Console Input
 117       * @param   OutputInterface  $output  Console Output
 118       *
 119       * @return void
 120       *
 121       * @since 4.0.0
 122       *
 123       */
 124      private function configureIO(InputInterface $input, OutputInterface $output): void
 125      {
 126          $this->cliInput = $input;
 127          $this->ioStyle = new SymfonyStyle($input, $output);
 128          $language = Factory::getLanguage();
 129          $language->load('', JPATH_ADMINISTRATOR, null, false, false) ||
 130          $language->load('', JPATH_ADMINISTRATOR, null, true);
 131          $language->load('com_installer', JPATH_ADMINISTRATOR, null, false, false) ||
 132          $language->load('com_installer', JPATH_ADMINISTRATOR, null, true);
 133      }
 134  
 135      /**
 136       * Initialise the command.
 137       *
 138       * @return  void
 139       *
 140       * @since   4.0.0
 141       */
 142      protected function configure(): void
 143      {
 144          $this->addArgument(
 145              'extensionId',
 146              InputArgument::REQUIRED,
 147              'ID of extension to be removed (run extension:list command to check)'
 148          );
 149  
 150          $help = "<info>%command.name%</info> is used to uninstall extensions.
 151          \nThe command requires one argument, the ID of the extension to uninstall.
 152          \nYou may find this ID by running the <info>extension:list</info> command.
 153          \nUsage: <info>php %command.full_name% <extension_id></info>";
 154  
 155          $this->setDescription('Remove an extension');
 156          $this->setHelp($help);
 157      }
 158  
 159      /**
 160       * Internal function to execute the command.
 161       *
 162       * @param   InputInterface   $input   The input to inject into the command.
 163       * @param   OutputInterface  $output  The output to inject into the command.
 164       *
 165       * @return  integer  The command exit code
 166       *
 167       * @since   4.0.0
 168       */
 169      protected function doExecute(InputInterface $input, OutputInterface $output): int
 170      {
 171          $this->configureIO($input, $output);
 172          $this->ioStyle->title('Remove Extension');
 173  
 174          $extensionId = $this->cliInput->getArgument('extensionId');
 175  
 176          $response = $this->ioStyle->ask('Are you sure you want to remove this extension?', 'yes/no');
 177  
 178          if (strtolower($response) === 'yes') {
 179              // Get an installer object for the extension type
 180              $installer = Installer::getInstance();
 181              $row       = new Extension($this->getDatabase());
 182  
 183              if ((int) $extensionId === 0 || !$row->load($extensionId)) {
 184                  $this->ioStyle->error("Extension with ID of $extensionId not found.");
 185  
 186                  return self::REMOVE_NOT_FOUND;
 187              }
 188  
 189              // Do not allow to uninstall locked extensions.
 190              if ((int) $row->locked === 1) {
 191                  $this->ioStyle->error(Text::sprintf('COM_INSTALLER_UNINSTALL_ERROR_LOCKED_EXTENSION', $row->name, $extensionId));
 192  
 193                  return self::REMOVE_LOCKED;
 194              }
 195  
 196              if ($row->type) {
 197                  if (!$installer->uninstall($row->type, $extensionId)) {
 198                      $this->ioStyle->error('Extension not removed.');
 199  
 200                      return self::REMOVE_FAILED;
 201                  }
 202  
 203                  $this->ioStyle->success('Extension removed!');
 204  
 205                  return self::REMOVE_SUCCESSFUL;
 206              }
 207  
 208              return self::REMOVE_INVALID_TYPE;
 209          } elseif (strtolower($response) === 'no') {
 210              $this->ioStyle->note('Extension not removed.');
 211  
 212              return self::REMOVE_ABORT;
 213          }
 214  
 215          $this->ioStyle->warning('Invalid response');
 216  
 217          return self::REMOVE_INVALID_RESPONSE;
 218      }
 219  }


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