3 namespace Symfony\Component\Console\CommandLoader;
5 use Psr\Container\ContainerInterface;
6 use Symfony\Component\Console\Exception\CommandNotFoundException;
9 * Loads commands from a PSR-11 container.
11 * @author Robin Chalas <robin.chalas@gmail.com>
13 class ContainerCommandLoader implements CommandLoaderInterface
19 * @param ContainerInterface $container A container from which to load command services
20 * @param array $commandMap An array with command names as keys and service ids as values
22 public function __construct(ContainerInterface $container, array $commandMap)
24 $this->container = $container;
25 $this->commandMap = $commandMap;
31 public function get($name)
33 if (!$this->has($name)) {
34 throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));
37 return $this->container->get($this->commandMap[$name]);
43 public function has($name)
45 return isset($this->commandMap[$name]) && $this->container->has($this->commandMap[$name]);
51 public function getNames()
53 return array_keys($this->commandMap);