Skip to content

Commit

Permalink
fix middleware runs before option and arguments parse
Browse files Browse the repository at this point in the history
  • Loading branch information
basuke committed Apr 19, 2017
1 parent 6faff4b commit 351dc1e
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions Clim/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ public function run($context)
$context = new Context($context);
}

return $this->task_middleware->run($context, function ($context) {
if ($this->parse($context)) return $context;

$options = new Collection($context->options());
$arguments = new Collection($context->arguments());

foreach ($this->tasks as $task) {
call_user_func($task, $options, $arguments);
}

return $context;
});
}

protected function parse(Context $context)
{
$this->running_arguments = array_slice($this->arguments, 0);

$this->collectDefaultOptions($context);
Expand All @@ -82,23 +98,14 @@ public function run($context)
} else {
$handled = $this->parseLongOption(substr($arg, 2), $context);
}
if ($handled) return;
if ($handled) return true;
}

foreach ($this->running_arguments as $argument) {
$argument->handle($context->next(), $context);
}

return $this->task_middleware->run($context, function ($context) {
$options = new Collection($context->options());
$arguments = new Collection($context->arguments());

foreach ($this->tasks as $task) {
call_user_func($task, $options, $arguments);
}

return $context;
});
return false;
}

protected function handleArgument($arg, Context $context)
Expand All @@ -120,7 +127,7 @@ protected function parseShortOption($arg, Context $context)
$arg = strlen($arg) > 1 ? substr($arg, 1) : null;

$context->tentative($arg);
$this->parse($value, $context);
$this->parseOption($value, $context);
$arg = $context->tentative();
}
}
Expand All @@ -135,14 +142,14 @@ protected function parseLongOption($value, Context $context)
$value = substr($value, 0, $pos);
}

$this->parse($value, $context);
$this->parseOption($value, $context);

if (!is_null($context->tentative())) {
throw new Exception\OptionException("value is not needed");
}
}

protected function parse($value, Context $context)
protected function parseOption($value, Context $context)
{
foreach ($this->options as /** @var Option */ $option) {
$parsed = $option->parse($value, $context);
Expand Down

0 comments on commit 351dc1e

Please sign in to comment.