[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/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 <[email protected]>
   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\Command\LazyCommand;
  16  use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
  17  use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
  18  use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  19  use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  20  use Symfony\Component\DependencyInjection\ContainerBuilder;
  21  use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  22  use Symfony\Component\DependencyInjection\Reference;
  23  use Symfony\Component\DependencyInjection\TypedReference;
  24  
  25  /**
  26   * Registers console commands.
  27   *
  28   * @author GrĂ©goire Pineau <[email protected]>
  29   */
  30  class AddConsoleCommandPass implements CompilerPassInterface
  31  {
  32      private $commandLoaderServiceId;
  33      private $commandTag;
  34      private $noPreloadTag;
  35      private $privateTagName;
  36  
  37      public function __construct(string $commandLoaderServiceId = 'console.command_loader', string $commandTag = 'console.command', string $noPreloadTag = 'container.no_preload', string $privateTagName = 'container.private')
  38      {
  39          if (0 < \func_num_args()) {
  40              trigger_deprecation('symfony/console', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
  41          }
  42  
  43          $this->commandLoaderServiceId = $commandLoaderServiceId;
  44          $this->commandTag = $commandTag;
  45          $this->noPreloadTag = $noPreloadTag;
  46          $this->privateTagName = $privateTagName;
  47      }
  48  
  49      public function process(ContainerBuilder $container)
  50      {
  51          $commandServices = $container->findTaggedServiceIds($this->commandTag, true);
  52          $lazyCommandMap = [];
  53          $lazyCommandRefs = [];
  54          $serviceIds = [];
  55  
  56          foreach ($commandServices as $id => $tags) {
  57              $definition = $container->getDefinition($id);
  58              $definition->addTag($this->noPreloadTag);
  59              $class = $container->getParameterBag()->resolveValue($definition->getClass());
  60  
  61              if (isset($tags[0]['command'])) {
  62                  $aliases = $tags[0]['command'];
  63              } else {
  64                  if (!$r = $container->getReflectionClass($class)) {
  65                      throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  66                  }
  67                  if (!$r->isSubclassOf(Command::class)) {
  68                      throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class));
  69                  }
  70                  $aliases = $class::getDefaultName();
  71              }
  72  
  73              $aliases = explode('|', $aliases ?? '');
  74              $commandName = array_shift($aliases);
  75  
  76              if ($isHidden = '' === $commandName) {
  77                  $commandName = array_shift($aliases);
  78              }
  79  
  80              if (null === $commandName) {
  81                  if (!$definition->isPublic() || $definition->isPrivate() || $definition->hasTag($this->privateTagName)) {
  82                      $commandId = 'console.command.public_alias.'.$id;
  83                      $container->setAlias($commandId, $id)->setPublic(true);
  84                      $id = $commandId;
  85                  }
  86                  $serviceIds[] = $id;
  87  
  88                  continue;
  89              }
  90  
  91              $description = $tags[0]['description'] ?? null;
  92  
  93              unset($tags[0]);
  94              $lazyCommandMap[$commandName] = $id;
  95              $lazyCommandRefs[$id] = new TypedReference($id, $class);
  96  
  97              foreach ($aliases as $alias) {
  98                  $lazyCommandMap[$alias] = $id;
  99              }
 100  
 101              foreach ($tags as $tag) {
 102                  if (isset($tag['command'])) {
 103                      $aliases[] = $tag['command'];
 104                      $lazyCommandMap[$tag['command']] = $id;
 105                  }
 106  
 107                  $description = $description ?? $tag['description'] ?? null;
 108              }
 109  
 110              $definition->addMethodCall('setName', [$commandName]);
 111  
 112              if ($aliases) {
 113                  $definition->addMethodCall('setAliases', [$aliases]);
 114              }
 115  
 116              if ($isHidden) {
 117                  $definition->addMethodCall('setHidden', [true]);
 118              }
 119  
 120              if (!$description) {
 121                  if (!$r = $container->getReflectionClass($class)) {
 122                      throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
 123                  }
 124                  if (!$r->isSubclassOf(Command::class)) {
 125                      throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class));
 126                  }
 127                  $description = $class::getDefaultDescription();
 128              }
 129  
 130              if ($description) {
 131                  $definition->addMethodCall('setDescription', [$description]);
 132  
 133                  $container->register('.'.$id.'.lazy', LazyCommand::class)
 134                      ->setArguments([$commandName, $aliases, $description, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id])]);
 135  
 136                  $lazyCommandRefs[$id] = new Reference('.'.$id.'.lazy');
 137              }
 138          }
 139  
 140          $container
 141              ->register($this->commandLoaderServiceId, ContainerCommandLoader::class)
 142              ->setPublic(true)
 143              ->addTag($this->noPreloadTag)
 144              ->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]);
 145  
 146          $container->setParameter('console.command.ids', $serviceIds);
 147      }
 148  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer