Skip to content

Commit

Permalink
Interface implementations were not saved.
Browse files Browse the repository at this point in the history
Because interface implementations were not stored in $loadedClasses, for every reference to the interface the implementing class was re-instantiated.
  • Loading branch information
GeoffreyDijkstra committed Nov 5, 2019
1 parent 1597979 commit 125277a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/DependencyInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
use ReflectionException;
use RuntimeException;

/**
* Class DependencyInjection
*
* @package Devorto\DependencyInjection
*/
class DependencyInjection
{
/**
Expand Down Expand Up @@ -68,6 +73,7 @@ public static function instantiate(string $class)
}

// Overwrite interface with class.
$interface = $class;
$class = static::$interfaceImplementations[$class];

try {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}

Expand Down
5 changes: 5 additions & 0 deletions src/KeyValueStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
use Iterator;
use Serializable;

/**
* Class KeyValueStorage
*
* @package Devorto\DependencyInjection
*/
class KeyValueStorage implements Iterator, Serializable
{
/**
Expand Down

0 comments on commit 125277a

Please sign in to comment.