Skip to content

Commit

Permalink
fix(compiling): unable to compile interface type-hinted argument
Browse files Browse the repository at this point in the history
  • Loading branch information
hultberg committed Aug 25, 2018
1 parent 7a94fe5 commit 24a2789
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v2.1.1

* Fix unable to compile any function that had interface type-hinted in its arguments.

## v2.1.0

* Add definition `value`, it can hold any value you provide it.
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private function resolveParameters(array $parameters, array $extraParameters = [
if (!$parameter->isOptional()) {
$type = $parameter->getType();

if ($type !== null && !$type->isBuiltin() && class_exists($type->getName())) {
if ($type !== null && !$type->isBuiltin() && \HbLib\Container\classNameExists($type->getName())) {
// a class we can, create a reference to it and compile.
$resolvedParameters[$name] = new DefinitionReference($type->getName());
continue;
Expand Down
20 changes: 1 addition & 19 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public function resolveArguments(\ReflectionFunctionAbstract $function, array $a
$resolvedParameters[$parameter->getName()] = $parameterValue;
continue;
} else {
if ($type !== null && !$type->isBuiltin() && $this->classNameExists($type->getName())) {
if ($type !== null && !$type->isBuiltin() && \HbLib\Container\classNameExists($type->getName())) {
// We need to resolve a class. In php you cant pass a default class instance AFAIK
try {
$resolvedParameters[$parameter->getName()] = $this->get($type->getName());
Expand Down Expand Up @@ -295,24 +295,6 @@ public function resolveArguments(\ReflectionFunctionAbstract $function, array $a
return $resolvedParameters;
}

/**
* Determines if a classname exists. Able to handle interface, traits and classes.
*
* @param string $className
*
* @return bool
*/
private function classNameExists($className): bool
{
try {
new \ReflectionClass($className);
} catch (\ReflectionException $e) {
return false;
}

return true;
}

/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
Expand Down
5 changes: 5 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ function value($value): DefinitionValue
{
return new DefinitionValue($value);
}

function classNameExists($value): bool
{
return class_exists($value) || interface_exists($value);
}

}
19 changes: 19 additions & 0 deletions tests/CompiledContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ public function testCompileResolve()
self::assertInstanceOf(Class1::class, $container->get(Class1::class));
}

public function testCompileFactoryWithInterfaceArgument()
{
$containerBuilder = new ContainerBuilder(new DefinitionSource([
Class2::class => factory(function(Interface1 $interface) {
return new Class1();
}),
Class100::class => resolve(),
Interface1::class => reference(Class100::class),
]));
$containerBuilder->enableCompiling($this->createTempFile(), $this->getUniqueClassName());
$container = $containerBuilder->build();

self::assertInstanceOf(Class1::class, $container->get(Class2::class));
}

public function testCompileFactory()
{
$containerBuilder = new ContainerBuilder(new DefinitionSource([
Expand Down Expand Up @@ -188,3 +203,7 @@ function __construct(string $name) {
$this->name = $name;
}
}

class Class100 implements Interface1 {

}
8 changes: 1 addition & 7 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,7 @@ public function testCallNotInvokeable()

public function testClassNameExists()
{
$container = new Container();

$ref = new \ReflectionClass(Container::class);
$classExistsMethod = $ref->getMethod('classNameExists');
$classExistsMethod->setAccessible(true);

self::assertFalse($classExistsMethod->invoke($container, 'SomeClassThatDoNotExist'));
self::assertFalse(\HbLib\Container\classNameExists('SomeClassThatDoNotExist'));
}

public function testMakeNotSingleton()
Expand Down

0 comments on commit 24a2789

Please sign in to comment.