Skip to content

Commit

Permalink
Merge bcd50d2 into adb8426
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Jun 11, 2016
2 parents adb8426 + bcd50d2 commit 4a618bb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/DI/Definition/Resolver/FactoryResolver.php
Expand Up @@ -62,9 +62,20 @@ public function resolve(Definition $definition, array $parameters = [])
$this->invoker = new Invoker($parameterResolver, $this->container);
}

$callable = $definition->getCallable();

try {
return $this->invoker->call($definition->getCallable(), [$this->container, $definition]);
return $this->invoker->call($callable, [$this->container, $definition]);
} catch (NotCallableException $e) {
// Custom error message to help debugging
if (is_string($callable) && class_exists($callable) && method_exists($callable, '__invoke')) {
throw new DefinitionException(sprintf(
'Entry "%s" cannot be resolved: factory %s. Invokable classes cannot be automatically resolved if autowiring is disabled on the container, you need to enable autowiring or define the entry manually.',
$definition->getName(),
$e->getMessage()
));
}

throw new DefinitionException(sprintf(
'Entry "%s" cannot be resolved: factory %s',
$definition->getName(),
Expand Down
15 changes: 15 additions & 0 deletions tests/IntegrationTest/Definitions/FactoryDefinitionTest.php
Expand Up @@ -84,6 +84,21 @@ public function test_named_invokable_container_entry_as_factory()
$this->assertSame('bar', $container->get('factory'));
}

/**
* @expectedException \DI\Definition\Exception\DefinitionException
* @expectedExceptionMessage Invokable classes cannot be automatically resolved if autowiring is disabled on the container, you need to enable autowiring or define the entry manually.
*/
public function test_error_message_on_invokable_class_without_autowiring()
{
$builder = new ContainerBuilder();
$builder->addDefinitions([
'factory' => \DI\factory(FactoryDefinitionInvokableTestClass::class),
]);
$builder->useAutowiring(false);
$container = $builder->build();
$container->get('factory');
}

public function test_container_gets_injected_as_first_argument_without_typehint()
{
$container = $this->createContainer([
Expand Down

0 comments on commit 4a618bb

Please sign in to comment.