[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Console/ -> ExtensionsListCommand.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\Console\Command\AbstractCommand;
  13  use Joomla\Database\DatabaseAwareTrait;
  14  use Joomla\Database\DatabaseInterface;
  15  use Symfony\Component\Console\Command\Command;
  16  use Symfony\Component\Console\Input\InputInterface;
  17  use Symfony\Component\Console\Input\InputOption;
  18  use Symfony\Component\Console\Output\OutputInterface;
  19  use Symfony\Component\Console\Style\SymfonyStyle;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('JPATH_PLATFORM') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Console command for listing installed extensions
  27   *
  28   * @since  4.0.0
  29   */
  30  class ExtensionsListCommand extends AbstractCommand
  31  {
  32      use DatabaseAwareTrait;
  33  
  34      /**
  35       * The default command name
  36       *
  37       * @var    string
  38       * @since  4.0.0
  39       */
  40      protected static $defaultName = 'extension:list';
  41  
  42      /**
  43       * Stores the installed Extensions
  44       * @var array
  45       * @since 4.0.0
  46       */
  47      protected $extensions;
  48  
  49      /**
  50       * Stores the Input Object
  51       * @var InputInterface
  52       * @since 4.0.0
  53       */
  54      protected $cliInput;
  55  
  56      /**
  57       * SymfonyStyle Object
  58       * @var   SymfonyStyle
  59       * @since 4.0.0
  60       */
  61      protected $ioStyle;
  62  
  63      /**
  64       * Instantiate the command.
  65       *
  66       * @param   DatabaseInterface  $db  Database connector
  67       *
  68       * @since   4.0.0
  69       */
  70      public function __construct(DatabaseInterface $db)
  71      {
  72          parent::__construct();
  73  
  74          $this->setDatabase($db);
  75      }
  76  
  77      /**
  78       * Configures the IO
  79       *
  80       * @param   InputInterface   $input   Console Input
  81       * @param   OutputInterface  $output  Console Output
  82       *
  83       * @return void
  84       *
  85       * @since 4.0.0
  86       *
  87       */
  88      protected function configureIO(InputInterface $input, OutputInterface $output): void
  89      {
  90          $this->cliInput = $input;
  91          $this->ioStyle = new SymfonyStyle($input, $output);
  92      }
  93  
  94      /**
  95       * Initialise the command.
  96       *
  97       * @return  void
  98       *
  99       * @since   4.0.0
 100       */
 101      protected function configure(): void
 102      {
 103  
 104          $this->addOption('type', null, InputOption::VALUE_REQUIRED, 'Type of the extension');
 105  
 106          $help = "<info>%command.name%</info> lists all installed extensions
 107          \nUsage: <info>php %command.full_name% <extension_id></info>
 108          \nYou may filter on the type of extension (component, module, plugin, etc.) using the <info>--type</info> option:
 109          \n  <info>php %command.full_name% --type=<type></info>";
 110  
 111          $this->setDescription('List installed extensions');
 112          $this->setHelp($help);
 113      }
 114  
 115      /**
 116       * Retrieves all extensions
 117       *
 118       * @return mixed
 119       *
 120       * @since 4.0.0
 121       */
 122      public function getExtensions()
 123      {
 124          if (!$this->extensions) {
 125              $this->setExtensions();
 126          }
 127  
 128          return $this->extensions;
 129      }
 130  
 131      /**
 132       * Retrieves the extension from the model and sets the class variable
 133       *
 134       * @param   null  $extensions  Array of extensions
 135       *
 136       * @return void
 137       *
 138       * @since 4.0.0
 139       */
 140      public function setExtensions($extensions = null): void
 141      {
 142          if (!$extensions) {
 143              $this->extensions = $this->getAllExtensionsFromDB();
 144          } else {
 145              $this->extensions = $extensions;
 146          }
 147      }
 148  
 149      /**
 150       * Retrieves extension list from DB
 151       *
 152       * @return array
 153       *
 154       * @since 4.0.0
 155       */
 156      private function getAllExtensionsFromDB(): array
 157      {
 158          $db    = $this->getDatabase();
 159          $query = $db->getQuery(true);
 160          $query->select('*')
 161              ->from('#__extensions');
 162          $db->setQuery($query);
 163          $extensions = $db->loadAssocList('extension_id');
 164  
 165          return $extensions;
 166      }
 167  
 168      /**
 169       * Transforms extension arrays into required form
 170       *
 171       * @param   array  $extensions  Array of extensions
 172       *
 173       * @return array
 174       *
 175       * @since 4.0.0
 176       */
 177      protected function getExtensionsNameAndId($extensions): array
 178      {
 179          $extInfo = [];
 180  
 181          foreach ($extensions as $key => $extension) {
 182              $manifest = json_decode($extension['manifest_cache']);
 183              $extInfo[] = [
 184                  $extension['name'],
 185                  $extension['extension_id'],
 186                  $manifest ? $manifest->version : '--',
 187                  $extension['type'],
 188                  $extension['enabled'] == 1 ? 'Yes' : 'No',
 189              ];
 190          }
 191  
 192          return $extInfo;
 193      }
 194  
 195      /**
 196       * Filters the extension type
 197       *
 198       * @param   string  $type  Extension type
 199       *
 200       * @return array
 201       *
 202       * @since 4.0.0
 203       */
 204      private function filterExtensionsBasedOn($type): array
 205      {
 206          $extensions = [];
 207  
 208          foreach ($this->extensions as $key => $extension) {
 209              if ($extension['type'] == $type) {
 210                  $extensions[] = $extension;
 211              }
 212          }
 213  
 214          return $extensions;
 215      }
 216  
 217      /**
 218       * Internal function to execute the command.
 219       *
 220       * @param   InputInterface   $input   The input to inject into the command.
 221       * @param   OutputInterface  $output  The output to inject into the command.
 222       *
 223       * @return  integer  The command exit code
 224       *
 225       * @since   4.0.0
 226       */
 227      protected function doExecute(InputInterface $input, OutputInterface $output): int
 228      {
 229          $this->configureIO($input, $output);
 230          $extensions = $this->getExtensions();
 231          $type = $this->cliInput->getOption('type');
 232  
 233          if ($type) {
 234              $extensions = $this->filterExtensionsBasedOn($type);
 235          }
 236  
 237          if (empty($extensions)) {
 238              $this->ioStyle->error("Cannot find extensions of the type '$type' specified.");
 239  
 240              return Command::SUCCESS;
 241          }
 242  
 243          $extensions = $this->getExtensionsNameAndId($extensions);
 244  
 245          $this->ioStyle->title('Installed Extensions');
 246          $this->ioStyle->table(['Name', 'Extension ID', 'Version', 'Type', 'Enabled'], $extensions);
 247  
 248          return Command::SUCCESS;
 249      }
 250  }


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