Skip to content

Commit

Permalink
Merge pull request #15 from henriquemoody/callable
Browse files Browse the repository at this point in the history
Use PHPFluent\Callback to run callbacks
  • Loading branch information
henriquemoody committed Dec 3, 2014
2 parents 24d8ff7 + c30ffe8 commit 13bdfee
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 94 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ composer require arara/process
- PHP 5.4+
- [PCNTL](http://php.net/pcntl)
- [POSIX](http://php.net/posix)
- [PHPFluent\Callback](https://github.com/PHPFluent/Callback) (installed by Composer)

Version [1.6.0](https://github.com/Arara/Process/tree/1.6.0) or less of Arara\Process works on PHP 5.3.

Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
"require": {
"ext-pcntl": "*",
"ext-posix": "*",
"php": ">=5.4.0"
"php": ">=5.4.0",
"phpfluent/callback": "~1.0.0"
},
"require-dev": {
"ext-uopz": "*",
"phpmd/phpmd" : "@stable",
"phpunit/phpunit": "@stable",
"squizlabs/php_codesniffer": "@stable"
"phpmd/phpmd" : "~2.1.3",
"phpunit/phpunit": "~4.3.5",
"squizlabs/php_codesniffer": "~1.5.5"
},
"autoload": {
"psr-0": {
Expand Down
121 changes: 83 additions & 38 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 36 additions & 14 deletions src/Arara/Process/Action/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,47 @@

use Arara\Process\Context;
use Arara\Process\Control;
use InvalidArgumentException;
use PHPFluent\Callback\Callback as FluentCallback;

/**
* {@inheritDoc}
*/
class Callback implements Action
{
/**
* @var FluentCallback
*/
protected $callback;

/**
* @var array
*/
protected $handlers = array();

/**
* @param callable $callback Callback to run as action.
*/
public function __construct($callback)
public function __construct(callable $callback)
{
if (! is_callable($callback)) {
throw new InvalidArgumentException('Given action is not a valid callback');
}
$this->callback = $this->fluentCallback($callback);
}

$this->callback = $callback;
/**
* @return callable
*/
public function getCallable()
{
return $this->callback->getCallable();
}

/**
* Creates a fluent callback based by the given callable.
*
* @return FluentCallback
*/
protected function fluentCallback(callable $callable)
{
return new FluentCallback($callable);
}

/**
Expand All @@ -43,7 +64,7 @@ public function trigger($event, Control $control, Context $context)
if ($event !== ($key & $event)) {
continue;
}
call_user_func($handler, $control, $context);
call_user_func($handler, $event, $control, $context);
break;
}
}
Expand All @@ -55,13 +76,9 @@ public function trigger($event, Control $control, Context $context)
* @param callable $handler Callback to handle the event (or events).
* @return void
*/
public function bind($event, $handler)
public function bind($event, callable $handler)
{
if (! is_callable($handler)) {
throw new InvalidArgumentException('Given event handler is not a valid callback');
}

$this->handlers[$event] = $handler;
$this->handlers[$event] = $this->fluentCallback($handler);
}

/**
Expand All @@ -71,6 +88,11 @@ public function bind($event, $handler)
*/
public function getHandlers()
{
return $this->handlers;
$handlers = array();
foreach ($this->handlers as $key => $handler) {
$handlers[$key] = $handler->getCallable();
}

return $handlers;
}
}
Loading

0 comments on commit 13bdfee

Please sign in to comment.