1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-12 09:09:35 +01:00
parallel/lib/Worker/Internal/worker-process.php
Aaron Piotrowski f2e5224856
Unite Process and Parallel context interfaces
Allows scripts to be written for either context.
2019-02-17 23:50:41 -06:00

38 lines
888 B
PHP

<?php
namespace Amp\Parallel\Worker\Internal;
use Amp\Parallel\Sync;
use Amp\Parallel\Worker;
use Amp\Promise;
return function (Sync\Channel $channel) use ($argv): Promise {
if (!\defined("AMP_WORKER")) {
\define("AMP_WORKER", \AMP_CONTEXT);
}
if (!isset($argv[1])) {
throw new \Error("No environment class name provided");
}
$className = $argv[1];
if (!\class_exists($className)) {
throw new \Error(\sprintf("Invalid environment class name '%s'", $className));
}
if (!\is_subclass_of($className, Worker\Environment::class)) {
throw new \Error(\sprintf(
"The class '%s' does not implement '%s'",
$className,
Worker\Environment::class
));
}
$environment = new $className;
$runner = new Worker\TaskRunner($channel, $environment);
return $runner->run();
};