AddConsoleCommandPass.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\Bundle\FrameworkBundle\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. /**
  14. * AddConsoleCommandPass.
  15. *
  16. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  17. */
  18. class AddConsoleCommandPass implements CompilerPassInterface
  19. {
  20. public function process(ContainerBuilder $container)
  21. {
  22. $commandServices = $container->findTaggedServiceIds('console.command');
  23. foreach ($commandServices as $id => $tags) {
  24. $definition = $container->getDefinition($id);
  25. if (!$definition->isPublic()) {
  26. throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be public.', $id));
  27. }
  28. if ($definition->isAbstract()) {
  29. throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must not be abstract.', $id));
  30. }
  31. $class = $container->getParameterBag()->resolveValue($definition->getClass());
  32. if (!is_subclass_of($class, 'Symfony\\Component\\Console\\Command\\Command')) {
  33. if (!class_exists($class, false)) {
  34. throw new \InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  35. }
  36. throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "Symfony\\Component\\Console\\Command\\Command".', $id));
  37. }
  38. $container->setAlias('console.command.'.strtolower(str_replace('\\', '_', $class)), $id);
  39. }
  40. $container->setParameter('console.command.ids', array_keys($commandServices));
  41. }
  42. }