2021-05-10 00:23:30 +00:00
< ? 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 .
*/
2021-08-13 11:57:49 +00:00
namespace RectorPrefix20210813\Symfony\Component\Console\DependencyInjection ;
2021-05-10 00:23:30 +00:00
2021-08-13 11:57:49 +00:00
use RectorPrefix20210813\Symfony\Component\Console\Command\Command ;
use RectorPrefix20210813\Symfony\Component\Console\Command\LazyCommand ;
use RectorPrefix20210813\Symfony\Component\Console\CommandLoader\ContainerCommandLoader ;
use RectorPrefix20210813\Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument ;
use RectorPrefix20210813\Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface ;
use RectorPrefix20210813\Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass ;
use RectorPrefix20210813\Symfony\Component\DependencyInjection\ContainerBuilder ;
use RectorPrefix20210813\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException ;
use RectorPrefix20210813\Symfony\Component\DependencyInjection\Reference ;
use RectorPrefix20210813\Symfony\Component\DependencyInjection\TypedReference ;
2021-05-10 00:23:30 +00:00
/**
* Registers console commands .
*
* @ author Grégoire Pineau < lyrixx @ lyrixx . info >
*/
2021-08-13 11:57:49 +00:00
class AddConsoleCommandPass implements \RectorPrefix20210813\Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface
2021-05-10 00:23:30 +00:00
{
private $commandLoaderServiceId ;
private $commandTag ;
private $noPreloadTag ;
private $privateTagName ;
public function __construct ( string $commandLoaderServiceId = 'console.command_loader' , string $commandTag = 'console.command' , string $noPreloadTag = 'container.no_preload' , string $privateTagName = 'container.private' )
{
2021-06-01 12:39:02 +00:00
if ( 0 < \func_num_args ()) {
trigger_deprecation ( 'symfony/console' , '5.3' , 'Configuring "%s" is deprecated.' , __CLASS__ );
}
2021-05-10 00:23:30 +00:00
$this -> commandLoaderServiceId = $commandLoaderServiceId ;
$this -> commandTag = $commandTag ;
$this -> noPreloadTag = $noPreloadTag ;
$this -> privateTagName = $privateTagName ;
}
2021-07-05 22:50:18 +00:00
/**
* @ param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*/
public function process ( $container )
2021-05-10 00:23:30 +00:00
{
$commandServices = $container -> findTaggedServiceIds ( $this -> commandTag , \true );
$lazyCommandMap = [];
$lazyCommandRefs = [];
$serviceIds = [];
foreach ( $commandServices as $id => $tags ) {
$definition = $container -> getDefinition ( $id );
$definition -> addTag ( $this -> noPreloadTag );
$class = $container -> getParameterBag () -> resolveValue ( $definition -> getClass ());
if ( isset ( $tags [ 0 ][ 'command' ])) {
2021-06-01 12:39:02 +00:00
$aliases = $tags [ 0 ][ 'command' ];
2021-05-10 00:23:30 +00:00
} else {
if ( ! ( $r = $container -> getReflectionClass ( $class ))) {
2021-08-13 11:57:49 +00:00
throw new \RectorPrefix20210813\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException ( \sprintf ( 'Class "%s" used for service "%s" cannot be found.' , $class , $id ));
2021-05-10 00:23:30 +00:00
}
2021-08-13 11:57:49 +00:00
if ( ! $r -> isSubclassOf ( \RectorPrefix20210813\Symfony\Component\Console\Command\Command :: class )) {
throw new \RectorPrefix20210813\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException ( \sprintf ( 'The service "%s" tagged "%s" must be a subclass of "%s".' , $id , $this -> commandTag , \RectorPrefix20210813\Symfony\Component\Console\Command\Command :: class ));
2021-05-10 00:23:30 +00:00
}
2021-06-01 12:39:02 +00:00
$aliases = $class :: getDefaultName ();
}
$aliases = \explode ( '|' , $aliases ? ? '' );
$commandName = \array_shift ( $aliases );
if ( $isHidden = '' === $commandName ) {
$commandName = \array_shift ( $aliases );
2021-05-10 00:23:30 +00:00
}
if ( null === $commandName ) {
if ( ! $definition -> isPublic () || $definition -> isPrivate () || $definition -> hasTag ( $this -> privateTagName )) {
$commandId = 'console.command.public_alias.' . $id ;
$container -> setAlias ( $commandId , $id ) -> setPublic ( \true );
$id = $commandId ;
}
$serviceIds [] = $id ;
continue ;
}
2021-06-01 12:39:02 +00:00
$description = $tags [ 0 ][ 'description' ] ? ? null ;
2021-05-10 00:23:30 +00:00
unset ( $tags [ 0 ]);
$lazyCommandMap [ $commandName ] = $id ;
2021-08-13 11:57:49 +00:00
$lazyCommandRefs [ $id ] = new \RectorPrefix20210813\Symfony\Component\DependencyInjection\TypedReference ( $id , $class );
2021-06-01 12:39:02 +00:00
foreach ( $aliases as $alias ) {
$lazyCommandMap [ $alias ] = $id ;
}
2021-05-10 00:23:30 +00:00
foreach ( $tags as $tag ) {
if ( isset ( $tag [ 'command' ])) {
$aliases [] = $tag [ 'command' ];
$lazyCommandMap [ $tag [ 'command' ]] = $id ;
}
2021-06-01 12:39:02 +00:00
$description = $description ? ? $tag [ 'description' ] ? ? null ;
2021-05-10 00:23:30 +00:00
}
$definition -> addMethodCall ( 'setName' , [ $commandName ]);
if ( $aliases ) {
$definition -> addMethodCall ( 'setAliases' , [ $aliases ]);
}
2021-06-01 12:39:02 +00:00
if ( $isHidden ) {
$definition -> addMethodCall ( 'setHidden' , [ \true ]);
}
if ( ! $description ) {
if ( ! ( $r = $container -> getReflectionClass ( $class ))) {
2021-08-13 11:57:49 +00:00
throw new \RectorPrefix20210813\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException ( \sprintf ( 'Class "%s" used for service "%s" cannot be found.' , $class , $id ));
2021-06-01 12:39:02 +00:00
}
2021-08-13 11:57:49 +00:00
if ( ! $r -> isSubclassOf ( \RectorPrefix20210813\Symfony\Component\Console\Command\Command :: class )) {
throw new \RectorPrefix20210813\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException ( \sprintf ( 'The service "%s" tagged "%s" must be a subclass of "%s".' , $id , $this -> commandTag , \RectorPrefix20210813\Symfony\Component\Console\Command\Command :: class ));
2021-06-01 12:39:02 +00:00
}
$description = $class :: getDefaultDescription ();
}
if ( $description ) {
$definition -> addMethodCall ( 'setDescription' , [ $description ]);
2021-08-13 11:57:49 +00:00
$container -> register ( '.' . $id . '.lazy' , \RectorPrefix20210813\Symfony\Component\Console\Command\LazyCommand :: class ) -> setArguments ([ $commandName , $aliases , $description , $isHidden , new \RectorPrefix20210813\Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument ( $lazyCommandRefs [ $id ])]);
$lazyCommandRefs [ $id ] = new \RectorPrefix20210813\Symfony\Component\DependencyInjection\Reference ( '.' . $id . '.lazy' );
2021-06-01 12:39:02 +00:00
}
2021-05-10 00:23:30 +00:00
}
2021-08-13 11:57:49 +00:00
$container -> register ( $this -> commandLoaderServiceId , \RectorPrefix20210813\Symfony\Component\Console\CommandLoader\ContainerCommandLoader :: class ) -> setPublic ( \true ) -> addTag ( $this -> noPreloadTag ) -> setArguments ([ \RectorPrefix20210813\Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass :: register ( $container , $lazyCommandRefs ), $lazyCommandMap ]);
2021-05-10 00:23:30 +00:00
$container -> setParameter ( 'console.command.ids' , $serviceIds );
}
}