/** * Returns class' code inside namespace * * @param ClassReflection $class */ private function dumpClass(ClassReflection $class) { if (array_search($class->getName(), $this->cachedClasses) !== false) { return; } if ($class->isInternal()) { return; } if ($class->getParentClass()) { $this->dumpClass($class->getParentClass()); } foreach ($class->getInterfaces() as $interface) { $this->dumpClass($interface); } if ($class->getTraits()) { foreach ($class->getTraits() as $trait) { $this->dumpClass($trait); } } $classContents = $class->getContents(false); $classFileDir = dirname($class->getFileName()); $classContents = trim(str_replace('__DIR__', sprintf("'%s'", $classFileDir), $classContents)); $uses = implode("\n", $this->codeGenerator->getUseLines($class)); $this->cache[] = "namespace " . $class->getNamespaceName() . " {\n" . ($uses ? $uses . "\n" : '') . $this->codeGenerator->getDeclarationLine($class) . "\n" . $classContents . "\n}\n"; $this->cachedClasses[] = $class->getName(); }
public function testInterfaceReturn() { $reflectionClass = new ClassReflection('ZendTest\\Code\\Reflection\\TestAsset\\TestSampleClass4'); $interfaces = $reflectionClass->getInterfaces(); $this->assertEquals(1, count($interfaces)); $interface = array_shift($interfaces); $this->assertEquals('ZendTest\\Code\\Reflection\\TestAsset\\TestSampleClassInterface', $interface->getName()); }
/** * Build a Code Generation Php Object from a Class Reflection * * @param ClassReflection $classReflection * @return ClassGenerator */ public static function fromReflection(ClassReflection $classReflection) { // class generator $cg = new static($classReflection->getName()); $cg->setSourceContent($cg->getSourceContent()); $cg->setSourceDirty(false); if ($classReflection->getDocComment() != '') { $cg->setDocBlock(DocBlockGenerator::fromReflection($classReflection->getDocBlock())); } $cg->setAbstract($classReflection->isAbstract()); // set the namespace if ($classReflection->inNamespace()) { $cg->setNamespaceName($classReflection->getNamespaceName()); } /* @var \Zend\Code\Reflection\ClassReflection $parentClass */ $parentClass = $classReflection->getParentClass(); if ($parentClass) { $cg->setExtendedClass($parentClass->getName()); $interfaces = array_diff($classReflection->getInterfaces(), $parentClass->getInterfaces()); } else { $interfaces = $classReflection->getInterfaces(); } $interfaceNames = array(); foreach ($interfaces as $interface) { /* @var \Zend\Code\Reflection\ClassReflection $interface */ $interfaceNames[] = $interface->getName(); } $cg->setImplementedInterfaces($interfaceNames); $properties = array(); foreach ($classReflection->getProperties() as $reflectionProperty) { if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) { $properties[] = PropertyGenerator::fromReflection($reflectionProperty); } } $cg->addProperties($properties); $methods = array(); foreach ($classReflection->getMethods() as $reflectionMethod) { $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . "\\" . $cg->getName() : $cg->getName(); if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $methods[] = MethodGenerator::fromReflection($reflectionMethod); } } $cg->addMethods($methods); return $cg; }
/** * Build the interface part * * @param array $uses * @param Reflection\ClassReflection $class * @return string */ protected function buildInterface(&$uses, Reflection\ClassReflection $class) { $code = ''; $interfaces = $class->getInterfaces(); $parentClass = $class->getParentClass(); // Normalize interfaces array to string foreach ($interfaces as &$interface) { $interfaceReflections[$interface->getName()] = $interface; $interface = $interface->getName(); } // Remove interface from parent class if ($parentClass !== false) { $parentInterfaces = $parentClass->getInterfaces(); foreach ($parentInterfaces as &$parentInterface) { $interfaceReflections[$parentInterface->getName()] = $parentInterface; $parentInterface = $parentInterface->getName(); } $interfaces = array_diff($interfaces, $parentInterfaces); } // No interfaces found? Return '' if (count($interfaces) === 0) { return $code; } // Create extend/implement keyword $code .= $class->isInterface() ? ' extends ' : ' implements '; $classNamespace = $class->getNamespaceName(); // Retrieve interfaces from the interfaces foreach ($interfaces as &$interface) { $parentInterfaces = $interfaceReflections[$interface]->getInterfaces(); foreach ($parentInterfaces as &$parentInterface) { $interfaceReflections[$parentInterface->getName()] = $parentInterface; $parentInterface = $parentInterface->getName(); } // Remove already implemented interfaces $interfaces = array_diff($interfaces, $parentInterfaces); } // define interface names foreach ($interfaces as &$interface) { // Make sure the class has already been cached $this->processClassIntoCacheFile($interfaceReflections[$interface]); // Check if the interface has been defined in the uses if (array_key_exists($interface, $uses)) { // Set the use alias or the shortname when no alias has been set $interface = empty($uses[$interface]) ? $interfaceReflections[$interface]->getShortName() : $uses[$interface]; } else { // Check if we're implementing from a subnamespace $inNamespace = strpos($interface, $classNamespace) === 0; if ($inNamespace) { $interface = substr($interface, strlen($classNamespace) + 1); } else { $interface = "\\{$interface}"; } } } $code .= implode(', ', $interfaces); return $code; }
/** * * @param ClassReflection $class * @return ClassReflection[] */ private function getInterfaces(ClassReflection $class) { $classes = []; foreach ($class->getInterfaces() as $interface) { $classes = array_merge($classes, $this->getInterfaces($interface)); } if ($class->isUserDefined() && $class->isInterface() && !in_array($class->getName(), $this->seen)) { $this->seen[] = $class->getName(); $classes[] = $class; } return $classes; }
/** * Copied from ClassGenerator::fromReflection and tweaked slightly * @param ClassReflection $classReflection * * @return ClassGenerator */ public function getGeneratorFromReflection(ClassReflection $classReflection) { // class generator $cg = new ClassGenerator($classReflection->getName()); $cg->setSourceContent($cg->getSourceContent()); $cg->setSourceDirty(false); if ($classReflection->getDocComment() != '') { $docblock = DocBlockGenerator::fromReflection($classReflection->getDocBlock()); $docblock->setIndentation(Generator::$indentation); $cg->setDocBlock($docblock); } $cg->setAbstract($classReflection->isAbstract()); // set the namespace if ($classReflection->inNamespace()) { $cg->setNamespaceName($classReflection->getNamespaceName()); } /* @var \Zend\Code\Reflection\ClassReflection $parentClass */ $parentClass = $classReflection->getParentClass(); if ($parentClass) { $cg->setExtendedClass('\\' . ltrim($parentClass->getName(), '\\')); $interfaces = array_diff($classReflection->getInterfaces(), $parentClass->getInterfaces()); } else { $interfaces = $classReflection->getInterfaces(); } $interfaceNames = array(); foreach ($interfaces as $interface) { /* @var \Zend\Code\Reflection\ClassReflection $interface */ $interfaceNames[] = $interface->getName(); } $cg->setImplementedInterfaces($interfaceNames); $properties = array(); foreach ($classReflection->getProperties() as $reflectionProperty) { if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) { $property = PropertyGenerator::fromReflection($reflectionProperty); $property->setIndentation(Generator::$indentation); $properties[] = $property; } } $cg->addProperties($properties); $methods = array(); foreach ($classReflection->getMethods() as $reflectionMethod) { $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . "\\" . $cg->getName() : $cg->getName(); if ($reflectionMethod->getDeclaringClass()->getName() == $className) { $method = MethodGenerator::fromReflection($reflectionMethod); $method->setBody(preg_replace("/^\\s+/m", '', $method->getBody())); $method->setIndentation(Generator::$indentation); $methods[] = $method; } } $cg->addMethods($methods); return $cg; }