PsyshExtension   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 9
dl 0
loc 47
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A load() 0 39 4
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;
13
14
use Psy\Command\Command;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Loader;
18
use Symfony\Component\DependencyInjection\Reference;
19
use Symfony\Component\ExpressionLanguage\Expression;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
use function array_merge;
22
use function is_string;
23
use function sprintf;
24
use function strpos;
25
use function substr;
26
27
/**
28
 * This is the class that loads and manages your bundle configuration.
29
 *
30
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
31 6
 *
32
 * @author Théo FIDRY <[email protected]>
33 6
 *
34 6
 * @private
35
 */
36 6
final class PsyshExtension extends Extension
37 6
{
38 6
    private const CONFIG_DIR = __DIR__.'/../../resources/config';
39 3
40 3
    /**
41 3
     * {@inheritdoc}
42 6
     */
43
    public function load(array $configs, ContainerBuilder $container): void
44
    {
45
        $loader = new Loader\XmlFileLoader($container, new FileLocator(self::CONFIG_DIR));
46
        $loader->load('services.xml');
47
48
        $config = $this->processConfiguration(new Configuration(), $configs);
49
50
        foreach ($config['variables'] as $name => &$value) {
51
            if (is_string($value) && strpos($value, '@') === 0) {
52
                $value = new Reference(substr($value, 1));
53
            }
54
        }
55
56
        $containerId = 'test.service_container';
57
58
        $container
59
            ->findDefinition('psysh.shell')
60
            ->addMethodCall(
61
                'setScopeVariables',
62
                [array_merge(
63
                    $config['variables'],
64
                    [
65
                        'container' => new Reference($containerId),
66
                        'kernel' => new Reference('kernel'),
67
                        'self' => new Reference('psysh.shell'),
68
                        'parameters' => new Expression(sprintf(
69
                            "service('%s').getParameterBag().all()",
70
                            $containerId
71
                        ))
72
                    ]
73
                )]
74
            )
75
        ;
76
77
        $container
78
            ->registerForAutoconfiguration(Command::class)
79
            ->addTag('psysh.command')
80
        ;
81
    }
82
}
83