2017-12-10 22:37:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Parallel\Worker\Internal;
|
|
|
|
|
|
|
|
use Amp\Parallel\Sync;
|
|
|
|
use Amp\Parallel\Worker;
|
|
|
|
use Amp\Promise;
|
|
|
|
|
2019-02-21 00:39:39 +01:00
|
|
|
return function (Sync\Channel $channel) use ($argc, $argv): Promise {
|
2017-12-10 22:37:33 +01:00
|
|
|
if (!\defined("AMP_WORKER")) {
|
2019-02-18 06:50:41 +01:00
|
|
|
\define("AMP_WORKER", \AMP_CONTEXT);
|
2017-12-10 22:37:33 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 00:39:39 +01:00
|
|
|
if (isset($argv[2])) {
|
|
|
|
if (!\is_file($argv[2])) {
|
2019-02-22 22:43:42 +01:00
|
|
|
throw new \Error(\sprintf("No file found at bootstrap file path given '%s'", $argv[2]));
|
2019-02-21 00:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Include file within closure to protect scope.
|
2019-08-28 01:14:56 +02:00
|
|
|
(function () use ($argc, $argv): void {
|
2019-02-21 00:39:39 +01:00
|
|
|
require $argv[2];
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
2017-12-10 22:37:33 +01:00
|
|
|
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();
|
|
|
|
};
|