Skip to content

Commit

Permalink
Simplify compilers
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMasterov committed Aug 5, 2017
1 parent ca35e1d commit 013463f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 51 deletions.
16 changes: 8 additions & 8 deletions src/DependencyInjection/Compiler/AddCommandPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ class AddCommandPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
if (false === $container->has('psysh.shell')) {
if (!$container->has('psysh.shell')) {
return;
}

$services = $container->findTaggedServiceIds('psysh.command', true);
if (empty($services)) {
return;
$commands = [];

foreach ($container->findTaggedServiceIds('psysh.command', true) as $id => $tags) {
$commands[] = new Reference($id);
}

$commands = \array_map(
static function ($id) { return new Reference($id); },
\array_keys($services)
);
if (empty($commands)) {
return;
}

$container->getDefinition('psysh.config')
->addMethodCall('addCommands', [$commands]);
Expand Down
16 changes: 8 additions & 8 deletions src/DependencyInjection/Compiler/AddTabCompletionMatcherPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ class AddTabCompletionMatcherPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
if (false === $container->has('psysh.shell')) {
if (!$container->has('psysh.shell')) {
return;
}

$services = $container->findTaggedServiceIds('psysh.matcher', true);
if (empty($services)) {
return;
$matchers = [];

foreach ($container->findTaggedServiceIds('psysh.matcher', true) as $id => $tags) {
$matchers[] = new Reference($id);
}

$matchers = \array_map(
static function ($id) { return new Reference($id); },
\array_keys($services)
);
if (empty($matchers)) {
return;
}

$container->getDefinition('psysh.config')
->addMethodCall('addTabCompletionMatchers', [$matchers]);
Expand Down
68 changes: 33 additions & 35 deletions src/DependencyInjection/Compiler/SetVariablePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\{
Compiler\CompilerPassInterface,
ContainerBuilder,
Definition,
Reference
};

Expand All @@ -16,64 +17,61 @@ class SetVariablePass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
if (false === $container->has('psysh.shell')) {
if (!$container->has('psysh.shell')) {
return;
}

$services = $container->findTaggedServiceIds('psysh.variable', true);
if (empty($services)) {
$variables = [];

foreach ($container->findTaggedServiceIds('psysh.variable', true) as $id => [$attributes]) {
$variable = $attributes['var'] ?? (\class_exists($id) ? $this->classify($id) : $id);
$variables[$variable] = new Reference($id);
}

if (empty($variables)) {
return;
}

$definition = $container->getDefinition('psysh.shell');

if ($definition->hasMethodCall('setScopeVariables')) {
$this->mergeScopeVariables($definition, $variables);

return;
}

$this->registerScopeVariables($services, $container);
$definition->addMethodCall('setScopeVariables', [$variables]);
}

private function registerScopeVariables(array $services, ContainerBuilder $container): void
// NameSpace\SomeName -> nameSpaceSomeName
private function classify(string $spec): string
{
$definition = $container->getDefinition('psysh.shell');
$parts = \explode('\\', $spec);

if ($definition->hasMethodCall('setScopeVariables')) {
$calls = $this->mergeScopeVariables(
$definition->getMethodCalls(),
$this->scopeVariables($services)
);
$definition->setMethodCalls($calls);
} else {
$definition->addMethodCall('setScopeVariables', [$this->scopeVariables($services)]);
if (!empty($parts[1])) {
$parts[0] = \strtolower($parts[0]);
}

return \implode($parts);
}

private function mergeScopeVariables(array $calls, array $variables): array
private function mergeScopeVariables(Definition $definition, array $variables): void
{
foreach ($calls as $i => [$method, $arguments]) {
$calls = $definition->getMethodCalls();

foreach ($calls as $call => [$method, $arguments]) {
if ('setScopeVariables' === $method) {
foreach ($arguments as $argument) {
$variables += $argument;
}
unset($calls[$i]);
unset($calls[$call]);
}
}

return $calls + [
$calls += [
['setScopeVariables', [$variables]],
];
}

private function scopeVariables(array $services): array
{
// NameSpace\SomeName -> nameSpaceSomeName
$classify = static function (string $spec): string {
$parts = \explode('\\', $spec);
empty($parts[1]) ?: $parts[0] = \strtolower($parts[0]);
return \implode($parts);
};

$scopeVariables = [];
foreach ($services as $id => [$attributes]) {
$variable = $attributes['var'] ?? (\class_exists($id) ? $classify($id) : $id);
$scopeVariables[$variable] = new Reference($id);
}

return $scopeVariables;
$definition->setMethodCalls($calls);
}
}

0 comments on commit 013463f

Please sign in to comment.