diff --git a/src/Forking/ForkContext.php b/src/Forking/ForkContext.php index a0bd34f..09a1232 100644 --- a/src/Forking/ForkContext.php +++ b/src/Forking/ForkContext.php @@ -32,11 +32,12 @@ class ForkContext extends Synchronized implements ContextInterface */ private $function; - public function __construct(callable $function) + public function __construct(callable $function /* , ...$args */) { parent::__construct(); $this->function = $function; + $this->args = array_slice(func_get_args(), 1); } /** @@ -104,8 +105,9 @@ class ForkContext extends Synchronized implements ContextInterface private function execute(Channel $channel) { try { - $function = $this->function; - $result = new ExitSuccess(yield $function($channel)); + $result = new ExitSuccess( + yield call_user_func_array($this->function, array_merge([$channel], $this->args)) + ); } catch (\Exception $exception) { $result = new ExitFailure($exception); } diff --git a/src/Threading/Thread.php b/src/Threading/Thread.php index c9ace03..a323e34 100644 --- a/src/Threading/Thread.php +++ b/src/Threading/Thread.php @@ -22,6 +22,11 @@ class Thread extends \Thread */ private $function; + /** + * @var + */ + private $args; + private $prepared = false; private $initialized = false; @@ -34,12 +39,14 @@ class Thread extends \Thread * Creates a new thread object. * * @param callable $function The function to execute in the thread. + * @param mixed[]|null $args Arguments to pass to the function. * @param string $autoloaderPath Path to autoloader include file. */ - public function __construct(callable $function, $autoloaderPath = '') + public function __construct(callable $function, array $args = [], $autoloaderPath = '') { $this->autoloaderPath = $autoloaderPath; $this->function = $function; + $this->args = $args; } /** @@ -111,8 +118,9 @@ class Thread extends \Thread private function execute(Channel $channel) { try { - $function = $this->function; - $result = new ExitSuccess(yield $function($channel)); + $result = new ExitSuccess( + yield call_user_func_array($this->function, array_merge([$channel], $this->args)) + ); } catch (\Exception $exception) { $result = new ExitFailure($exception); } diff --git a/src/Threading/ThreadContext.php b/src/Threading/ThreadContext.php index 9d8a138..cb526d9 100644 --- a/src/Threading/ThreadContext.php +++ b/src/Threading/ThreadContext.php @@ -31,9 +31,11 @@ class ThreadContext implements ContextInterface * * @param callable $function */ - public function __construct(callable $function) + public function __construct(callable $function /* , ...$args */) { - $this->thread = new Thread($function, $this->getComposerAutoloader()); + $args = array_slice(func_get_args(), 1); + + $this->thread = new Thread($function, $args, $this->getComposerAutoloader()); } /**