Completed
Push — master ( c7e9bf...6c9a09 )
by Théo
01:19
created

AddPsyshCommandPass   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 10
wmc 3
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 18 3
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
 * @private
25
 */
26
final class AddPsyshCommandPass implements CompilerPassInterface
27
{
28
    public function process(ContainerBuilder $container)
29
    {
30
        if (!$container->has('psysh.shell')) {
31
            return;
32
        }
33
34
        $commands = [];
35
        foreach ($container->findTaggedServiceIds('psysh.command') as $id => $attributes) {
36
            // Workaround to avoid Psysh commands to be registered as regular console commands
37
            // (conflict with service autoconfiguration as Psysh commands inherit from \Symfony\Component\Console\Command\Command as well
38
            // Note that this compiler pass must run with a higher priority than AddConsoleCommandPass to be efficient.
39
            $container->findDefinition($id)->clearTag('console.command');
40
            $commands[] = new Reference($id);
41
        }
42
43
        $shellRef = $container->findDefinition('psysh.shell');
44
        $shellRef->addMethodCall('addCommands', [$commands]);
45
    }
46
}
47