Completed
Pull Request — master (#28)
by Jérôme
09:50
created

AddPsyshCommandPass::process()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 7
nop 1
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the PsyshBundle package.
5
 *
6
 * (c) Théo FIDRY <[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 Fidry\PsyshBundle\DependencyInjection\Compiler;
13
14
use Psy\Command\Command as PsyshCommand;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
/**
20
 * Compiler pass allowing to add Psysh commands dynamically
21
 *
22
 * @author Jérôme Vieilledent <[email protected]>
23
 */
24
class AddPsyshCommandPass implements CompilerPassInterface
25
{
26
    public function process(ContainerBuilder $container)
27
    {
28
        if (!$container->has('psysh.shell')) {
29
            return;
30
        }
31
32
        // Workaround to avoid Psysh commands to be registered as regular console commands
33
        // (conflict with service autoconfiguration as Psysh commands inherit from \Symfony\Component\Console\Command\Command as well
34
        // Note that this compiler pass must run with a higher priority than AddConsoleCommandPass to be efficient.
35
        foreach ($container->findTaggedServiceIds('console.command') as $id => $attributes) {
36
            $definition = $container->findDefinition($id);
37
            if (is_subclass_of($definition->getClass(), PsyshCommand::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Psy\Command\Command::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
38
                $definition->clearTag('console.command');
39
            }
40
        }
41
42
        $commands = [];
43
        foreach ($container->findTaggedServiceIds('psysh.command') as $id => $attributes) {
44
            $commands[] = new Reference($id);
45
        }
46
47
48
        $shellRef = $container->findDefinition('psysh.shell');
49
        $shellRef->addMethodCall('addCommands', [$commands]);
50
    }
51
}
52