|
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
|
|
|
use function is_string; |
|
20
|
|
|
use function strpos; |
|
21
|
|
|
use function substr; |
|
22
|
|
|
|
|
23
|
|
|
class Extension extends ConfigurableExtension |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
* @codeCoverageIgnore |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getAlias() |
|
30
|
|
|
{ |
|
31
|
|
|
return 'psysh'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** {@inheritdoc} */ |
|
35
|
1 |
|
protected function loadInternal(array $mergedConfig, ContainerBuilder $container) |
|
36
|
|
|
{ |
|
37
|
1 |
|
$this->registerTags($container); |
|
38
|
1 |
|
$this->registerCommands($container); |
|
39
|
1 |
|
$this->registerConfig($mergedConfig, $container); |
|
40
|
1 |
|
$this->registerShell($mergedConfig, $container); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
private function registerTags(ContainerBuilder $container): void |
|
44
|
|
|
{ |
|
45
|
1 |
|
static $tags = [ |
|
46
|
|
|
'psysh.command' => Command::class, |
|
47
|
|
|
'psysh.matcher' => AbstractMatcher::class, |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
1 |
|
foreach ($tags as $tag => $spec) { |
|
51
|
1 |
|
$container->registerForAutoconfiguration($spec)->addTag($tag); |
|
52
|
|
|
} |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
private function registerCommands(ContainerBuilder $container): void |
|
56
|
|
|
{ |
|
57
|
1 |
|
$container->register('psysh.command.shell_command', ShellCommand::class) |
|
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 |
|
->addArgument($config); |
|
69
|
|
|
|
|
70
|
1 |
|
if (isset($config['historyFile'])) { |
|
71
|
1 |
|
$definition->addMethodCall('setHistoryFile', [$config['historyFile']]); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
$container->setDefinition('psysh.config', $definition); |
|
75
|
1 |
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
private function registerConfigurationTags(array $config, ContainerBuilder $container): array |
|
78
|
|
|
{ |
|
79
|
|
|
$configurator = function (array $services, ContainerBuilder $container): void { |
|
80
|
1 |
|
foreach ($services as $service) { |
|
81
|
1 |
|
$container->hasDefinition($service) ?: $container->register($service)->setPublic(false); |
|
82
|
1 |
|
$container->getDefinition($service)->setAutoconfigured(true); |
|
83
|
|
|
} |
|
84
|
1 |
|
}; |
|
85
|
|
|
|
|
86
|
1 |
|
foreach (['commands', 'matchers'] as $option) { |
|
87
|
1 |
|
if (isset($config[$option])) { |
|
88
|
1 |
|
$configurator($config[$option], $container); |
|
89
|
1 |
|
unset($config[$option]); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
return $config; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
private function registerShell(array $config, ContainerBuilder $container): void |
|
97
|
|
|
{ |
|
98
|
1 |
|
$definition = (new Definition(Shell::class)) |
|
99
|
1 |
|
->addArgument(new Reference('psysh.config')); |
|
100
|
|
|
|
|
101
|
1 |
|
if (isset($config['variables'])) { |
|
102
|
1 |
|
$definition->addMethodCall('setScopeVariables', [ |
|
103
|
1 |
|
$this->scopeVariables($config['variables']), |
|
104
|
|
|
]); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
$container->setDefinition('psysh.shell', $definition); |
|
108
|
1 |
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
private function scopeVariables(array $variables): array |
|
111
|
|
|
{ |
|
112
|
1 |
|
foreach ($variables as &$spec) { |
|
113
|
1 |
|
if (is_string($spec) && 0 === strpos($spec, '@')) { |
|
114
|
1 |
|
$spec = new Reference(substr($spec, 1)); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
return $variables; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|