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
|
|
|
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
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: