1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-02 17:52:14 +01:00
parallel/lib/Worker/CallableTask.php
2018-11-04 10:17:19 -06:00

39 lines
1.0 KiB
PHP

<?php
namespace Amp\Parallel\Worker;
/**
* Task implementation dispatching a simple callable.
*/
final class CallableTask implements Task
{
/** @var string */
private $callable;
/** @var mixed[] */
private $args;
/**
* @param callable $callable Callable will be serialized.
* @param mixed $args Arguments to pass to the function. Must be serializable.
*/
public function __construct(callable $callable, array $args)
{
$this->callable = $callable;
$this->args = $args;
}
public function run(Environment $environment)
{
if ($this->callable instanceof \__PHP_Incomplete_Class) {
throw new \Error('When using a class instance as a callable, the class must be autoloadable');
}
if (\is_array($this->callable) && ($this->callable[0] ?? null) instanceof \__PHP_Incomplete_Class) {
throw new \Error('When using a class instance method as a callable, the class must be autoloadable');
}
return ($this->callable)(...$this->args);
}
}