AddPsyshCommandPass   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 21 3
1
<?php declare(strict_types=1);
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 Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Reference;
17
18
/**
19
 * Compiler pass allowing to add Psysh commands dynamically
20
 *
21
 * @author Jérôme Vieilledent <[email protected]>
22
 *
23
 * @private
24
 */
25
final class AddPsyshCommandPass implements CompilerPassInterface
26
{
27
    public function process(ContainerBuilder $container): void
28
    {
29
        if (!$container->has('psysh.shell')) {
30
            return;
31
        }
32
33
        $commands = [];
34
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
38
            // \Symfony\Component\Console\Command\Command as well
39
            // Note that this compiler pass must run with a higher priority than
40
            // AddConsoleCommandPass to be efficient.
41
            $container->findDefinition($id)->clearTag('console.command');
42
            $commands[] = new Reference($id);
43
        }
44
45
        $shellRef = $container->findDefinition('psysh.shell');
46
        $shellRef->addMethodCall('addCommands', [$commands]);
47
    }
48
}
49