Simplify checking environment class name

This commit is contained in:
Aaron Piotrowski 2017-11-30 23:58:41 -06:00
parent 3f7f21875d
commit 51b6421933
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
2 changed files with 13 additions and 21 deletions

View File

@ -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);

View File

@ -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");