PHP Classes

File: vendor/symfony/console/DependencyInjection/AddConsoleCommandPass.php

Recommend this page to a friend!
  Classes of Renato Lucena   PHP Pokemon Script   vendor/symfony/console/DependencyInjection/AddConsoleCommandPass.php   Download  
File: vendor/symfony/console/DependencyInjection/AddConsoleCommandPass.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Pokemon Script
Provides an API to manage a database of Pokemons
Author: By
Last change:
Date: 6 years ago
Size: 2,039 bytes
 

Contents

Class file image Download
<?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 Symfony\Component\Console\DependencyInjection;

use
Symfony\Component\Console\Command\Command;
use
Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use
Symfony\Component\DependencyInjection\ContainerBuilder;
use
Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;

/**
 * Registers console commands.
 *
 * @author Grégoire Pineau <lyrixx@lyrixx.info>
 */
class AddConsoleCommandPass implements CompilerPassInterface
{
    public function
process(ContainerBuilder $container)
    {
       
$commandServices = $container->findTaggedServiceIds('console.command', true);
       
$serviceIds = array();

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

            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 "console.command" must be a subclass of "%s".', $id, Command::class));
            }

           
$commandId = 'console.command.'.strtolower(str_replace('\\', '_', $class));
            if (
$container->hasAlias($commandId) || isset($serviceIds[$commandId])) {
               
$commandId = $commandId.'_'.$id;
            }
            if (!
$definition->isPublic()) {
               
$container->setAlias($commandId, $id);
               
$id = $commandId;
            }

           
$serviceIds[$commandId] = $id;
        }

       
$container->setParameter('console.command.ids', $serviceIds);
    }
}