[ Index ]

PHP Cross Reference of phpBB-3.3.5-deutsch

title

Body

[close]

/vendor/symfony/console/DependencyInjection/ -> AddConsoleCommandPass.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Console\DependencyInjection;
  13  
  14  use Symfony\Component\Console\Command\Command;
  15  use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
  16  use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  17  use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  18  use Symfony\Component\DependencyInjection\ContainerBuilder;
  19  use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  20  use Symfony\Component\DependencyInjection\TypedReference;
  21  
  22  /**
  23   * Registers console commands.
  24   *
  25   * @author Grégoire Pineau <lyrixx@lyrixx.info>
  26   */
  27  class AddConsoleCommandPass implements CompilerPassInterface
  28  {
  29      private $commandLoaderServiceId;
  30      private $commandTag;
  31  
  32      public function __construct($commandLoaderServiceId = 'console.command_loader', $commandTag = 'console.command')
  33      {
  34          $this->commandLoaderServiceId = $commandLoaderServiceId;
  35          $this->commandTag = $commandTag;
  36      }
  37  
  38      public function process(ContainerBuilder $container)
  39      {
  40          $commandServices = $container->findTaggedServiceIds($this->commandTag, true);
  41          $lazyCommandMap = [];
  42          $lazyCommandRefs = [];
  43          $serviceIds = [];
  44          $lazyServiceIds = [];
  45  
  46          foreach ($commandServices as $id => $tags) {
  47              $definition = $container->getDefinition($id);
  48              $class = $container->getParameterBag()->resolveValue($definition->getClass());
  49  
  50              $commandId = 'console.command.'.strtolower(str_replace('\\', '_', $class));
  51  
  52              if (isset($tags[0]['command'])) {
  53                  $commandName = $tags[0]['command'];
  54              } else {
  55                  if (!$r = $container->getReflectionClass($class)) {
  56                      throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  57                  }
  58                  if (!$r->isSubclassOf(Command::class)) {
  59                      throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class));
  60                  }
  61                  $commandName = $class::getDefaultName();
  62              }
  63  
  64              if (null === $commandName) {
  65                  if (isset($serviceIds[$commandId]) || $container->hasAlias($commandId)) {
  66                      $commandId = $commandId.'_'.$id;
  67                  }
  68                  if (!$definition->isPublic() || $definition->isPrivate()) {
  69                      $container->setAlias($commandId, $id)->setPublic(true);
  70                      $id = $commandId;
  71                  }
  72                  $serviceIds[$commandId] = $id;
  73  
  74                  continue;
  75              }
  76  
  77              $serviceIds[$commandId] = $id;
  78              $lazyServiceIds[$id] = true;
  79              unset($tags[0]);
  80              $lazyCommandMap[$commandName] = $id;
  81              $lazyCommandRefs[$id] = new TypedReference($id, $class);
  82              $aliases = [];
  83  
  84              foreach ($tags as $tag) {
  85                  if (isset($tag['command'])) {
  86                      $aliases[] = $tag['command'];
  87                      $lazyCommandMap[$tag['command']] = $id;
  88                  }
  89              }
  90  
  91              $definition->addMethodCall('setName', [$commandName]);
  92  
  93              if ($aliases) {
  94                  $definition->addMethodCall('setAliases', [$aliases]);
  95              }
  96          }
  97  
  98          $container
  99              ->register($this->commandLoaderServiceId, ContainerCommandLoader::class)
 100              ->setPublic(true)
 101              ->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]);
 102  
 103          $container->setParameter('console.command.ids', $serviceIds);
 104          $container->setParameter('console.lazy_command.ids', $lazyServiceIds);
 105      }
 106  }


Generated: Mon Oct 4 17:42:11 2021 Cross-referenced by PHPXref 0.7.1