From 40f47458975cce451b5f90d97b6f10772f2b590d Mon Sep 17 00:00:00 2001 From: filecage Date: Wed, 22 May 2019 22:37:30 +0200 Subject: [PATCH 1/3] boyscout --- lib/FactoryCreatable.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/FactoryCreatable.php b/lib/FactoryCreatable.php index dae85f3..b93f566 100644 --- a/lib/FactoryCreatable.php +++ b/lib/FactoryCreatable.php @@ -6,6 +6,7 @@ class FactoryCreatable extends Creatable { /** * @param string $className + * @throws \ReflectionException */ function __construct ($className) { parent::__construct($this->buildFactoryClassName($className)); From 835017745a7387b1a95df2283c81c196244f179b Mon Sep 17 00:00:00 2001 From: filecage Date: Mon, 27 May 2019 10:33:19 +0200 Subject: [PATCH 2/3] removes dynamic Factory class builder from creation process --- lib/Creation.php | 49 +++---------------- lib/FactoryCreatable.php | 24 --------- lib/ResourceRegistry.php | 1 - tests/ExceptionsTest.php | 16 ------ ...FactoryInstanceTestUninstantiableClass.php | 5 -- ...InstanceTestUninstantiableClassFactory.php | 11 ----- .../InvalidFactoryTestUninstantiableClass.php | 5 -- ...dFactoryTestUninstantiableClassFactory.php | 9 ---- tests/Mocks/MoreExtendedInterfaceFactory.php | 41 ---------------- tests/Mocks/SimpleAbstractClassFactory.php | 16 ------ tests/Mocks/SimpleInterfaceFactory.php | 13 ----- tests/UninstantiableCreationTest.php | 39 --------------- 12 files changed, 6 insertions(+), 223 deletions(-) delete mode 100644 lib/FactoryCreatable.php delete mode 100644 tests/Mocks/InvalidFactoryInstanceTestUninstantiableClass.php delete mode 100644 tests/Mocks/InvalidFactoryInstanceTestUninstantiableClassFactory.php delete mode 100644 tests/Mocks/InvalidFactoryTestUninstantiableClass.php delete mode 100644 tests/Mocks/InvalidFactoryTestUninstantiableClassFactory.php delete mode 100644 tests/Mocks/MoreExtendedInterfaceFactory.php delete mode 100644 tests/Mocks/SimpleAbstractClassFactory.php delete mode 100644 tests/Mocks/SimpleInterfaceFactory.php diff --git a/lib/Creation.php b/lib/Creation.php index 4c08471..e830a53 100644 --- a/lib/Creation.php +++ b/lib/Creation.php @@ -122,10 +122,13 @@ private function createInstanceFromUninstantiableCreatable (Creatable $creatable if ($reflector->implementsInterface(Interfaces\Singleton::class)) { return $this->getInstanceFromSingleton($reflector); } elseif ($reflector->isInterface() || $reflector->isAbstract()) { - return $this->findsFulfillngInstanceForUninstantiableCreatable($creatable) ?? $this->createInstanceFromFactoryCreatable($creatable, $this->getFactoryClassCreatable($reflector)); - } else { - throw new Unresolvable('Class is neither instantiable nor implements Singleton interface', $reflector->getName()); + $instance = $this->findsFulfillngInstanceForUninstantiableCreatable($creatable); + if ($instance !== null) { + return $instance; + } } + + throw new Unresolvable('Class is neither instantiable nor implements Singleton interface', $reflector->getName()); } /** @@ -138,30 +141,6 @@ private function createInstanceFromCreatable (Creatable $creatable) { return (new Invocation($creatable, $this->resourceRegistry, $this->injectionRegistry))->invoke(); } - /** - * @param Creatable $creatable - * @param Creatable $factory - * - * @return object - * @throws Unresolvable - */ - private function createInstanceFromFactoryCreatable (Creatable $creatable, Creatable $factory) { - $factoryReflector = $factory->getReflectionClass(); - $reflector = $creatable->getReflectionClass(); - - $factory = $this->createInstance($factory); - if (!$factory instanceof Factory) { - throw new Unresolvable('Factory `' . $factoryReflector->getName() . '` does not implement required interface Creator\\Interfaces\\Factory', $reflector->getName()); - } - - $class = $factory->createInstance(); - if (!$reflector->isInstance($class)) { - throw new Unresolvable('Create method of factory `' . $factoryReflector->getName() . '` did not return instance of `' . $reflector->getName() . '` class', $reflector->getName()); - } - - return $class; - } - /** * @param ReflectionClass $reflector * @@ -179,20 +158,4 @@ private function findsFulfillngInstanceForUninstantiableCreatable (Creatable $cr return $this->injectionRegistry->findFulfillingInstance($creatable) ?? $this->resourceRegistry->findFulfillingInstance($creatable); } - /** - * @param ReflectionClass $reflector - * - * @return Creatable - * @throws Unresolvable - */ - private function getFactoryClassCreatable (ReflectionClass $reflector) { - $className = $reflector->getName(); - try { - $factoryCreatable = new FactoryCreatable($className); - } catch (ReflectionException $e) { - throw new Unresolvable('Can not load factory for uninstantiable class `' . $className . '`: ' . $e->getMessage(), $reflector->getName()); - } - - return $factoryCreatable; - } } \ No newline at end of file diff --git a/lib/FactoryCreatable.php b/lib/FactoryCreatable.php deleted file mode 100644 index b93f566..0000000 --- a/lib/FactoryCreatable.php +++ /dev/null @@ -1,24 +0,0 @@ -buildFactoryClassName($className)); - } - - /** - * @param string $className - * - * @return string - */ - private function buildFactoryClassName ($className) { - return sprintf('%sFactory', $className); - } - - } \ No newline at end of file diff --git a/lib/ResourceRegistry.php b/lib/ResourceRegistry.php index 7887ab0..6b96a2c 100644 --- a/lib/ResourceRegistry.php +++ b/lib/ResourceRegistry.php @@ -145,7 +145,6 @@ function hasPrimitiveResource (string $resourceKey) : string { * @param string $resourceKey * * @return mixed - * @throws UnresolvablePrimitive */ function getPrimitiveResource ($resourceKey) { return $this->primitiveResources[$resourceKey] ?? null; diff --git a/tests/ExceptionsTest.php b/tests/ExceptionsTest.php index 1d5f372..276b843 100644 --- a/tests/ExceptionsTest.php +++ b/tests/ExceptionsTest.php @@ -6,8 +6,6 @@ use Creator\Exceptions\Unresolvable; use Creator\Exceptions\UnresolvableDependency; use Creator\Tests\Mocks\InvalidClass; - use Creator\Tests\Mocks\InvalidFactoryInstanceTestUninstantiableClass; - use Creator\Tests\Mocks\InvalidFactoryTestUninstantiableClass; use Creator\Tests\Mocks\InvalidNestedRequirementClass; use Creator\Tests\Mocks\InvalidPrimitiveDependencyClass; use Creator\Tests\Mocks\InvalidRequirementClass; @@ -29,20 +27,6 @@ function testShouldThrowExceptionIfClassRequiresInexistentDependency () { $this->creator->create(InvalidRequirementClass::class); } - function testShouldThrowExceptionIfFactoryDoesNotImplementInterface () { - $this->expectException(Unresolvable::class); - $this->expectExceptionMessageRegExp('/^Factory `\S+` does not implement required interface/'); - - $this->creator->create(InvalidFactoryTestUninstantiableClass::class); - } - - function testShouldThrowExceptionIfFactoryDoesNotReturnRequestedInstance () { - $this->expectException(Unresolvable::class); - $this->expectExceptionMessageRegExp('/^Create method of factory `\S+` did not return instance/'); - - $this->creator->create(InvalidFactoryInstanceTestUninstantiableClass::class); - } - function testShouldThrowExceptionIfClassRequiresUnknownPrimitiveResource () { $this->expectException(UnresolvableDependency::class); $this->expectExceptionMessage('`Creator\Tests\Mocks\InvalidPrimitiveDependencyClass::__construct()` demands a primitive resource for parameter `$unknownParameter` but the resource is unresolvable'); diff --git a/tests/Mocks/InvalidFactoryInstanceTestUninstantiableClass.php b/tests/Mocks/InvalidFactoryInstanceTestUninstantiableClass.php deleted file mode 100644 index 324819b..0000000 --- a/tests/Mocks/InvalidFactoryInstanceTestUninstantiableClass.php +++ /dev/null @@ -1,5 +0,0 @@ -extendedClass = $extendedClass; - $this->simpleClass = $simpleClass; - $this->anotherSimpleClass = $anotherSimpleClass; - } - - /** - * @return MoreExtendedClass - */ - function createInstance () { - return new MoreExtendedClass($this->extendedClass, $this->simpleClass, $this->anotherSimpleClass); - } - - } \ No newline at end of file diff --git a/tests/Mocks/SimpleAbstractClassFactory.php b/tests/Mocks/SimpleAbstractClassFactory.php deleted file mode 100644 index 68e0fdb..0000000 --- a/tests/Mocks/SimpleAbstractClassFactory.php +++ /dev/null @@ -1,16 +0,0 @@ -assertInstanceOf(SimpleSingleton::class, $this->creator->create(SimpleSingleton::class)); } - function testExpectsInstanceFromUninstantiableInterface () { - $this->assertInstanceOf(SimpleInterface::class, $this->creator->create(SimpleInterface::class)); - } - - function testExpectsInstanceFromUninstantiableAbstractClass () { - $this->assertInstanceOf(SimpleAbstractClass::class, $this->creator->create(SimpleAbstractClass::class)); - } - - function testExpectsInstanceFromFactoryWithDependencies () { - $this->assertInstanceOf(ExtendedClass::class, $this->creator->create(ExtendedInterface::class)); - } - - function testExpectsFactoryInstanceCreatedWithInjectedInstance () { - $simpleInstance = new SimpleClass(); - /** @var ExtendedInterface $extendedInstance */ - $extendedInstance = $this->creator->createInjected(ExtendedInterface::class) - ->with($simpleInstance) - ->create(); - - $this->assertInstanceOf(ExtendedInterface::class, $extendedInstance); - $this->assertInstanceOf(ExtendedClass::class, $extendedInstance); - $this->assertSame($simpleInstance, $extendedInstance->getSimpleClass()); - } - - function testExpectsSupplierInstanceWhenDependingUninstantiable () { - $supplierInstance = new UninstantiableSupplier(); - $this->creator->registerClassResource($supplierInstance); - - $this->assertSame($supplierInstance, $this->creator->create(UninstantiableInterface::class)); - $this->assertSame($supplierInstance, $this->creator->create(UninstantiableSupplier::class)); - } - } \ No newline at end of file From 18fa885028dce4fc1d5eaa8ccbb04901fabda0e4 Mon Sep 17 00:00:00 2001 From: filecage Date: Mon, 1 Jul 2019 08:12:59 +0200 Subject: [PATCH 3/3] removes paragraphs explaining the implicit factory creation --- README.md | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2c409c4..eb35f08 100644 --- a/README.md +++ b/README.md @@ -86,8 +86,6 @@ Injecting instances is supported as well. If you have resources that can not be created without additional logic, but also should only be created once another component depends them, you can register a factory for this factory. A factory can be a callable, an instance of `Creator\Interfaces\Factory` or a class string of a Factory (see [lazy bound factories](#lazy-bound-factories)) and can be registered for any class resource, i.e. interfaces, abstracts or normal classes. -*Please note* that factory classes share the interface with Factories for [uninstantiable class factories](#uninstantiable-classes) but are different features and are being called at different points in the creation order. - ### Global Factories ````php