Completed
Push — master ( 403289...b06dd8 )
by Théo
01:07
created

PsyshExtension::prepend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 10
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;
13
14
use Psy\Command\Command;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
18
use Symfony\Component\DependencyInjection\Loader;
19
use Symfony\Component\DependencyInjection\Reference;
20
use Symfony\Component\ExpressionLanguage\Expression;
21
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
22
use Symfony\Bundle\FrameworkBundle\Test\TestContainer;
23
24
/**
25
 * This is the class that loads and manages your bundle configuration.
26
 *
27
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
28
 *
29
 * @author Théo FIDRY <[email protected]>
30
 */
31 6
final class PsyshExtension extends Extension implements PrependExtensionInterface
32
{
33 6
    public function prepend(ContainerBuilder $container)
34 6
    {
35
        if (class_exists(TestContainer::class)) {
36 6
            $container->prependExtensionConfig('framework', ['test' => true]);
37 6
        }
38 6
    }
39 3
40 3
    /**
41 3
     * {@inheritdoc}
42 6
     */
43
    public function load(array $configs, ContainerBuilder $container)
44
    {
45
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../../resources/config'));
46
        $loader->load('services.xml');
47
48
        $configuration = new Configuration();
49
        $config = $this->processConfiguration($configuration, $configs);
50
        foreach ($config['variables'] as $name => &$value) {
51
            if (is_string($value) && $value[0] === '@') {
52
                $value = new Reference(substr($value, 1));
53
            }
54
        }
55
        $containerId = class_exists(TestContainer::class) ? 'test.service_container' : 'service_container';
56
        $container->findDefinition('psysh.shell')
57
            ->addMethodCall('setScopeVariables', [$config['variables'] + [
58
                'container' => new Reference($containerId),
59
                'kernel' => new Reference('kernel'),
60
                'self' => new Reference('psysh.shell'),
61
                'parameters' => new Expression(sprintf("service('%s').getParameterBag().all()", $containerId))
62
            ]]);
63
64
        $container->registerForAutoconfiguration(Command::class)->addTag('psysh.command');
65
    }
66
}
67