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

Commit

Permalink
Merge branch 'hotfix/4649' into develop
Browse files Browse the repository at this point in the history
Forward port #4649
  • Loading branch information
weierophinney committed Jun 28, 2013
2 parents 58703e7 + 3924e0e commit 3977a0b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 78 deletions.
24 changes: 4 additions & 20 deletions library/Zend/Code/Generator/FileGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Zend\Code\Generator;

use Zend\Code\Reflection\Exception as ReflectionException;
use Zend\Code\Reflection\FileReflection;

class FileGenerator extends AbstractGenerator
Expand Down Expand Up @@ -66,30 +67,13 @@ public function __construct($options = null)
*
* @param string $filePath
* @param bool $includeIfNotAlreadyIncluded
* @throws Exception\InvalidArgumentException If file does not exists
* @throes Exception\RuntimeException If file exists but is not included or required
* @throws ReflectionException\InvalidArgumentException If file does not exists
* @throws ReflectionException\RuntimeException If file exists but is not included or required
* @return FileGenerator
*/
public static function fromReflectedFileName($filePath, $includeIfNotAlreadyIncluded = true)
{
$realpath = realpath($filePath);

if ($realpath === false) {
$realpath = stream_resolve_include_path($filePath);
}

if (!$realpath) {
throw new Exception\InvalidArgumentException(sprintf(
'No file for %s was found.',
$filePath
));
}

if ($includeIfNotAlreadyIncluded && !in_array($realpath, get_included_files())) {
include $realpath;
}

$fileReflector = new FileReflection($realpath);
$fileReflector = new FileReflection($filePath, $includeIfNotAlreadyIncluded);
$codeGenerator = static::fromReflection($fileReflector);

return $codeGenerator;
Expand Down
23 changes: 18 additions & 5 deletions library/Zend/Code/Reflection/FileReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,34 @@ class FileReflection implements ReflectionInterface

/**
* @param string $filename
* @throws Exception\RuntimeException
* @param bool $includeIfNotAlreadyIncluded
* @throws Exception\InvalidArgumentException If file does not exists
* @throws Exception\RuntimeException If file exists but is not included or required
*/
public function __construct($filename)
public function __construct($filename, $includeIfNotAlreadyIncluded = false)
{
if (($fileRealPath = realpath($filename)) === false) {
$fileRealPath = stream_resolve_include_path($filename);
}

if (!$fileRealPath || !in_array($fileRealPath, get_included_files())) {
throw new Exception\RuntimeException(sprintf(
'File %s must be required before it can be reflected',
if (!$fileRealPath) {
throw new Exception\InvalidArgumentException(sprintf(
'No file for %s was found.',
$filename
));
}

if (!in_array($fileRealPath, get_included_files())) {
if (!$includeIfNotAlreadyIncluded) {
throw new Exception\RuntimeException(sprintf(
'File %s must be required before it can be reflected',
$filename
));
}

include $fileRealPath;
}

$this->filePath = $fileRealPath;
$this->reflect();
}
Expand Down
55 changes: 3 additions & 52 deletions tests/ZendTest/Code/Generator/FileGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,58 +300,9 @@ public function testCreateFromArrayWithClassFromArray()
$this->assertInstanceOf('Zend\Code\Generator\ClassGenerator', $class);
}

public function testGeneratingFromAReflectedFilenameShouldRaiseExceptionIfFileDoesNotExist()
public function testGeneratingFromAReflectedFileName()
{
$this->setExpectedException('Zend\Code\Generator\Exception\InvalidArgumentException', 'found');
$generator = FileGenerator::fromReflectedFileName(__DIR__ . '/does/not/exist.really');
}

public function testGeneratingFromAReflectedFilenameShouldRaiseExceptionIfFileDoesNotExistInIncludePath()
{
$this->setExpectedException('Zend\Code\Generator\Exception\InvalidArgumentException', 'found');
FileGenerator::fromReflectedFileName('an_empty_file.php');
}

public function testGeneratingFromAReflectedFilenameInIncludePathWithoutIncludeFlagEnable()
{
$this->setExpectedException('Zend\Code\Reflection\Exception\RuntimeException', 'must be required');
$oldIncludePath = set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/TestAsset/');

try {
FileGenerator::fromReflectedFileName('an_empty_file.php', false);
set_include_path($oldIncludePath);
$this->fail('Should throw exception');
} catch(Exception $e) {
set_include_path($oldIncludePath);
throw $e;
}
}

public function testGeneratingFromAReflectedFilenameIncluded()
{
include_once __DIR__ . '/TestAsset/an_empty_file.php';
$oldIncludePath = set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/TestAsset/');

try {
FileGenerator::fromReflectedFileName('an_empty_file.php', false);
set_include_path($oldIncludePath);
} catch(Exception $e) {
set_include_path($oldIncludePath);
throw $e;
}
}

public function testGeneratingFromAReflectedFilenameInIncludePath()
{
$this->assertFalse(in_array(realpath(__DIR__ . '/TestAsset/a_second_empty_file.php'), get_included_files()));
$oldIncludePath = set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/TestAsset/');

try {
FileGenerator::fromReflectedFileName('a_second_empty_file.php');
set_include_path($oldIncludePath);
} catch(Exception $e) {
set_include_path($oldIncludePath);
throw $e;
}
$generator = FileGenerator::fromReflectedFileName(__DIR__ . '/TestAsset/OneInterface.php');
$this->assertInstanceOf('Zend\Code\Generator\FileGenerator', $generator);
}
}
46 changes: 45 additions & 1 deletion tests/ZendTest/Code/Reflection/FileReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace ZendTest\Code\Reflection;

use Exception;
use Zend\Code\Reflection\FileReflection;

/**
Expand All @@ -31,10 +32,53 @@ public function testFileConstructor()
public function testFileConstructorThrowsExceptionOnNonExistentFile()
{
$nonExistentFile = 'Non/Existent/File.php';
$this->setExpectedException('Zend\Code\Reflection\Exception\RuntimeException', 'File Non/Existent/File.php must be required before it can be reflected');
$this->setExpectedException('Zend\Code\Reflection\Exception\InvalidArgumentException', 'found');
$reflectionFile = new FileReflection($nonExistentFile);
}

public function testFileConstructorFromAReflectedFilenameInIncludePathWithoutIncludeFlagEnabled()
{
$this->setExpectedException('Zend\Code\Reflection\Exception\RuntimeException', 'must be required');
$oldIncludePath = set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/TestAsset/');

try {
new FileReflection('an_empty_file.php', false);
set_include_path($oldIncludePath);
$this->fail('Should throw exception');
} catch (Exception $e) {
set_include_path($oldIncludePath);
throw $e;
}
}

public function testFileConstructorFromAReflectedFilenameIncluded()
{
include_once __DIR__ . '/TestAsset/an_empty_file.php';
$oldIncludePath = set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/TestAsset/');

try {
new FileReflection('an_empty_file.php', false);
set_include_path($oldIncludePath);
} catch (Exception $e) {
set_include_path($oldIncludePath);
throw $e;
}
}

public function testFileConstructorFromAReflectedFilenameInIncludePath()
{
$this->assertFalse(in_array(realpath(__DIR__ . '/TestAsset/a_second_empty_file.php'), get_included_files()));
$oldIncludePath = set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/TestAsset/');

try {
new FileReflection('a_second_empty_file.php', true);
set_include_path($oldIncludePath);
} catch(Exception $e) {
set_include_path($oldIncludePath);
throw $e;
}
}

public function testFileGetClassReturnsClassReflectionObject()
{
$fileToReflect = __DIR__ . '/TestAsset/TestSampleClass.php';
Expand Down

0 comments on commit 3977a0b

Please sign in to comment.