"Fossies" - the Fresh Open Source Software Archive

Member "glpi/vendor/symfony/console/DependencyInjection/AddConsoleCommandPass.php" (25 Jan 2019, 3994 Bytes) of package /linux/misc/glpi-9.4.4.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AddConsoleCommandPass.php" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 9.4.0_vs_9.4.1.1.

    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 }