From bae9873d8a6eefa1f96436c872774e7c9004c634 Mon Sep 17 00:00:00 2001 From: Ilia Novikov <3932742+ilyano@users.noreply.github.com> Date: Thu, 8 Feb 2024 16:10:45 +0300 Subject: [PATCH] Prevent drush from crushing, when command extends non-existent class - #5860 --- src/Runtime/LegacyServiceInstantiator.php | 16 +++++++++++++--- src/Runtime/ServiceManager.php | 6 +++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Runtime/LegacyServiceInstantiator.php b/src/Runtime/LegacyServiceInstantiator.php index b3aa6eacba..9f70f6529f 100644 --- a/src/Runtime/LegacyServiceInstantiator.php +++ b/src/Runtime/LegacyServiceInstantiator.php @@ -130,6 +130,9 @@ public function instantiateServices(array $services) $info['arguments'] ?? [], $info['calls'] ?? [] ); + if (empty($service)) { + continue; + } $this->instantiatedDrushServices[$serviceName] = $service; @@ -166,12 +169,15 @@ public function taggedServices($tagName) * @param string[] $arguments Parameters to class constructor * @param array Method names and arguments to call after object is instantiated * - * @return object - * Instantiated command handler from the service file + * @return object|null + * Instantiated command handler from the service file or empty result */ public function create($class, array $arguments, array $calls) { $instance = $this->instantiateObject($class, $arguments); + if (empty($instance)) { + return; + } foreach ($calls as $callInfo) { $this->call($instance, $callInfo[0], $callInfo[1]); } @@ -189,7 +195,11 @@ public function create($class, array $arguments, array $calls) */ public function instantiateObject($class, array $arguments) { - $refl = new \ReflectionClass($class); + try { + $refl = new \ReflectionClass($class); + } catch (\Throwable $e) { + return; + } return $refl->newInstanceArgs($this->resolveArguments($arguments)); } diff --git a/src/Runtime/ServiceManager.php b/src/Runtime/ServiceManager.php index 29a401bc60..e21185b1fd 100644 --- a/src/Runtime/ServiceManager.php +++ b/src/Runtime/ServiceManager.php @@ -309,7 +309,11 @@ public function instantiateServices(array $bootstrapCommandClasses, DrushContain // n.b. we cannot simply use 'isInstantiable' here because // the constructor is typically protected when using a static create method $bootstrapCommandClasses = array_filter($bootstrapCommandClasses, function ($class) { - $reflection = new \ReflectionClass($class); + try { + $reflection = new \ReflectionClass($class); + } catch (\Throwable $e) { + return false; + } return !$reflection->isAbstract(); });