Completed
Push — master ( c1430b...ac5424 )
by Alex
04:47 queued 03:13
created

Extension::registerShell()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
crap 2
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,
1 ignored issue
show
Bug introduced by
This use statement conflicts with another class in this namespace, AlexMasterov\PsyshBundle...Injection\Configuration.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
    Shell
11
};
12
use Symfony\Component\DependencyInjection\{
13
    ContainerBuilder,
14
    Definition,
15
    Reference
16
};
17
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
18
19
class Extension extends ConfigurableExtension
20
{
21
    /**
22
     * @inheritDoc
23
     */
24 1
    protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
25
    {
26 1
        $this->registerShell($mergedConfig, $container);
27 1
        $this->registerCommand($container);
28 1
    }
29
30 1
    private function registerShell(array $config, ContainerBuilder $container): void
31
    {
32 1
        $configId = 'psysh.config';
33 1
        $container->setDefinition($configId, $this->configDefinition($config));
34
35 1
        $definition = (new Definition(Shell::class))
36 1
            ->setPublic(false)
37 1
            ->addArgument(new Reference($configId));
38
39 1
        if (isset($config['variables'])) {
40 1
            $definition->addMethodCall('setScopeVariables', [
41 1
                $this->scopeVariables($config['variables']),
42
            ]);
43
        }
44
45 1
        $container->setDefinition('psysh.shell', $definition);
46 1
    }
47
48 1
    private function configDefinition(array $config): Definition
49
    {
50 1
        $definition = (new Definition(Configuration::class))
51 1
            ->setShared(false)
52 1
            ->setPublic(false)
53 1
            ->addArgument($config);
54
55 1
        if (isset($config['historyFile'])) {
56 1
            $definition->addMethodCall('setHistoryFile', [$config['historyFile']]);
57
        }
58
59 1
        return $definition;
60
    }
61
62 1
    private function scopeVariables(array $variables): array
63
    {
64 1
        foreach ($variables as &$spec) {
65 1
            if (\is_string($spec) && '@' === $spec[0]) {
66 1
                $spec = new Reference(\substr($spec, 1));
67
            }
68
        }
69
70 1
        return $variables;
71
    }
72
73 1
    private function registerCommand(ContainerBuilder $container): void
74
    {
75 1
        $container->registerForAutoconfiguration(Command::class)
76 1
            ->addTag('psysh.command');
77
78 1
        $container->register('psysh.command.shell_command', ShellCommand::class)
79 1
            ->setPublic(false)
80 1
            ->addArgument(new Reference('psysh.shell'))
81 1
            ->setAutoconfigured(true);
82 1
    }
83
}
84