]> git.mxchange.org Git - friendica.git/commitdiff
Make InstanceManager strategy naming convention case insensitive.
authorPhilipp <admin@philipp.info>
Sat, 29 Jul 2023 11:20:02 +0000 (13:20 +0200)
committerPhilipp <admin@philipp.info>
Sat, 29 Jul 2023 11:20:02 +0000 (13:20 +0200)
src/Core/Hooks/Model/DiceInstanceManager.php
tests/src/Core/Hooks/Model/InstanceManagerTest.php

index 194fc6e5c7679e9f82a667823e2daaa57b5e8543..07ea729794d53be002d2117d44903811a6acf97a 100644 (file)
@@ -49,11 +49,11 @@ class DiceInstanceManager implements ICanCreateInstances, ICanRegisterStrategies
        /** {@inheritDoc} */
        public function registerStrategy(string $interface, string $class, ?string $name = null): ICanRegisterStrategies
        {
-               if (!empty($this->instance[$interface][$name])) {
+               if (!empty($this->instance[$interface][strtolower($name)])) {
                        throw new HookRegisterArgumentException(sprintf('A class with the name %s is already set for the interface %s', $name, $interface));
                }
 
-               $this->instance[$interface][$name] = $class;
+               $this->instance[$interface][strtolower($name)] = $class;
 
                return $this;
        }
@@ -61,10 +61,10 @@ class DiceInstanceManager implements ICanCreateInstances, ICanRegisterStrategies
        /** {@inheritDoc} */
        public function create(string $class, string $strategy, array $arguments = []): object
        {
-               if (empty($this->instance[$class][$strategy])) {
+               if (empty($this->instance[$class][strtolower($strategy)])) {
                        throw new HookInstanceException(sprintf('The class with the name %s isn\'t registered for the class or interface %s', $strategy, $class));
                }
 
-               return $this->dice->create($this->instance[$class][$strategy], $arguments);
+               return $this->dice->create($this->instance[$class][strtolower($strategy)], $arguments);
        }
 }
index 44459eb2eeed8171ba45e21550f7e401eedf179c..c9746d26a0432d97162df8bfa989ea3489ea859d 100644 (file)
@@ -235,4 +235,23 @@ class InstanceManagerTest extends MockedTest
                self::assertEquals($cBool, $getInstanceA->getCBool());
                self::assertEquals($cBool, $getInstanceB->getCBool());
        }
+
+       /**
+        * @see https://github.com/friendica/friendica/issues/13318
+        */
+       public function testCaseInsensitiveNames()
+       {
+               $instance = new DiceInstanceManager(new Dice(), $this->hookFileManager);
+
+               $instance->registerStrategy(IAmADecoratedInterface::class, FakeInstance::class, 'fake');
+
+               // CamelCase
+               self::assertInstanceOf(FakeInstance::class, $instance->create(IAmADecoratedInterface::class, 'Fake'));
+               // UPPER CASE
+               self::assertInstanceOf(FakeInstance::class, $instance->create(IAmADecoratedInterface::class, 'FAKE'));
+               // lower case
+               self::assertInstanceOf(FakeInstance::class, $instance->create(IAmADecoratedInterface::class, 'fake'));
+               // UnKnOwN
+               self::assertInstanceOf(FakeInstance::class, $instance->create(IAmADecoratedInterface::class, 'fAkE'));
+       }
 }