Completed
Push — master ( 742ab5...82b4e6 )
by Alex
01:39
created

Extension::loadInternal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace AlexMasterov\PsyshBundle\DependencyInjection;
5
6
use AlexMasterov\PsyshBundle\Command\ShellCommand;
7
use Psy\{
8
    Command\Command,
9
    Configuration,
10
    Matcher\AbstractMatcher,
11
    Shell
12
};
13
use Symfony\Component\DependencyInjection\{
14
    ContainerBuilder,
15
    Definition,
16
    Reference
17
};
18
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
19
20
class Extension extends ConfigurableExtension
21
{
22
    /**
23
     * @inheritDoc
24
     * @codeCoverageIgnore
25
     */
26
    public function getAlias()
27
    {
28
        return 'psysh';
29
    }
30
31
    /**
32
     * @inheritDoc
33
     */
34 1
    protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
35
    {
36 1
        $this->registerTags($container);
37 1
        $this->registerCommands($container);
38 1
        $this->registerConfig($mergedConfig, $container);
39 1
        $this->registerShell($mergedConfig, $container);
40 1
    }
41
42 1
    private function registerTags(ContainerBuilder $container): void
43
    {
44 1
        static $tags = [
45
            'psysh.command' => Command::class,
46
            'psysh.matcher' => AbstractMatcher::class,
47
        ];
48
49 1
        foreach ($tags as $tag => $spec) {
50 1
            $container->registerForAutoconfiguration($spec)->addTag($tag);
51
        }
52 1
    }
53
54 1
    private function registerCommands(ContainerBuilder $container): void
55
    {
56 1
        $container->register('psysh.command.shell_command', ShellCommand::class)
57 1
            ->setPublic(false)
58 1
            ->addArgument(new Reference('psysh.shell'))
59 1
            ->setAutoconfigured(true);
60 1
    }
61
62 1
    private function registerConfig(array $config, ContainerBuilder $container): void
63
    {
64 1
        $config = $this->registerConfigurationTags($config, $container);
65
66 1
        $definition = (new Definition(Configuration::class))
67 1
            ->setShared(false)
68 1
            ->setPublic(false)
69 1
            ->addArgument($config);
70
71 1
        if (isset($config['historyFile'])) {
72 1
            $definition->addMethodCall('setHistoryFile', [$config['historyFile']]);
73
        }
74
75 1
        $container->setDefinition('psysh.config', $definition);
76 1
    }
77
78
    private function registerConfigurationTags(array $config, ContainerBuilder $container): array
79
    {
80 1
        $configurator = function (array $services, ContainerBuilder $container): void {
81 1
            foreach ($services as $service) {
82 1
                $container->hasDefinition($service) ?: $container->register($service)->setPublic(false);
83 1
                $container->getDefinition($service)->setAutoconfigured(true);
84
            }
85 1
        };
86
87 1
        foreach (['commands', 'tabCompletionMatchers'] as $option) {
88 1
            if (isset($config[$option])) {
89 1
                $configurator($config[$option], $container);
90 1
                unset($config[$option]);
91
            }
92
        }
93
94 1
        return $config;
95
    }
96
97 1
    private function registerShell(array $config, ContainerBuilder $container): void
98
    {
99 1
        $definition = (new Definition(Shell::class))
100 1
            ->setPublic(false)
101 1
            ->addArgument(new Reference('psysh.config'));
102
103 1
        if (isset($config['variables'])) {
104 1
            $definition->addMethodCall('setScopeVariables', [
105 1
                $this->scopeVariables($config['variables']),
106
            ]);
107
        }
108
109 1
        $container->setDefinition('psysh.shell', $definition);
110 1
    }
111
112 1
    private function scopeVariables(array $variables): array
113
    {
114 1
        foreach ($variables as &$spec) {
115 1
            if (\is_string($spec) && 0 === \strpos($spec, '@')) {
116 1
                $spec = new Reference(\substr($spec, 1));
117
            }
118
        }
119
120 1
        return $variables;
121
    }
122
}
123