Skip to content

Commit

Permalink
Add argument to method translator in configurator.
Browse files Browse the repository at this point in the history
It's now possible to use CLI argument in a configuration file.
Argument with no value required are translated with these rules :
1) Remove trailing - ;
2) Substitute /-(.)/ with \u\1 ;
So, '--loop' argument is translated by configurator to method 'loop()'.
Argument with at least one value required are translated with these
rules :
1) Remove trailing - ;
2) Substitute /-(.)/ with \u\1 ;
3) Add 'set' before the result ;
So '--bootstrap-file' argument is translated by configurator to method
'setBootstrapFile()', and -bf argument is translated to method 'setBf()'.
  • Loading branch information
mageekguy committed Feb 28, 2012
1 parent 6724bcf commit b2736b5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
26 changes: 26 additions & 0 deletions classes/configurator.php
Expand Up @@ -4,15 +4,41 @@

class configurator
{
protected $script = null;
protected $methods = array();

public function __construct(scripts\runner $script)
{
$this->script = $script;

foreach ($this->script->getHelp() as $help)
{
list($arguments, $values) = $help;

foreach ($arguments as $argument)
{
$methodName = preg_replace('/-(.)/e', 'ucfirst(\'\1\')', ltrim($argument, '-'));

if ($values === null)
{
$this->methods[$methodName] = $argument;
}
else
{
$this->methods['set' . ucfirst($methodName)] = $argument;
}
}
}
}

public function __call($method, $arguments)
{
switch (true)
{
case isset($this->methods[$method]):
$this->script->getArgumentsParser()->invokeHandlers($this->script, $this->methods[$method], $arguments);
return $this;

case method_exists($this->script->getRunner(), $method):
call_user_func_array(array($this->script->getRunner(), $method), $arguments);
return $this;
Expand Down
14 changes: 7 additions & 7 deletions classes/script/arguments/parser.php
Expand Up @@ -173,12 +173,7 @@ public function init(array $array = array())
return $this;
}

public static function isArgument($value)
{
return (preg_match('/^(\+|-{1,2})[a-z][-_a-z0-9]*/i', $value) === 1);
}

protected function triggerHandlers($argument, array $values, atoum\script $script)
public function triggerHandlers($argument, array $values, atoum\script $script)
{
if (isset($this->handlers[$argument]) === true)
{
Expand Down Expand Up @@ -222,7 +217,7 @@ protected function triggerHandlers($argument, array $values, atoum\script $scrip
return $this;
}

protected function invokeHandlers(atoum\script $script, $argument, array $values)
public function invokeHandlers(atoum\script $script, $argument, array $values)
{
foreach ($this->handlers[$argument] as $handler)
{
Expand All @@ -231,6 +226,11 @@ protected function invokeHandlers(atoum\script $script, $argument, array $values

return $this;
}

public static function isArgument($value)
{
return (preg_match('/^(\+|-{1,2})[a-z][-_a-z0-9]*/i', $value) === 1);
}
}

?>
3 changes: 3 additions & 0 deletions tests/units/classes/configurator.php
Expand Up @@ -30,6 +30,7 @@ public function test__call()
->mock('mageekguy\atoum\scripts\runner')
->assert
->if($runner = new \mock\mageekguy\atoum\runner())
->and($runner->getMockController()->setBootstrapFile = function() {})
->and($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
->and($script->setRunner($runner))
->and($configurator = new atoum\configurator($script))
Expand All @@ -38,6 +39,8 @@ public function test__call()
->mock($script)->call('setScoreFile')->withArguments($scoreFile)->once()
->object($configurator->setPhpPath($phpPath = uniqid()))->isIdenticalTo($configurator)
->mock($runner)->call('setPhpPath')->withArguments($phpPath)->once()
->object($configurator->setBf($bootstrapFile = uniqid()))->isIdenticalTo($configurator)
->mock($runner)->call('setBootstrapFile')->withArguments($bootstrapFile)->once()
->exception(function() use ($configurator, & $method) { $configurator->{$method = uniqid()}(); })
->isInstanceOf('mageekguy\atoum\exceptions\runtime\unexpectedValue')
->hasMessage('Method \'' . $method . '\' is unavailable')
Expand Down

0 comments on commit b2736b5

Please sign in to comment.