PHPSA\Definition\FileParser::parserFile PHP Method

parserFile() public method

public parserFile ( string $filepath, Context $context )
$filepath string
$context PHPSA\Context
    public function parserFile($filepath, Context $context)
    {
        $context->setFilepath($filepath);
        try {
            if (!is_readable($filepath)) {
                throw new RuntimeException('File ' . $filepath . ' is not readable');
            }
            $context->debug('<comment>Precompile: ' . $filepath . '.</comment>');
            $code = file_get_contents($filepath);
            $astTree = $this->parser->parse($code);
            $this->nodeTraverser->traverse($astTree);
            $context->aliasManager = new AliasManager();
            $namespace = null;
            /**
             * Step 1 Precompile
             */
            foreach ($astTree as $topStatement) {
                if ($topStatement instanceof Node\Stmt\Namespace_) {
                    /**
                     * Namespace block can be created without NS name
                     */
                    if ($topStatement->name) {
                        $namespace = $topStatement->name->toString();
                        $context->aliasManager->setNamespace($namespace);
                    }
                    if ($topStatement->stmts) {
                        $this->parseTopDefinitions($topStatement->stmts, $context->aliasManager, $filepath);
                    }
                } else {
                    if (is_array($topStatement)) {
                        $this->parseTopDefinitions($topStatement, $context->aliasManager, $filepath);
                    } else {
                        $this->parseTopDefinitions($astTree, $context->aliasManager, $filepath);
                    }
                }
            }
            $context->clear();
        } catch (\PhpParser\Error $e) {
            $context->syntaxError($e, $filepath);
        } catch (Exception $e) {
            $context->output->writeln("<error>{$e->getMessage()}</error>");
        }
    }

Usage Example

Example #1
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('');
     if (extension_loaded('xdebug')) {
         /**
          * This will disable only showing stack traces on error conditions.
          */
         if (function_exists('xdebug_disable')) {
             xdebug_disable();
         }
         $output->writeln('<error>It is highly recommended to disable the XDebug extension before invoking this command.</error>');
     }
     $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new \PhpParser\Lexer\Emulative(['usedAttributes' => ['comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos']]));
     /** @var Application $application */
     $application = $this->getApplication();
     $application->compiler = new Compiler();
     $em = EventManager::getInstance();
     $context = new Context($output, $application, $em);
     $fileParser = new FileParser($parser, $this->getCompiler());
     $path = $input->getArgument('path');
     if (is_dir($path)) {
         $directoryIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS));
         $output->writeln('Scanning directory <info>' . $path . '</info>');
         $count = 0;
         /** @var SplFileInfo $file */
         foreach ($directoryIterator as $file) {
             if ($file->getExtension() !== 'php') {
                 continue;
             }
             $context->debug($file->getPathname());
             $count++;
         }
         $output->writeln("Found <info>{$count} files</info>");
         if ($count > 100) {
             $output->writeln('<comment>Caution: You are trying to scan a lot of files; this might be slow. For bigger libraries, consider setting up a dedicated platform or using ci.lowl.io.</comment>');
         }
         $output->writeln('');
         /** @var SplFileInfo $file */
         foreach ($directoryIterator as $file) {
             if ($file->getExtension() !== 'php') {
                 continue;
             }
             $fileParser->parserFile($file->getPathname(), $context);
         }
     } elseif (is_file($path)) {
         $fileParser->parserFile($path, $context);
     }
     /**
      * Step 2 Recursive check ...
      */
     $application->compiler->compile($context);
 }
All Usage Examples Of PHPSA\Definition\FileParser::parserFile