Beispiel #1
0
 /**
  * @param ClassReflection $class
  * @return array
  */
 private function getUseArray(ClassReflection $class)
 {
     $usesNames = [];
     foreach ($class->getDeclaringFile()->getUses() as $use) {
         $usesNames[$use['use']] = $use['as'];
     }
     return $usesNames;
 }
Beispiel #2
0
 /**
  * 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;
 }
 /**
  * Generate code to cache from class reflection.
  *
  * @todo maybe move some of this into Zend\Code
  *
  * @param  ClassReflection $classReflection
  * @return string
  */
 public function getCacheCode(ClassReflection $classReflection)
 {
     $useString = $this->fileReflectionService->getUseString($classReflection->getDeclaringFile());
     $declaration = $this->classDeclarationService->getClassDeclaration($classReflection);
     $classContents = $classReflection->getContents(false);
     $classFileDir = dirname($classReflection->getFileName());
     $classContents = trim(str_replace('__DIR__', sprintf("'%s'", $classFileDir), $classContents));
     $return = "\nnamespace " . $classReflection->getNamespaceName() . " {\n" . $useString . $declaration . "\n" . $classContents . "\n}\n";
     return $return;
 }
 private function getUseStringAndNames(ClassReflection $classReflection)
 {
     $useString = '';
     $usesNames = array();
     if (count($uses = $classReflection->getDeclaringFile()->getUses())) {
         foreach ($uses as $use) {
             $usesNames[$use['use']] = $use['as'];
             $useString .= "use {$use['use']}";
             if ($use['as']) {
                 $useString .= " as {$use['as']}";
             }
             $useString .= ";\n";
         }
     }
     return array($useString, $usesNames);
 }
 public function getClassUseName(ClassReflection $currentClass, ClassReflection $useClass)
 {
     $useNames = $this->fileReflectionUseStatementService->getUseNames($currentClass->getDeclaringFile());
     $fullUseClassName = $useClass->getName();
     $classUseName = null;
     if (array_key_exists($fullUseClassName, $useNames)) {
         $classUseName = $useNames[$fullUseClassName] ?: $useClass->getShortName();
     } else {
         if (0 === strpos($fullUseClassName, $currentClass->getNamespaceName())) {
             $classUseName = substr($fullUseClassName, strlen($currentClass->getNamespaceName()) + 1);
         } else {
             $classUseName = '\\' . $fullUseClassName;
         }
     }
     return $classUseName;
 }
Beispiel #6
0
 /**
  * Create namespace code
  *
  * @param string $namespace
  * @param Reflection\ClassReflection $class
  * @return string the namespace + class + uses
  * @todo Detect defined constants
  */
 protected function buildNamespace(Reflection\ClassReflection $class)
 {
     $code = "namespace {$class->getNamespaceName()} {" . PHP_EOL;
     $uses = array();
     // Reformat uses
     foreach ($class->getDeclaringFile()->getUses() as $use) {
         $uses[$use['use']] = $use['as'];
     }
     //Just sort the uses
     ksort($uses);
     // Create block by block
     $code .= $this->buildUses($uses);
     // @TODO $this->buildConstants($class);
     $code .= $this->buildClassDecleration($class);
     $code .= $this->buildExtend($uses, $class);
     $code .= $this->buildInterface($uses, $class);
     $code .= PHP_EOL . $this->buildContent($class) . PHP_EOL;
     $code .= "}" . PHP_EOL . PHP_EOL;
     // Clear reflection memory
     \Zend\Code\Scanner\CachingFileScanner::clearCache();
     return fwrite($this->handle, $code);
 }
 public function testGetDeclaringFileReturnsFilename()
 {
     $reflectionClass = new ClassReflection('ZendTest\\Code\\Reflection\\TestAsset\\TestSampleClass2');
     $this->assertContains('TestSampleClass2.php', $reflectionClass->getDeclaringFile()->getFileName());
 }