From 3143bca4cba6d937fbc537b9c87e793240ed45e2 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 | 17 ++++++++++++++--- src/Runtime/ServiceManager.php | 6 +++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Runtime/LegacyServiceInstantiator.php b/src/Runtime/LegacyServiceInstantiator.php index 7e18c3c7c9..45db45da40 100644 --- a/src/Runtime/LegacyServiceInstantiator.php +++ b/src/Runtime/LegacyServiceInstantiator.php @@ -119,6 +119,10 @@ public function instantiateServices(array $services) $info['arguments'] ?? [], $info['calls'] ?? [] ); + if (empty($service)) { + $this->logger->debug("Could not instantiate {class} for '{service_name}' service", ['class' => $info['class'], 'service_name' => $serviceName]); + continue; + } $this->instantiatedDrushServices[$serviceName] = $service; @@ -155,12 +159,15 @@ public function taggedServices($tagName) * @param string[] $arguments Parameters to class constructor * @param array $calls 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(string $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]); } @@ -178,7 +185,11 @@ public function create(string $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 bf97cb1d7f..894fe5bf8b 100644 --- a/src/Runtime/ServiceManager.php +++ b/src/Runtime/ServiceManager.php @@ -332,7 +332,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(); });