From 78c9aea61fc07f275cb84512a0d2f155613e13ac Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 23 Mar 2022 10:39:58 +1300 Subject: [PATCH] ENH Faster method for creating injected instances (#10265) --- src/Core/Injector/InjectionCreator.php | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/src/Core/Injector/InjectionCreator.php b/src/Core/Injector/InjectionCreator.php index 44fe99a21..0a43bf136 100644 --- a/src/Core/Injector/InjectionCreator.php +++ b/src/Core/Injector/InjectionCreator.php @@ -2,29 +2,18 @@ namespace SilverStripe\Core\Injector; -use ReflectionClass; -use ReflectionException; - /** * A class for creating new objects by the injector. */ class InjectionCreator implements Factory { - public function create($class, array $params = []) { - try { - $reflector = new ReflectionClass($class); - } catch (ReflectionException $e) { - throw new InjectorNotFoundException($e); + if (!class_exists($class)) { + throw new InjectorNotFoundException("Class {$class} does not exist"); } - - if (count($params)) { - // Remove named keys to ensure that PHP7 and PHP8 interpret these the same way - $params = array_values($params); - return $reflector->newInstanceArgs($params); - } - - return $reflector->newInstance(); + // Ensure there are no string keys as they cannot be unpacked with the `...` operator + $values = array_values($params); + return new $class(...$values); } }