-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
203 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
namespace Clim\Helper; | ||
|
||
use Clim\Context; | ||
use Closure; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class ArgumentsSupplier | ||
{ | ||
protected $context; | ||
|
||
protected $container; | ||
|
||
protected $options; | ||
|
||
protected $arguments; | ||
|
||
protected $missingArgs; | ||
|
||
protected $assignedArguments; | ||
|
||
public function __construct(Context $context, ContainerInterface $container) | ||
{ | ||
$this->context = $context; | ||
$this->container = $container; | ||
$this->options = new Hash($context->options()); | ||
$this->arguments = new Hash($context->arguments()); | ||
} | ||
|
||
public function supplyFor(Closure $closure) | ||
{ | ||
$args = []; | ||
|
||
$ref = new \ReflectionFunction($closure); | ||
/** @var \ReflectionParameter[] $params */ | ||
$params = $ref->getParameters(); | ||
|
||
if (count($params) > 0) { | ||
$this->assignedArguments = []; | ||
$this->missingArgs = []; | ||
|
||
foreach ($params as $index => $param) { | ||
$class = $param->getClass(); | ||
$allow_null = $param->allowsNull(); | ||
|
||
$arg = $this->findArgumentFor( | ||
$param->getName(), | ||
$index, | ||
$class ? $class->getName() : null, | ||
$param->isArray() | ||
); | ||
|
||
$args[] = $arg; | ||
} | ||
|
||
} | ||
|
||
return $args; | ||
} | ||
|
||
protected function findArgumentFor($name, $index, $class, $is_array) | ||
{ | ||
if ($name === 'arguments' || $name === 'args') { | ||
return $this->hashToDesiredArgument($this->arguments, $class, $is_array); | ||
} | ||
|
||
if ($name === 'options' || $name === 'opts') { | ||
return $this->hashToDesiredArgument($this->options, $class, $is_array); | ||
} | ||
|
||
if ($name === 'context') { | ||
return $this->context; | ||
} | ||
|
||
if ($this->container->has($name)) { | ||
$result = $this->container->get($name); | ||
if (!$class || ($result instanceof $class)) { | ||
return $result; | ||
} | ||
} | ||
|
||
if (isset($this->arguments[$name])) { | ||
$result = $this->arguments[$name]; | ||
return $result; | ||
} | ||
|
||
if (isset($this->options[$name])) { | ||
$result = $this->options[$name]; | ||
return $result; | ||
} | ||
|
||
foreach ($this->arguments->all() as $index => $value) { | ||
if (is_integer($index) && in_array($index, $this->assignedArguments, true) === false) { | ||
if (($is_array && is_array($value)) || ($class && $value instanceof $class)) { | ||
$this->assignedArguments[] = $index; | ||
return $value; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected function hashToDesiredArgument(Hash $hash, $class, $is_array) | ||
{ | ||
if ($is_array) { | ||
return $hash->all(); | ||
} elseif ($class && $class !== 'Clim\\Helper\\Hash') { | ||
try { | ||
return new $class($hash->all()); | ||
} catch (\Exception $e) { | ||
// ... pass through | ||
} | ||
} | ||
|
||
return $hash; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Clim; | ||
|
||
use Clim\Helper\ArgumentsSupplier; | ||
use Clim\Helper\Hash; | ||
use Closure; | ||
use Psr\Container\ContainerInterface; | ||
use Slim\CallableResolverAwareTrait; | ||
|
||
class Task | ||
{ | ||
use CallableResolverAwareTrait; | ||
|
||
private $callable; | ||
|
||
private $container; | ||
|
||
/** | ||
* @param callable|string $callable | ||
* @param ContainerInterface $container | ||
*/ | ||
public function __construct($callable, ContainerInterface $container) | ||
{ | ||
$this->callable = $callable; | ||
$this->container = $container; | ||
} | ||
|
||
public function execute(Context $context) | ||
{ | ||
$callable = $this->resolveCallable($this->callable); | ||
if ($callable instanceof Closure) { | ||
$callable = $callable->bindTo($this->container); | ||
|
||
$supplier = new ArgumentsSupplier($context, $this->container); | ||
$args = $supplier->supplyFor($callable); | ||
} else { | ||
$options = $context->options(); | ||
$arguments = $context->arguments(); | ||
|
||
$args = [new Hash($options), new Hash($arguments)]; | ||
} | ||
|
||
return call_user_func_array($callable, $args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
$I = new UnitTester($scenario); | ||
$I->wantTo('perform actions and see result'); | ||
|
||
$app = new \Clim\App(); | ||
$container = $app->getContainer(); | ||
|
||
$container['hello'] = function () { | ||
return "Hello World!!!"; | ||
}; | ||
|
||
class SimpleArray | ||
{ | ||
public function __construct($array) | ||
{ | ||
} | ||
} | ||
|
||
$app->task(function ($hello, array $arguments, \Clim\Helper\Hash $opts, SimpleArray $options) use ($I) { | ||
$I->assertEquals("Hello World!!!", $hello); | ||
$I->assertTrue(is_array($arguments)); | ||
$I->assertTrue($opts instanceof \Clim\Helper\Hash); | ||
$I->assertTrue($options instanceof SimpleArray); | ||
return 42; | ||
}); | ||
|
||
$context = $app->runner()->run(['prog_name']); | ||
|
||
$I->assertEquals(42, $context->getResult()); |