diff --git a/README.md b/README.md index 952b31a..1a5bce6 100644 --- a/README.md +++ b/README.md @@ -104,8 +104,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 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 2ff4c60..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/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