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

AddPsyshCommandPass::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 9
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 19
rs 9.4285
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
        $commands = [];
33
        foreach ($container->findTaggedServiceIds('psysh.command') as $id => $attributes) {
34
            // Workaround to avoid Psysh commands to be registered as regular console commands
35
            // (conflict with service autoconfiguration as Psysh commands inherit from \Symfony\Component\Console\Command\Command as well
36
            // Note that this compiler pass must run with a higher priority than AddConsoleCommandPass to be efficient.
37
            $container->findDefinition($id)->clearTag('console.command');
38
            $commands[] = new Reference($id);
39
        }
40
41
42
        $shellRef = $container->findDefinition('psysh.shell');
43
        $shellRef->addMethodCall('addCommands', [$commands]);
44
    }
45
}
46