Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'SocalNick/hotfix/di-compiler-allow-refl…
Browse files Browse the repository at this point in the history
…ection-exception'
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/Definition/CompilerDefinition.php
Expand Up @@ -16,7 +16,9 @@ class CompilerDefinition implements DefinitionInterface
protected $isCompiled = false;

protected $introspectionStrategy = null;


protected $allowReflectionExceptions = false;

/**
* @var AggregateDirectoryScanner
*/
Expand Down Expand Up @@ -44,7 +46,12 @@ public function setIntrospectionStrategy(IntrospectionStrategy $introspectionStr
{
$this->introspectionStrategy = $introspectionStrategy;
}


public function setAllowReflectionExceptions($allowReflectionExceptions = true)
{
$this->allowReflectionExceptions = (bool) $allowReflectionExceptions;
}

/**
* Get introspection strategy
*
Expand Down Expand Up @@ -114,7 +121,14 @@ protected function processClass($class)
$strategy = $this->introspectionStrategy; // localize for readability

/** @var $rClass \Zend\Code\Reflection\ClassReflection */
$rClass = new Reflection\ClassReflection($class);
try {
$rClass = new Reflection\ClassReflection($class);
} catch (\ReflectionException $e) {
if (!$this->allowReflectionExceptions) {
throw $e;
}
return;
}
$className = $rClass->getName();
$matches = null; // used for regex below

Expand Down Expand Up @@ -159,7 +173,14 @@ protected function processClass($class)

if ($rClass->hasMethod('__construct')) {
$def['methods']['__construct'] = true; // required
$this->processParams($def, $rClass, $rClass->getMethod('__construct'));
try {
$this->processParams($def, $rClass, $rClass->getMethod('__construct'));
} catch (\ReflectionException $e) {
if (!$this->allowReflectionExceptions) {
throw $e;
}
return;
}
}


Expand Down
20 changes: 20 additions & 0 deletions test/Definition/CompilerDefinitionTest.php
Expand Up @@ -79,4 +79,24 @@ public function testCompilerFileScanner()
$this->assertContains('ZendTest\Di\TestAsset\CompilerClasses\C', $definition->getClassSupertypes('ZendTest\Di\TestAsset\CompilerClasses\E'));
$this->assertContains('ZendTest\Di\TestAsset\CompilerClasses\D', $definition->getClassSupertypes('ZendTest\Di\TestAsset\CompilerClasses\E'));
}

public function testCompilerReflectionException()
{
$this->setExpectedException('ReflectionException', 'Class ZendTest\Di\TestAsset\InvalidCompilerClasses\Foo does not exist');
$definition = new CompilerDefinition;
$definition->addDirectory(__DIR__ . '/../TestAsset/InvalidCompilerClasses');
$definition->compile();
}

public function testCompilerAllowReflectionException()
{
$definition = new CompilerDefinition;
$definition->setAllowReflectionExceptions();
$definition->addDirectory(__DIR__ . '/../TestAsset/InvalidCompilerClasses');
$definition->compile();
$parameters = $definition->getMethodParameters('ZendTest\Di\TestAsset\InvalidCompilerClasses\InvalidClass', '__construct');

// The exception gets caught before the parameter's class is set
$this->assertCount(1, current($parameters));
}
}
12 changes: 12 additions & 0 deletions test/TestAsset/InvalidCompilerClasses/InvalidClass.php
@@ -0,0 +1,12 @@
<?php

namespace ZendTest\Di\TestAsset\InvalidCompilerClasses;

class InvalidClass
{

public function __construct(Foo $foo)
{
$this->foo = $foo;
}
}

0 comments on commit 3b5c10e

Please sign in to comment.