Created
May 2, 2012 13:52
-
-
Save sasezaki/2576661 to your computer and use it in GitHub Desktop.
Zend Framework 2 Code component fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git library/Zend/Code/Generator/ClassGenerator.php library/Zend/Code/Generator/ClassGenerator.php | |
| index 5e633cf..2fa7724 100644 | |
| --- library/Zend/Code/Generator/ClassGenerator.php | |
| +++ library/Zend/Code/Generator/ClassGenerator.php | |
| @@ -139,7 +139,8 @@ class ClassGenerator extends AbstractGenerator | |
| $methods = array(); | |
| foreach ($classReflection->getMethods() as $reflectionMethod) { | |
| /* @var $reflectionMethod \MethodReflection\Code\Reflection\ReflectionMethod */ | |
| - if ($reflectionMethod->getDeclaringClass()->getName() == $cg->getName()) { | |
| + // @todo should check namespace | |
| + if ($reflectionMethod->getDeclaringClass()->getName() == $cg->getNamespaceName()."\\".$cg->getName()) { | |
| $methods[] = MethodGenerator::fromReflection($reflectionMethod); | |
| } | |
| } | |
| @@ -195,14 +196,16 @@ class ClassGenerator extends AbstractGenerator | |
| $cg->setImplementedInterfaces($value); | |
| break; | |
| case 'properties': | |
| - foreach ($value as $pValue) { | |
| - $cg->setProperty((!$pValue instanceof PropertyGenerator) ?: PropertyGenerator::fromArray($pValue)); | |
| - } | |
| + $cg->setProperties($value); | |
| + //foreach ($value as $pValue) { | |
| + // $cg->setProperty(($pValue instanceof PropertyGenerator) ?: PropertyGenerator::fromArray($pValue)); | |
| + //} | |
| break; | |
| case 'methods': | |
| - foreach ($value as $mValue) { | |
| - $cg->setMethod((!$mValue instanceof MethodGenerator) ?: MethodGenerator::fromArray($mValue)); | |
| - } | |
| + $cg->setMethods($value); | |
| + //foreach ($value as $mValue) { | |
| + // $cg->setMethod((!$mValue instanceof MethodGenerator) ?: MethodGenerator::fromArray($mValue)); | |
| + //} | |
| break; | |
| } | |
| } | |
| @@ -458,19 +461,41 @@ class ClassGenerator extends AbstractGenerator | |
| public function setProperties(array $properties) | |
| { | |
| foreach ($properties as $property) { | |
| - $this->setProperty($property); | |
| + if ($property instanceof PropertyGenerator) { | |
| + $this->setProperty($property); | |
| + } else { | |
| + if (is_string($property)) { | |
| + $this->addProperty($property); | |
| + } else if (is_array($property)) { | |
| + call_user_func_array(array($this, 'addProperty'), $property); | |
| + } | |
| + } | |
| } | |
| return $this; | |
| } | |
| /** | |
| + * @see | |
| + */ | |
| + public function addProperty($name, $defaultValue = null, $flags = PropertyGenerator::FLAG_PUBLIC) | |
| + { | |
| + if (!is_string($name)) { | |
| + throw new Exception\InvalidArgumentException('setProperty() expects either a string or an instance of Zend\Code\Generator\PropertyGenerator'); | |
| + } | |
| + | |
| + $property = new PropertyGenerator($name, $defaultValue, $flags); | |
| + $this->setProperty($property); | |
| + } | |
| + | |
| + /** | |
| * setProperty() | |
| * | |
| * @param PropertyGenerator $property | |
| * @return ClassGenerator | |
| */ | |
| public function setProperty(PropertyGenerator $property) | |
| + //public function addPropertyGenerator(PropertyGenerator $property) | |
| { | |
| //if (is_string($property)) { | |
| // $property = new PropertyGenerator($property); | |
| @@ -534,18 +559,40 @@ class ClassGenerator extends AbstractGenerator | |
| public function setMethods(array $methods) | |
| { | |
| foreach ($methods as $method) { | |
| - $this->setMethod($method); | |
| + if ($method instanceof MethodGenerator) { | |
| + $this->setMethod($method); | |
| + } else { | |
| + if (is_string($method)) { | |
| + $this->addMethod($method); | |
| + } else if (is_array($method)){ | |
| + call_user_func_array(array($this, 'addMethod'), $method); | |
| + } | |
| + } | |
| } | |
| return $this; | |
| } | |
| /** | |
| + * Add Method from scalars | |
| + */ | |
| + public function addMethod($name = null, array $parameters = array(), $flags = MethodGenerator::FLAG_PUBLIC, $body = null, $docblock = null) | |
| + { | |
| + if (!is_string($name)) { | |
| + throw new Exception\InvalidArgumentException('setMethod() expects either a string method name or an instance of Zend\Code\Generator\MethodGenerator'); | |
| + } | |
| + $method = new MethodGenerator($name, $parameters, $flags, $body, $docblock); | |
| + $this->setMethod($method); | |
| + } | |
| + | |
| + | |
| + /** | |
| * setMethod() | |
| * | |
| * @param array|\MethodGenerator\Code\Generator\PhpMethod $method | |
| * @return \ClassGenerator\Code\Generator\PhpClass | |
| */ | |
| public function setMethod(MethodGenerator $method) | |
| + //public function setMethodGenerator(MethodGenerator $method) | |
| { | |
| //if (is_string($method)) { | |
| // $method = new MethodGenerator($method); | |
| diff --git library/Zend/Code/Generator/Docblock/Tag.php library/Zend/Code/Generator/Docblock/Tag.php | |
| index 54fadd6..26c34a3 100644 | |
| --- library/Zend/Code/Generator/Docblock/Tag.php | |
| +++ library/Zend/Code/Generator/Docblock/Tag.php | |
| @@ -21,7 +21,8 @@ | |
| namespace Zend\Code\Generator\Docblock; | |
| -use Zend\Code\Reflection\ReflectionDocblockTag, | |
| +use Zend\Code\Reflection\Docblock\Tag as ReflectionDocblockTag, | |
| + //Zend\Code\Reflection\ReflectionDocblockTag, | |
| Zend\Code\Generator\AbstractGenerator; | |
| /** | |
| diff --git library/Zend/Code/Generator/FileGenerator.php library/Zend/Code/Generator/FileGenerator.php | |
| index 74f964a..8ddc063 100644 | |
| --- library/Zend/Code/Generator/FileGenerator.php | |
| +++ library/Zend/Code/Generator/FileGenerator.php | |
| @@ -115,6 +115,10 @@ class FileGenerator extends AbstractGenerator | |
| /* @var $class \Zend\Code\Reflection\ReflectionClass */ | |
| $phpClass = ClassGenerator::fromReflection($class); | |
| $phpClass->setContainingFileGenerator($file); | |
| + | |
| + //var_dump($class->getMethods()); | |
| + //var_dump($phpClass->getMethods()); | |
| + | |
| $file->setClass($phpClass); | |
| $classStartLine = $class->getStartLine(true); | |
| $classEndLine = $class->getEndLine(); | |
| @@ -140,8 +144,9 @@ class FileGenerator extends AbstractGenerator | |
| } | |
| $namespace = $fileReflection->getNamespace(); | |
| + | |
| if ($namespace != '') { | |
| - $file->setNamespace($fileReflection->getNamespace()); | |
| + $file->setNamespace($namespace); | |
| } | |
| $uses = $fileReflection->getUses(); | |
| @@ -480,7 +485,13 @@ class FileGenerator extends AbstractGenerator | |
| // if there are markers, put the body into the output | |
| if (preg_match('#/\* Zend_CodeGenerator_Php_File-(.*?)Marker:#', $body)) { | |
| - $output .= $body; | |
| + $tokens = token_get_all($body); | |
| + foreach($tokens as $token) { | |
| + if (is_array($token) && in_array($token[0], array(T_OPEN_TAG, T_COMMENT, T_DOC_COMMENT, T_WHITESPACE))) { | |
| + $output .= $token[1]; | |
| + } | |
| + } | |
| + //$output .= $body; | |
| $body = ''; | |
| } | |
| @@ -537,6 +548,9 @@ class FileGenerator extends AbstractGenerator | |
| if (preg_match('#'.$regex.'#', $output)) { | |
| $output = preg_replace('#'.$regex.'#', $class->generate(), $output, 1); | |
| } else { | |
| + if ($namespace) { | |
| + $class->setNamespaceName(null); | |
| + } | |
| $output .= $class->generate() . self::LINE_FEED; | |
| } | |
| } | |
| diff --git library/Zend/Code/Reflection/FileReflection.php library/Zend/Code/Reflection/FileReflection.php | |
| index 0fdf528..bcde8b8 100644 | |
| --- library/Zend/Code/Reflection/FileReflection.php | |
| +++ library/Zend/Code/Reflection/FileReflection.php | |
| @@ -266,7 +266,8 @@ class FileReflection implements Reflection | |
| */ | |
| public function getContents() | |
| { | |
| - return $this->contents; | |
| + return file_get_contents($this->filePath); | |
| + //return $this->contents; | |
| } | |
| public function toString() | |
| diff --git library/Zend/Code/Scanner/CachingFileScanner.php library/Zend/Code/Scanner/CachingFileScanner.php | |
| index 8212b9c..8275f48 100644 | |
| --- library/Zend/Code/Scanner/CachingFileScanner.php | |
| +++ library/Zend/Code/Scanner/CachingFileScanner.php | |
| @@ -29,6 +29,11 @@ class CachingFileScanner extends FileScanner | |
| } | |
| } | |
| + public static function clearCache() | |
| + { | |
| + static::$cache = array(); | |
| + } | |
| + | |
| public function getAnnotationManager() | |
| { | |
| return $this->fileScanner->getAnnotationManager(); | |
| @@ -88,4 +93,4 @@ class CachingFileScanner extends FileScanner | |
| { | |
| return $this->fileScanner->getFunctions(); | |
| } | |
| -} | |
| \ No newline at end of file | |
| +} | |
| diff --git tests/Zend/Code/Generator/ClassGeneratorTest.php tests/Zend/Code/Generator/ClassGeneratorTest.php | |
| index 90a4035..285e85b 100644 | |
| --- tests/Zend/Code/Generator/ClassGeneratorTest.php | |
| +++ tests/Zend/Code/Generator/ClassGeneratorTest.php | |
| @@ -91,27 +91,27 @@ class ClassGeneratorTest extends \PHPUnit_Framework_TestCase | |
| $properties = $classGenerator->getProperties(); | |
| $this->assertEquals(count($properties), 2); | |
| - $this->assertInstanceOf('PropertyGenerator', current($properties)); | |
| + $this->assertInstanceOf('Zend\Code\Generator\PropertyGenerator', current($properties)); | |
| $property = $classGenerator->getProperty('propTwo'); | |
| - $this->assertInstanceOf('PropertyGenerator', $property); | |
| + $this->assertInstanceOf('Zend\Code\Generator\PropertyGenerator', $property); | |
| $this->assertEquals($property->getName(), 'propTwo'); | |
| // add a new property | |
| - $classGenerator->setProperty('prop3'); | |
| + $classGenerator->addProperty('prop3'); | |
| $this->assertEquals(count($classGenerator->getProperties()), 3); | |
| } | |
| public function testSetPropertyAlreadyExistsThrowsException() | |
| { | |
| $classGenerator = new ClassGenerator(); | |
| - $classGenerator->setProperty('prop3'); | |
| + $classGenerator->addProperty('prop3'); | |
| $this->setExpectedException( | |
| 'Zend\Code\Generator\Exception\InvalidArgumentException', | |
| 'A property by name prop3 already exists in this class' | |
| ); | |
| - $classGenerator->setProperty('prop3'); | |
| + $classGenerator->addProperty('prop3'); | |
| } | |
| public function testSetPropertyNoArrayOrPropertyThrowsException() | |
| @@ -122,7 +122,7 @@ class ClassGeneratorTest extends \PHPUnit_Framework_TestCase | |
| 'Zend\Code\Generator\Exception\InvalidArgumentException', | |
| 'setProperty() expects either a string or an instance of Zend\Code\Generator\PropertyGenerator' | |
| ); | |
| - $classGenerator->setProperty(true); | |
| + $classGenerator->addProperty(true); | |
| } | |
| public function testMethodAccessors() | |
| @@ -142,7 +142,7 @@ class ClassGeneratorTest extends \PHPUnit_Framework_TestCase | |
| $this->assertEquals($method->getName(), 'methodOne'); | |
| // add a new property | |
| - $classGenerator->setMethod('methodThree'); | |
| + $classGenerator->addMethod('methodThree'); | |
| $this->assertEquals(count($classGenerator->getMethods()), 3); | |
| } | |
| @@ -155,7 +155,7 @@ class ClassGeneratorTest extends \PHPUnit_Framework_TestCase | |
| 'setMethod() expects either a string method name or an instance of Zend\Code\Generator\MethodGenerator' | |
| ); | |
| - $classGenerator->setMethod(true); | |
| + $classGenerator->addMethod(true); | |
| } | |
| public function testSetMethodNameAlreadyExistsThrowsException() | |
| @@ -220,6 +220,24 @@ class ClassGeneratorTest extends \PHPUnit_Framework_TestCase | |
| ), | |
| )); | |
| + | |
| + $classGenerator = ClassGenerator::fromArray( | |
| + array( | |
| + 'name' => 'SampleClass', | |
| + //'abstract' => true, | |
| + 'flags' => ClassGenerator::FLAG_ABSTRACT, | |
| + 'name' => 'SampleClass', | |
| + 'extendedClass' => 'ExtendedClassName', | |
| + 'implementedInterfaces' => array('Iterator', 'Traversable'), | |
| + 'properties' => array('foo', | |
| + array('name' => 'bar') | |
| + ), | |
| + 'methods' => array( | |
| + array('name' => 'baz') | |
| + ), | |
| + )); | |
| + | |
| + | |
| $expectedOutput = <<<EOS | |
| abstract class SampleClass extends ExtendedClassName implements Iterator, Traversable | |
| { | |
| diff --git tests/Zend/Code/Generator/FileGeneratorTest.php tests/Zend/Code/Generator/FileGeneratorTest.php | |
| index b67e717..41dcdc2 100644 | |
| --- tests/Zend/Code/Generator/FileGeneratorTest.php | |
| +++ tests/Zend/Code/Generator/FileGeneratorTest.php | |
| @@ -20,7 +20,8 @@ | |
| */ | |
| namespace ZendTest\Code\Generator; | |
| -use Zend\Code\Generator\FileGenerator, | |
| +use Zend\Code\Generator\ClassGenerator, | |
| + Zend\Code\Generator\FileGenerator, | |
| Zend\Code\Reflection\FileReflection; | |
| /** | |
| @@ -59,10 +60,10 @@ class FileGeneratorTest extends \PHPUnit_Framework_TestCase | |
| public function testToString() | |
| { | |
| - $codeGenFile = new FileGenerator(array( | |
| + $codeGenFile = FileGenerator::fromArray(array( | |
| 'requiredFiles' => array('SampleClass.php'), | |
| 'class' => array( | |
| - 'abstract' => true, | |
| + 'flags' => ClassGenerator::FLAG_ABSTRACT, | |
| 'name' => 'SampleClass', | |
| 'extendedClass' => 'ExtendedClassName', | |
| 'implementedInterfaces' => array('Iterator', 'Traversable') | |
| @@ -92,7 +93,7 @@ EOS; | |
| { | |
| $tempFile = tempnam(sys_get_temp_dir(), 'UnitFile'); | |
| - $codeGenFile = new FileGenerator(array( | |
| + $codeGenFile = FileGenerator::fromArray(array( | |
| 'class' => array( | |
| 'name' => 'SampleClass' | |
| ) | |
| @@ -117,9 +118,10 @@ EOS; | |
| $file = __DIR__ . '/TestAsset/TestSampleSingleClass.php'; | |
| require_once $file; | |
| - $codeGenFileFromDisk = FileGenerator::fromReflection(new FileReflection($file)); | |
| + $codeGenFileFromDisk = FileGenerator::fromReflection($fileRefl = new FileReflection($file)); | |
| + //var_dump('**', $fileRefl->getClass('ZendTest\Code\Generator\TestAsset\TestSampleSingleClass')); | |
| - $codeGenFileFromDisk->getClass()->setMethod(array('name' => 'foobar')); | |
| + $codeGenFileFromDisk->getClass()->addMethod('foobar'); | |
| $expectedOutput = <<<EOS | |
| <?php | |
| @@ -127,25 +129,29 @@ EOS; | |
| * File header here | |
| * | |
| * @author Ralph Schindler <ralph.schindler@zend.com> | |
| - * | |
| */ | |
| + | |
| + | |
| +/* Zend_CodeGenerator_Php_File-ClassMarker: {ZendTest\Code\Generator\TestAsset\TestSampleSingleClass} */ | |
| + | |
| + | |
| namespace ZendTest\Code\Generator\TestAsset; | |
| /** | |
| * class docblock | |
| - * | |
| + * | |
| * @package Zend_Reflection_TestSampleSingleClass | |
| - * | |
| + * | |
| */ | |
| class TestSampleSingleClass | |
| { | |
| /** | |
| * Enter description here... | |
| - * | |
| + * | |
| * @return bool | |
| - * | |
| + * | |
| */ | |
| public function someMethod() | |
| { | |
| @@ -161,12 +167,14 @@ class TestSampleSingleClass | |
| EOS; | |
| + //echo $codeGenFileFromDisk->generate();die; | |
| $this->assertEquals($expectedOutput, $codeGenFileFromDisk->generate()); | |
| } | |
| public function testFileLineEndingsAreAlwaysLineFeed() | |
| { | |
| - $codeGenFile = new FileGenerator(array( | |
| + //$codeGenFile = new FileGenerator(array( | |
| + $codeGenFile = FileGenerator::fromArray(array( | |
| 'requiredFiles' => array('SampleClass.php'), | |
| 'class' => array( | |
| 'abstract' => true, | |
| @@ -177,7 +185,7 @@ EOS; | |
| )); | |
| // explode by newline, this would leave CF in place if it were generated | |
| - $lines = explode("\n", $codeGenFile); | |
| + $lines = explode("\n", $codeGenFile->generate()); | |
| $targetLength = strlen('require_once \'SampleClass.php\';'); | |
| $this->assertEquals($targetLength, strlen($lines[2])); | |
| diff --git tests/Zend/Code/Scanner/CachingFileScannerTest.php tests/Zend/Code/Scanner/CachingFileScannerTest.php | |
| index 298d449..877c851 100644 | |
| --- tests/Zend/Code/Scanner/CachingFileScannerTest.php | |
| +++ tests/Zend/Code/Scanner/CachingFileScannerTest.php | |
| @@ -9,6 +9,8 @@ class CachingFileScannerTest extends \PHPUnit_Framework_TestCase | |
| { | |
| public function testCachingFileScannerWillUseSameInternalFileScannerWithMatchingFileNameAnAnnotationManagerObject() | |
| { | |
| + CachingFileScanner::clearCache(); | |
| + | |
| // single entry, based on file | |
| $cfs1 = new CachingFileScanner(__DIR__ . '/../TestAsset/BarClass.php'); | |
| $this->assertContains('ZendTest\Code\TestAsset\BarClass', $cfs1->getClassNames()); | |
| @@ -68,4 +70,4 @@ class CachingFileScannerTest extends \PHPUnit_Framework_TestCase | |
| $this->assertNotSame($fileScannerPropOne->getValue($one), $fileScannerPropTwo->getValue($two)); | |
| } | |
| -} | |
| \ No newline at end of file | |
| +} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment