From 125277af462c15ea61707f1b71446b5702e9c7cd Mon Sep 17 00:00:00 2001 From: Geoffrey Dijkstra Date: Tue, 5 Nov 2019 15:28:11 +0100 Subject: [PATCH] Interface implementations were not saved. Because interface implementations were not stored in $loadedClasses, for every reference to the interface the implementing class was re-instantiated. --- src/DependencyInjection.php | 16 ++++++++++++++++ src/KeyValueStorage.php | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/src/DependencyInjection.php b/src/DependencyInjection.php index 4c7849c..b87c0e8 100644 --- a/src/DependencyInjection.php +++ b/src/DependencyInjection.php @@ -6,6 +6,11 @@ use ReflectionException; use RuntimeException; +/** + * Class DependencyInjection + * + * @package Devorto\DependencyInjection + */ class DependencyInjection { /** @@ -68,6 +73,7 @@ public static function instantiate(string $class) } // Overwrite interface with class. + $interface = $class; $class = static::$interfaceImplementations[$class]; try { @@ -79,6 +85,11 @@ public static function instantiate(string $class) $arguments = $reflection->getConstructor(); if (empty($arguments)) { + // If the current class is an interface implementation also save the interface class. + if (isset($interface)) { + return static::$loadedClasses[$interface] = static::$loadedClasses[$class] = new $class; + } + return static::$loadedClasses[$class] = new $class; } @@ -126,6 +137,11 @@ public static function instantiate(string $class) } } + // If the current class is an interface implementation also save the interface class. + if (isset($interface)) { + return static::$loadedClasses[$interface] = static::$loadedClasses[$class] = new $class(...$parameters); + } + return static::$loadedClasses[$class] = new $class(...$parameters); } diff --git a/src/KeyValueStorage.php b/src/KeyValueStorage.php index 2fd6628..bbb17bc 100644 --- a/src/KeyValueStorage.php +++ b/src/KeyValueStorage.php @@ -6,6 +6,11 @@ use Iterator; use Serializable; +/** + * Class KeyValueStorage + * + * @package Devorto\DependencyInjection + */ class KeyValueStorage implements Iterator, Serializable { /**