[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/console/src/Loader/ -> ContainerLoader.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Console Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Console\Loader;
  10  
  11  use Joomla\Console\Command\AbstractCommand;
  12  use Psr\Container\ContainerInterface;
  13  use Symfony\Component\Console\Exception\CommandNotFoundException;
  14  
  15  /**
  16   * PSR-11 compatible command loader.
  17   *
  18   * @since  2.0.0
  19   */
  20  final class ContainerLoader implements LoaderInterface
  21  {
  22      /**
  23       * The service container.
  24       *
  25       * @var    ContainerInterface
  26       * @since  2.0.0
  27       */
  28      private $container;
  29  
  30      /**
  31       * The command name to service ID map.
  32       *
  33       * @var    string[]
  34       * @since  2.0.0
  35       */
  36      private $commandMap;
  37  
  38      /**
  39       * Constructor.
  40       *
  41       * @param   ContainerInterface  $container   A container from which to load command services.
  42       * @param   array               $commandMap  An array with command names as keys and service IDs as values.
  43       *
  44       * @since   2.0.0
  45       */
  46  	public function __construct(ContainerInterface $container, array $commandMap)
  47      {
  48          $this->container  = $container;
  49          $this->commandMap = $commandMap;
  50      }
  51  
  52      /**
  53       * Loads a command.
  54       *
  55       * @param   string  $name  The command to load.
  56       *
  57       * @return  AbstractCommand
  58       *
  59       * @since   2.0.0
  60       * @throws  CommandNotFoundException
  61       */
  62  	public function get(string $name): AbstractCommand
  63      {
  64          if (!$this->has($name))
  65          {
  66              throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));
  67          }
  68  
  69          return $this->container->get($this->commandMap[$name]);
  70      }
  71  
  72      /**
  73       * Get the names of the registered commands.
  74       *
  75       * @return  string[]
  76       *
  77       * @since   2.0.0
  78       */
  79  	public function getNames(): array
  80      {
  81          return array_keys($this->commandMap);
  82      }
  83  
  84      /**
  85       * Checks if a command exists.
  86       *
  87       * @param   string  $name  The command to check.
  88       *
  89       * @return  boolean
  90       *
  91       * @since   2.0.0
  92       */
  93  	public function has($name): bool
  94      {
  95          return isset($this->commandMap[$name]) && $this->container->has($this->commandMap[$name]);
  96      }
  97  }


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