Вход Регистрация
Файл: system/vendor/symfony/console/DependencyInjection/AddConsoleCommandPass.php
Строк: 193
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace SymfonyComponentConsoleDependencyInjection;

use 
SymfonyComponentConsoleCommandCommand;
use 
SymfonyComponentConsoleCommandLoaderContainerCommandLoader;
use 
SymfonyComponentDependencyInjectionCompilerCompilerPassInterface;
use 
SymfonyComponentDependencyInjectionCompilerServiceLocatorTagPass;
use 
SymfonyComponentDependencyInjectionContainerBuilder;
use 
SymfonyComponentDependencyInjectionExceptionInvalidArgumentException;
use 
SymfonyComponentDependencyInjectionTypedReference;

/**
 * Registers console commands.
 *
 * @author Grégoire Pineau <lyrixx@lyrixx.info>
 */
class AddConsoleCommandPass implements CompilerPassInterface
{
    private 
$commandLoaderServiceId;
    private 
$commandTag;

    public function 
__construct(string $commandLoaderServiceId 'console.command_loader'string $commandTag 'console.command')
    {
        
$this->commandLoaderServiceId $commandLoaderServiceId;
        
$this->commandTag $commandTag;
    }

    public function 
process(ContainerBuilder $container)
    {
        
$commandServices $container->findTaggedServiceIds($this->commandTagtrue);
        
$lazyCommandMap = [];
        
$lazyCommandRefs = [];
        
$serviceIds = [];

        foreach (
$commandServices as $id => $tags) {
            
$definition $container->getDefinition($id);
            
$class $container->getParameterBag()->resolveValue($definition->getClass());

            if (isset(
$tags[0]['command'])) {
                
$commandName $tags[0]['command'];
            } else {
                if (!
$r $container->getReflectionClass($class)) {
                    throw new 
InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.'$class$id));
                }
                if (!
$r->isSubclassOf(Command::class)) {
                    throw new 
InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".'$id$this->commandTagCommand::class));
                }
                
$commandName $class::getDefaultName();
            }

            if (
null === $commandName) {
                if (!
$definition->isPublic() || $definition->isPrivate()) {
                    
$commandId 'console.command.public_alias.'.$id;
                    
$container->setAlias($commandId$id)->setPublic(true);
                    
$id $commandId;
                }
                
$serviceIds[] = $id;

                continue;
            }

            unset(
$tags[0]);
            
$lazyCommandMap[$commandName] = $id;
            
$lazyCommandRefs[$id] = new TypedReference($id$class);
            
$aliases = [];

            foreach (
$tags as $tag) {
                if (isset(
$tag['command'])) {
                    
$aliases[] = $tag['command'];
                    
$lazyCommandMap[$tag['command']] = $id;
                }
            }

            
$definition->addMethodCall('setName', [$commandName]);

            if (
$aliases) {
                
$definition->addMethodCall('setAliases', [$aliases]);
            }
        }

        
$container
            
->register($this->commandLoaderServiceIdContainerCommandLoader::class)
            ->
setPublic(true)
            ->
setArguments([ServiceLocatorTagPass::register($container$lazyCommandRefs), $lazyCommandMap]);

        
$container->setParameter('console.command.ids'$serviceIds);
    }
}
Онлайн: 0
Реклама