From 6fffceb13fbaf412f770a24f9ff21dafad4bc906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20Scho=CC=88ndorn?= Date: Tue, 5 Sep 2017 16:21:28 +0200 Subject: [PATCH] Added alias argument to share method --- Source/DependencyInjector.php | 8 +++++++- Source/DependencyInjectorInterface.php | 3 ++- Test/Unit/DependencyInjectorTest.php | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Source/DependencyInjector.php b/Source/DependencyInjector.php index 236a8a6..3c3f2a2 100644 --- a/Source/DependencyInjector.php +++ b/Source/DependencyInjector.php @@ -89,14 +89,20 @@ public function __construct(ReflectionHandlerInterface $reflectionHandler, Logge * The given class instance will be treated like a singleton (at least until overloaded). * * @param object $classInstance + * @param string|null $alias * * @return DependencyInjectorInterface */ - public function share($classInstance): DependencyInjectorInterface + public function share($classInstance, string $alias = null): DependencyInjectorInterface { $classId = get_class($classInstance); $this->logger->debug('Registered shared instance of class {classId}', [ 'classId' => $classId ]); $this->sharedInstances[$classId] = $classInstance; + + if ($alias) { + $this->alias($classId, $alias); + } + return $this; } diff --git a/Source/DependencyInjectorInterface.php b/Source/DependencyInjectorInterface.php index 2c15198..cf186d3 100644 --- a/Source/DependencyInjectorInterface.php +++ b/Source/DependencyInjectorInterface.php @@ -40,10 +40,11 @@ public function __construct(ReflectionHandlerInterface $reflectionHandler, Logge * The given class instance will be treated like a singleton (at least until overloaded). * * @param object $classInstance + * @param string|null $alias * * @return DependencyInjectorInterface */ - public function share($classInstance): DependencyInjectorInterface; + public function share($classInstance, string $alias = null): DependencyInjectorInterface; /** * Registers a new class id and a factory in order to create instances of the referenced class. diff --git a/Test/Unit/DependencyInjectorTest.php b/Test/Unit/DependencyInjectorTest.php index 2593473..1892a0d 100644 --- a/Test/Unit/DependencyInjectorTest.php +++ b/Test/Unit/DependencyInjectorTest.php @@ -85,6 +85,25 @@ public function testShare() verify($shares[TestClass01::class])->same($instance); } + public function testShareWithAlias() + { + verify($this->handler)->isInstanceOf(ReflectionHandlerInterface::class); + $alias = 'a random alias'; + $instance = new TestClass01(); + $di = new DependencyInjector($this->handler, $this->logger); + $accessor = new ClassAccessor($di); + + verify($di->share($instance, $alias))->same($di); + + $shares = $accessor->getProperty('sharedInstances'); + verify(isset($shares[TestClass01::class]))->true(); + verify($shares[TestClass01::class])->same($instance); + + $aliases = $accessor->getProperty('aliases'); + verify(isset($aliases[$alias]))->true(); + verify($aliases[$alias])->equals(TestClass01::class); + } + public function testDelegate() { $delegate = function() {};