Skip to content

Commit

Permalink
Merge pull request zendframework#4790 from nickpeirson/DIClassExisten…
Browse files Browse the repository at this point in the history
…ceCheck

Add check to DI to see if we have a class to instantiate
  • Loading branch information
weierophinney committed Jul 19, 2013
2 parents 68691c0 + 2863eba commit 92b2773
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
7 changes: 7 additions & 0 deletions library/Zend/Di/Di.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,13 @@ protected function createInstanceViaConstructor($class, $params, $alias = null)
$callParameters = $this->resolveMethodParameters($class, '__construct', $params, $alias, self::METHOD_IS_CONSTRUCTOR, true);
}

if (!class_exists($class)) {
if (interface_exists($class)) {
throw new Exception\ClassNotFoundException('Cannot instantiate interface '.$class);
}
throw new Exception\ClassNotFoundException($class.' does not exist');
}

// Hack to avoid Reflection in most common use cases
switch (count($callParameters)) {
case 0:
Expand Down
35 changes: 35 additions & 0 deletions tests/ZendTest/Di/DiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,41 @@ public function testNewInstanceWillUsePreferredClassForInterfaceHints()
$this->assertSame($a, $d->a);
}

public function testNewInstanceWillThrowAnClassNotFoundExceptionWhenClassIsAnInterface()
{
$definitionArray = array (
'ZendTest\\Di\\TestAsset\\ConstructorInjection\\D' => array(
'supertypes' => array(),
'instantiator' => '__construct',
'methods' => array('__construct' => 3),
'parameters' => array(
'__construct' =>
array (
'ZendTest\\Di\\TestAsset\\ConstructorInjection\\D::__construct:0' => array(
0 => 'd',
1 => 'ZendTest\\Di\\TestAsset\\DummyInterface',
2 => true,
3 => NULL,
),
),
),
),
'ZendTest\\Di\\TestAsset\\DummyInterface' => array(
'supertypes' => array(),
'instantiator' => NULL,
'methods' => array(),
'parameters' => array(),
),
);
$definitionList = new DefinitionList(array(
new Definition\ArrayDefinition($definitionArray)
));
$di = new Di($definitionList);

$this->setExpectedException('Zend\Di\Exception\ClassNotFoundException', 'Cannot instantiate interface');
$di->get('ZendTest\Di\TestAsset\ConstructorInjection\D');
}

public function testMatchPreferredClassWithAwareInterface()
{
$di = new Di();
Expand Down
21 changes: 21 additions & 0 deletions tests/ZendTest/Di/TestAsset/ConstructorInjection/D.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Di\TestAsset\ConstructorInjection;

use ZendTest\Di\TestAsset\DummyInterface;

class D
{
protected $d = null;
public function __construct(DummyInterface $d)
{
$this->d = $d;
}
}
15 changes: 15 additions & 0 deletions tests/ZendTest/Di/TestAsset/DummyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Di\TestAsset;

interface DummyInterface
{
public function __construct();
}

0 comments on commit 92b2773

Please sign in to comment.