/** * @inheritDoc */ public function __invoke($fcqn, $namespace, $commandName) { $commandName = ucfirst($commandName); $reflectionClass = new ClassReflection($fcqn); $fileGenerator = FileGenerator::fromReflectedFileName($reflectionClass->getFileName()); $fileGenerator->setFilename($reflectionClass->getFileName()); $fileGenerator->setUse(ltrim($namespace, '\\') . '\\' . $commandName); $classGenerator = $fileGenerator->getClass(); // workaround for import namespace if ($classToExtend = $classGenerator->getExtendedClass()) { $classGenerator->setExtendedClass(substr($classToExtend, strrpos($classToExtend, '\\') + 1)); } $classGenerator->addMethodFromGenerator($this->methodWhenEvent($commandName)); return $fileGenerator; }
/** * 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(); }
/** * Generate code to cache from class reflection. * This is a total mess, I know. Just wanted to flesh out the logic. * * @todo Refactor into a class, clean up logic, DRY it up, maybe move * some of this into Zend\Code * @param ClassReflection $r * @return string */ protected static function getCacheCode(ClassReflection $r) { $useString = ''; $usesNames = array(); if (count($uses = $r->getDeclaringFile()->getUses())) { foreach ($uses as $use) { $usesNames[$use['use']] = $use['as']; $useString .= "use {$use['use']}"; if ($use['as']) { $useString .= " as {$use['as']}"; } $useString .= ";\n"; } } $declaration = ''; if ($r->isAbstract() && !$r->isInterface()) { $declaration .= 'abstract '; } if ($r->isFinal()) { $declaration .= 'final '; } if ($r->isInterface()) { $declaration .= 'interface '; } if (!$r->isInterface()) { $declaration .= 'class '; } $declaration .= $r->getShortName(); $parentName = false; if (($parent = $r->getParentClass()) && $r->getNamespaceName()) { $parentName = array_key_exists($parent->getName(), $usesNames) ? $usesNames[$parent->getName()] ?: $parent->getShortName() : (0 === strpos($parent->getName(), $r->getNamespaceName()) ? substr($parent->getName(), strlen($r->getNamespaceName()) + 1) : '\\' . $parent->getName()); } else { if ($parent && !$r->getNamespaceName()) { $parentName = '\\' . $parent->getName(); } } if ($parentName) { $declaration .= " extends {$parentName}"; } $interfaces = array_diff($r->getInterfaceNames(), $parent ? $parent->getInterfaceNames() : array()); if (count($interfaces)) { foreach ($interfaces as $interface) { $iReflection = new ClassReflection($interface); $interfaces = array_diff($interfaces, $iReflection->getInterfaceNames()); } $declaration .= $r->isInterface() ? ' extends ' : ' implements '; $declaration .= implode(', ', array_map(function ($interface) use($usesNames, $r) { $iReflection = new ClassReflection($interface); return array_key_exists($iReflection->getName(), $usesNames) ? $usesNames[$iReflection->getName()] ?: $iReflection->getShortName() : (0 === strpos($iReflection->getName(), $r->getNamespaceName()) ? substr($iReflection->getName(), strlen($r->getNamespaceName()) + 1) : '\\' . $iReflection->getName()); }, $interfaces)); } $classContents = $r->getContents(false); $classFileDir = dirname($r->getFileName()); $classContents = trim(str_replace('__DIR__', sprintf("'%s'", $classFileDir), $classContents)); $return = "\nnamespace " . $r->getNamespaceName() . " {\n" . $useString . $declaration . "\n" . $classContents . "\n}\n"; return $return; }
/** * Build the content of the class * It replace the __DIR__ constant with the class directoryname * * @param Reflection\ClassReflection $class * @return string */ protected function buildContent(Reflection\ClassReflection $class) { $code = $class->getContents(false); $classDir = dirname($class->getFileName()); $code = str_replace('__DIR__', "'{$classDir}'", $code); return $code; }
/** * @param ClassReflection $class * @return bool */ public function isSatisfiedBy(ClassReflection $class) { return static::hasDIR($class->getFileName()); }
/** * Builds and populates Action object based on the provided class name * * @param string $className * @return Action */ public function buildAction($className) { $classReflection = new ClassReflection($className); $scanner = new FileScanner($classReflection->getFileName(), $this->annotationManager); $classScanner = $scanner->getClass($classReflection->getName()); $action = new Action($classScanner->getName()); foreach ($classScanner->getMethods() as $classMethod) { if ($classMethod->isPublic() && $classMethod->getName() != '__construct') { $action->addMethod($this->buildMethod($classMethod)); } } return $action; }
protected function findImports(ClassReflection $reflection) { $matches = array(); preg_match_all('/^use ([a-zA-Z0-9\\\\]*)\\;$/im', file_get_contents($reflection->getFileName()), $matches); return $matches[1]; }
/** * @return array */ protected function setClientMethodDocblocks() { $client = new \SoapClient(__DIR__ . '/../../Resources/wsdl/services.wsdl'); $functions = $client->__getFunctions(); sort($functions); $functions = array_map(function ($function) { return preg_replace("~^[a-z]+\\s([a-z]+) ?\\(.+\\)\$~i", "\$1", $function); }, $functions); $exchangeWebServicesReflection = new ClassReflection(ExchangeWebServices::class); $fileGen = Generator\FileGenerator::fromReflectedFileName($exchangeWebServicesReflection->getFileName()); $fileGen->setFilename($exchangeWebServicesReflection->getFileName()); $exchangeWebServicesClass = Generator\ClassGenerator::fromReflection($exchangeWebServicesReflection); $docblock = $exchangeWebServicesClass->getDocBlock(); $reflection = new \ReflectionClass($docblock); $property = $reflection->getProperty('tags'); $property->setAccessible(true); $property->setValue($docblock, []); $docblock->setWordWrap(false); $tags = []; $tags[] = new Generator\DocBlock\Tag\GenericTag('@package php-ews\\Client'); $tags[] = new EmptyDocblockTag(); foreach ($functions as $function) { $tag = new MethodWIthRequestTag($function, ['Type']); $tags[] = $tag; } $docblock->setTags($tags); $exchangeWebServicesClass->getDocBlock()->setSourceDirty(true); $fileGen->setClass($exchangeWebServicesClass); $fileGen->write(); }
/** * @covers SpeedLoader\BuildClass::compressCode */ public function testCompressCodeHigh() { $classRef = new ClassReflection('SpeedLoaderTestAsset\\Simple\\ClassToCompress'); $buildClass = new BuildClass(); $buildClass->setCompressionLevel(BuildClass::COMPRESS_HIGH); $content = str_replace('<?php' . "\n", '', file_get_contents($classRef->getFileName())); $result = $this->invokeMethod($buildClass, 'compressCode', [$content]); $expected = php_strip_whitespace($classRef->getFileName()); $expected = str_replace('<?php' . "\n", '', $expected); $this->assertEquals($expected, $result); }