mirror of
https://github.com/amphp/parallel.git
synced 2025-02-20 04:44:36 +01:00
Simplify checking environment class name
This commit is contained in:
parent
3f7f21875d
commit
51b6421933
20
bin/worker
20
bin/worker
@ -51,21 +51,19 @@ Amp\Loop::run(function () {
|
||||
|
||||
$className = $options["e"];
|
||||
|
||||
try {
|
||||
$reflection = new ReflectionClass($className);
|
||||
} catch (ReflectionException $e) {
|
||||
throw new Error(sprintf("Invalid class name '%s'", $className));
|
||||
if (!class_exists($className)) {
|
||||
throw new Error(sprintf("Invalid environment class name '%s'", $className));
|
||||
}
|
||||
|
||||
if (!$reflection->isInstantiable()) {
|
||||
throw new Error(sprintf("'%s' is not instatiable class", $className));
|
||||
if (!is_subclass_of($className, Worker\Environment::class)) {
|
||||
throw new Error(sprintf(
|
||||
"The class '%s' does not implement '%s'",
|
||||
$className,
|
||||
Worker\Environment::class
|
||||
));
|
||||
}
|
||||
|
||||
if (!$reflection->implementsInterface(Worker\Environment::class)) {
|
||||
throw new Error(sprintf("The class '%s' does not implement '%s'", $className,Worker\Environment::class));
|
||||
}
|
||||
|
||||
return $reflection->newInstance();
|
||||
return new $className;
|
||||
})();
|
||||
|
||||
$runner = new Worker\TaskRunner($channel, $environment);
|
||||
|
@ -15,21 +15,15 @@ class WorkerThread extends AbstractWorker {
|
||||
*/
|
||||
public function __construct(string $envClassName = BasicEnvironment::class) {
|
||||
parent::__construct(new Thread(function (string $className): Promise {
|
||||
try {
|
||||
$reflection = new \ReflectionClass($className);
|
||||
} catch (\ReflectionException $e) {
|
||||
throw new \Error(\sprintf("Invalid class name '%s'", $className));
|
||||
if (!\class_exists($className)) {
|
||||
throw new \Error(\sprintf("Invalid environment class name '%s'", $className));
|
||||
}
|
||||
|
||||
if (!$reflection->isInstantiable()) {
|
||||
throw new \Error(\sprintf("'%s' is not instatiable class", $className));
|
||||
}
|
||||
|
||||
if (!$reflection->implementsInterface(Environment::class)) {
|
||||
if (!\is_subclass_of($className, Environment::class)) {
|
||||
throw new \Error(\sprintf("The class '%s' does not implement '%s'", $className, Environment::class));
|
||||
}
|
||||
|
||||
$environment = $reflection->newInstance();
|
||||
$environment = new $className;
|
||||
|
||||
if (!\defined("AMP_WORKER")) {
|
||||
\define("AMP_WORKER", "amp-worker");
|
||||
|
Loading…
x
Reference in New Issue
Block a user