1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-04 18:47:50 +01:00
parallel/lib/Worker/Internal/worker-process.php
Aaron Piotrowski 7c8f936618
Autoloading → Bootstrap
Calling the file a boostrap makes more sense as more than defining an autoloader can be done in the file.
2019-02-22 16:10:30 -06:00

49 lines
1.2 KiB
PHP

<?php
namespace Amp\Parallel\Worker\Internal;
use Amp\Parallel\Sync;
use Amp\Parallel\Worker;
use Amp\Promise;
return function (Sync\Channel $channel) use ($argc, $argv): Promise {
if (!\defined("AMP_WORKER")) {
\define("AMP_WORKER", \AMP_CONTEXT);
}
if (isset($argv[2])) {
if (!\is_file($argv[2])) {
throw new \Error(\sprintf("No file found at bootstrap file path given '%s'", $argv[2]));
}
// Include file within closure to protect scope.
(function () use ($argc, $argv) {
require $argv[2];
})();
}
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();
};