/** * fromReflection() * * @param \Zend\Reflection\ReflectionFile $reflectionFile * @return \Zend\CodeGenerator\Php\PhpFile */ public static function fromReflection(Reflection\ReflectionFile $reflectionFile) { $file = new self(); $file->setSourceContent($reflectionFile->getContents()); $file->setSourceDirty(false); $body = $reflectionFile->getContents(); // @todo this whole area needs to be reworked with respect to how body lines are processed foreach ($reflectionFile->getClasses() as $class) { $phpClass = PhpClass::fromReflection($class); $phpClass->setPhpFile($file); $file->setClass($phpClass); $classStartLine = $class->getStartLine(true); $classEndLine = $class->getEndLine(); $bodyLines = explode("\n", $body); $bodyReturn = array(); for ($lineNum = 1; $lineNum <= count($bodyLines); $lineNum++) { if ($lineNum == $classStartLine) { $bodyReturn[] = str_replace('?', $class->getName(), self::$_markerClass); //'/* Zend_CodeGenerator_Php_File-ClassMarker: {' . $class->getName() . '} */'; $lineNum = $classEndLine; } else { $bodyReturn[] = $bodyLines[$lineNum - 1]; // adjust for index -> line conversion } } $body = implode("\n", $bodyReturn); unset($bodyLines, $bodyReturn, $classStartLine, $classEndLine); } $namespace = $reflectionFile->getNamespace(); if ($namespace != '') { $file->setNamespace($reflectionFile->getNamespace()); } $uses = $reflectionFile->getUses(); if ($uses) { $file->setUses($uses); } if ($reflectionFile->getDocComment() != '') { $docblock = $reflectionFile->getDocblock(); $file->setDocblock(PhpDocblock::fromReflection($docblock)); $bodyLines = explode("\n", $body); $bodyReturn = array(); for ($lineNum = 1; $lineNum <= count($bodyLines); $lineNum++) { if ($lineNum == $docblock->getStartLine()) { $bodyReturn[] = str_replace('?', $class->getName(), self::$_markerDocblock); //'/* Zend_CodeGenerator_Php_File-ClassMarker: {' . $class->getName() . '} */'; $lineNum = $docblock->getEndLine(); } else { $bodyReturn[] = $bodyLines[$lineNum - 1]; // adjust for index -> line conversion } } $body = implode("\n", $bodyReturn); unset($bodyLines, $bodyReturn, $classStartLine, $classEndLine); } $file->setBody($body); return $file; }
public function testFileCanReflectFileWithUses() { $fileToReflect = __DIR__ . '/TestAsset/TestSampleClass8.php'; include_once $fileToReflect; $reflectionFile = new Reflection\ReflectionFile($fileToReflect); $expected = array(array('namespace' => 'Zend\\Config', 'as' => 'ZendConfig', 'asResolved' => 'ZendConfig'), array('namespace' => 'FooBar\\Foo\\Bar', 'as' => '', 'asResolved' => 'Bar'), array('namespace' => 'One\\Two\\Three\\Four\\Five', 'as' => 'ottff', 'asResolved' => 'ottff')); $this->assertSame($expected, $reflectionFile->getUses()); }