Skip to content

Commit

Permalink
Now we can define sub command alias on dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
basuke committed Apr 24, 2017
1 parent 12f3a01 commit c508191
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
14 changes: 14 additions & 0 deletions Clim/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,18 @@ public function setResult($result)
{
$this->result = $result;
}

/**
* @param callable $callable
* @return mixed
*/
public function execute(callable $callable)
{
$options = new Hash($this->options());
$arguments = new Hash($this->arguments());

$result = call_user_func_array($callable, [$options, $arguments]);
if (!is_null($result)) $this->setResult($result);
return $result;
}
}
15 changes: 12 additions & 3 deletions Clim/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Dispatcher extends Component implements ArgumentInterface
/** @var ContainerInterface $container */
private $container;

/** @var array */
private $map;
/**
* @param string $definition
* @param array $children
Expand All @@ -31,17 +33,24 @@ public function __construct($definition, array $children, ContainerInterface $co

$this->children = $children;
$this->container = $container;
$this->map = [];
foreach (array_keys($this->children) as $key) {
$keys = explode('|', $key);
foreach ($keys as $name) {
$this->map[$name] = $children[$key];
}
}
}

public function handle($argument, Parameters $parameters, Context $context)
{
$this->needDefined();

if (!array_key_exists($argument, $this->children)) {
throw new \Exception("invalid sub command");
if (!array_key_exists($argument, $this->map)) {
throw new \Exception("invalid sub command: " . $argument);
}

$builder = $this->children[$argument];
$builder = $this->map[$argument];
$child = new App($this->container);

call_user_func($builder, $child);
Expand Down
5 changes: 1 addition & 4 deletions Clim/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ public function run($argv, Context $context = null)
$context->push($this->parameters->next());
}

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

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

return $context;
Expand Down
18 changes: 17 additions & 1 deletion tests/unit/DispatchCept.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

use Clim\App;
use Clim\Context;

$I = new UnitTester($scenario);
$I->wantTo('perform dispatch and see the result');

$app = new Clim\App(['name' => 'Kashiyuka']);
$app = new App(['name' => 'Kashiyuka']);

// global option
$app->option('--age {AGE|\\d+}');
Expand Down Expand Up @@ -34,3 +35,18 @@
$I->willPassArguments($app, ['hello_dispatch', 'bar']);
$I->assertOutputEquals($app, "BAR\n");

$app = new App();
$app->dispatch('{COMMAND}', [
'ls|list' => function (App $app) {
$app->task(function () {
return 'HELLO';
});
}
]);

$context = $app->runner()->run(['ls']);
$I->assertEquals('HELLO', $context->getResult());

$context = $app->runner()->run(['list']);
$I->assertEquals('HELLO', $context->getResult());

0 comments on commit c508191

Please sign in to comment.