- md5
- c9abe46c63b78738fbdc99b80441d3ec
- sha1
- 97c90ee07d99ebd9832ddced020f826524d94855
- sha256
- d42c8a9680fc04ccd13eb7fce575eb26d5949ea9919720442fc36010fd4b064c
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
<?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Console\Loader;
use Joomla\Console\Command\AbstractCommand;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Exception\CommandNotFoundException;
/**
* PSR-11 compatible writable command loader.
*
* @since 4.0.0
*/
final class WritableContainerLoader implements WritableLoaderInterface
{
/**
* The service container.
*
* @var ContainerInterface
* @since 4.0.0
*/
private $container;
/**
* The command name to service ID map.
*
* @var string[]
* @since 4.0.0
*/
private $commandMap;
/**
* Constructor.
*
* @param ContainerInterface $container A container from which to load command services.
* @param array $commandMap An array with command names as keys and service IDs as values.
*
* @since 4.0.0
*/
public function __construct(ContainerInterface $container, array $commandMap)
{
$this->container = $container;
$this->commandMap = $commandMap;
}
/**
* Adds a command to the loader.
*
* @param string $commandName The name of the command to load.
* @param string $className The fully qualified class name of the command.
*
* @return void
*
* @since 4.0.0
*/
public function add(string $commandName, string $className)
{
$this->commandMap[$commandName] = $className;
}
/**
* Loads a command.
*
* @param string $name The command to load.
*
* @return AbstractCommand
*
* @since 4.0.0
* @throws CommandNotFoundException
*/
public function get(string $name): AbstractCommand
{
if (!$this->has($name)) {
throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));
}
return $this->container->get($this->commandMap[$name]);
}
/**
* Get the names of the registered commands.
*
* @return string[]
*
* @since 4.0.0
*/
public function getNames(): array
{
return array_keys($this->commandMap);
}
/**
* Checks if a command exists.
*
* @param string $name The command to check.
*
* @return boolean
*
* @since 4.0.0
*/
public function has($name): bool
{
return isset($this->commandMap[$name]) && $this->container->has($this->commandMap[$name]);
}
}
The same content is in 5 releases, Joomla 4.2.0-beta3 to 4.2.1-rc2. See them all, or every version of this file.