Completed
Pull Request — master (#47)
by Maxime
12:36
created

PsyshExtension::prepend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
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
        $container->prependExtensionConfig('framework', ['test' => true]);
36 6
    }
37 6
38 6
    /**
39 3
     * {@inheritdoc}
40 3
     */
41 3
    public function load(array $configs, ContainerBuilder $container)
42 6
    {
43
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../../resources/config'));
44
        $loader->load('services.xml');
45
46
        $configuration = new Configuration();
47
        $config = $this->processConfiguration($configuration, $configs);
48
        foreach ($config['variables'] as $name => &$value) {
49
            if (is_string($value) && $value[0] === '@') {
50
                $value = new Reference(substr($value, 1));
51
            }
52
        }
53
        $containerId = $container->has('test.service_container') ? 'test.service_container' : 'service_container';
54
        $container->findDefinition('psysh.shell')
55
            ->addMethodCall('setScopeVariables', [$config['variables'] + [
56
                'container' => new Reference($containerId),
57
                'kernel' => new Reference('kernel'),
58
                'self' => new Reference('psysh.shell'),
59
                'parameters' => new Expression(sprintf("service('%s').getParameterBag().all()", $containerId))
60
            ]]);
61
        
62
        // Register Psysh commands for service autoconfiguration (Symfony 3.3+)
63
        if (method_exists($container, 'registerForAutoconfiguration')) {
64
            $container->registerForAutoconfiguration(Command::class)->addTag('psysh.command');
65
        }
66
    }
67
}
68