Transphpile\Transpile\Transpile::getTraverser PHP Метод

getTraverser() публичный статический Метод

public static getTraverser ( ) : NodeTraverser
Результат PhpParser\NodeTraverser
    public static function getTraverser()
    {
        $traverser = new StackVarNodeTraverser();
        // Find Path
        $reflector = new \Reflectionclass(__CLASS__);
        $path = $reflector->getFileName();
        $path = dirname($path);
        // Generate base FQCN and path based on the current file
        $baseFqcn = __CLASS__;
        $baseFqcn = explode('\\', $baseFqcn);
        $baseFqcn[count($baseFqcn) - 1] = 'Visitors';
        $baseFqcn = implode('\\', $baseFqcn);
        $baseFqcnPath = dirname(__FILE__) . "/Visitors/";
        // Iterate node visitors and add them to the traverser
        $it = new \RecursiveDirectoryIterator($path . '/Visitors', \RecursiveDirectoryIterator::SKIP_DOTS);
        $it = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::LEAVES_ONLY);
        foreach ($it as $fileInfo) {
            /* @var \SplFileInfo $fileInfo */
            // Convert path into FQCN
            $class = $fileInfo->getPathname();
            $class = str_replace($baseFqcnPath, "", $class);
            $class = str_replace('.php', '', $class);
            $class = str_replace('/', '\\', $class);
            $fqcn = $baseFqcn . '\\' . $class;
            $traverser->addVisitor(new $fqcn());
        }
        return $traverser;
    }

Usage Example

Пример #1
0
 /**
  * Add final anonymous classes to the statements
  *
  * @param array $nodes
  * @return array
  */
 public function afterTraverse(array $nodes)
 {
     // Nothing to do when there are no anonymous classes
     if (NodeStateStack::getInstance()->count('anonClasses') == 0) {
         return $nodes;
     }
     // We must transpile anonymous classes first, as we haven't done this yet
     $traverser = Transpile::getTraverser();
     $anonClassStmts = $traverser->traverse(NodeStateStack::getInstance()->get('anonClasses'));
     // Find hook point for anonymous classes, which must be after declare, namespaces and use-statements, and can
     // before anything else. This might cause issues when anonymous classes implement interfaces that are defined
     // later on.
     $idx = $this->getAnonymousClassHookIndex($nodes);
     // Array_merge the anonymous class statements on the correct position
     $preStmts = array_slice($nodes, 0, $idx);
     $postStmts = array_slice($nodes, $idx);
     $nodes = array_merge($preStmts, $anonClassStmts, $postStmts);
     return $nodes;
 }