You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.5 KiB

3 years ago
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\CommandLoader;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\Console\Exception\CommandNotFoundException;
  13. /**
  14. * Loads commands from a PSR-11 container.
  15. *
  16. * @author Robin Chalas <robin.chalas@gmail.com>
  17. */
  18. class ContainerCommandLoader implements CommandLoaderInterface
  19. {
  20. private $container;
  21. private $commandMap;
  22. /**
  23. * @param array $commandMap An array with command names as keys and service ids as values
  24. */
  25. public function __construct(ContainerInterface $container, array $commandMap)
  26. {
  27. $this->container = $container;
  28. $this->commandMap = $commandMap;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function get(string $name)
  34. {
  35. if (!$this->has($name)) {
  36. throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));
  37. }
  38. return $this->container->get($this->commandMap[$name]);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function has(string $name)
  44. {
  45. return isset($this->commandMap[$name]) && $this->container->has($this->commandMap[$name]);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getNames()
  51. {
  52. return array_keys($this->commandMap);
  53. }
  54. }