[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Console/Loader/ -> WritableContainerLoader.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2019 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\Loader;
  11  
  12  use Joomla\Console\Command\AbstractCommand;
  13  use Psr\Container\ContainerInterface;
  14  use Symfony\Component\Console\Exception\CommandNotFoundException;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * PSR-11 compatible writable command loader.
  22   *
  23   * @since  4.0.0
  24   */
  25  final class WritableContainerLoader implements WritableLoaderInterface
  26  {
  27      /**
  28       * The service container.
  29       *
  30       * @var    ContainerInterface
  31       * @since  4.0.0
  32       */
  33      private $container;
  34  
  35      /**
  36       * The command name to service ID map.
  37       *
  38       * @var    string[]
  39       * @since  4.0.0
  40       */
  41      private $commandMap;
  42  
  43      /**
  44       * Constructor.
  45       *
  46       * @param   ContainerInterface  $container   A container from which to load command services.
  47       * @param   array               $commandMap  An array with command names as keys and service IDs as values.
  48       *
  49       * @since   4.0.0
  50       */
  51      public function __construct(ContainerInterface $container, array $commandMap)
  52      {
  53          $this->container  = $container;
  54          $this->commandMap = $commandMap;
  55      }
  56  
  57      /**
  58       * Adds a command to the loader.
  59       *
  60       * @param   string  $commandName  The name of the command to load.
  61       * @param   string  $className    The fully qualified class name of the command.
  62       *
  63       * @return  void
  64       *
  65       * @since   4.0.0
  66       */
  67      public function add(string $commandName, string $className)
  68      {
  69          $this->commandMap[$commandName] = $className;
  70      }
  71  
  72      /**
  73       * Loads a command.
  74       *
  75       * @param   string  $name  The command to load.
  76       *
  77       * @return  AbstractCommand
  78       *
  79       * @since   4.0.0
  80       * @throws  CommandNotFoundException
  81       */
  82      public function get(string $name): AbstractCommand
  83      {
  84          if (!$this->has($name)) {
  85              throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));
  86          }
  87  
  88          return $this->container->get($this->commandMap[$name]);
  89      }
  90  
  91      /**
  92       * Get the names of the registered commands.
  93       *
  94       * @return  string[]
  95       *
  96       * @since   4.0.0
  97       */
  98      public function getNames(): array
  99      {
 100          return array_keys($this->commandMap);
 101      }
 102  
 103      /**
 104       * Checks if a command exists.
 105       *
 106       * @param   string  $name  The command to check.
 107       *
 108       * @return  boolean
 109       *
 110       * @since   4.0.0
 111       */
 112      public function has($name): bool
 113      {
 114          return isset($this->commandMap[$name]) && $this->container->has($this->commandMap[$name]);
 115      }
 116  }


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