ClassPreloader\ClassPreloader::getCode PHP Метод

getCode() публичный Метод

Get a pretty printed string of code from a file while applying visitors.
public getCode ( string $file, $comments = true ) : string
$file string
Результат string
    public function getCode($file, $comments = true)
    {
        if (!is_string($file) || empty($file)) {
            throw new RuntimeException('Invalid filename provided.');
        }
        if (!is_readable($file)) {
            throw new RuntimeException("Cannot open {$file} for reading.");
        }
        if ($comments) {
            $content = file_get_contents($file);
        } else {
            $content = php_strip_whitespace($file);
        }
        $parsed = $this->parser->parse($content);
        $stmts = $this->traverser->traverseFile($parsed, $file);
        $pretty = $this->printer->prettyPrint($stmts);
        if (substr($pretty, 30) === '<?php declare(strict_types=1);' || substr($pretty, 30) === "<?php\ndeclare(strict_types=1);") {
            $pretty = substr($pretty, 32);
        } elseif (substr($pretty, 31) === "<?php\r\ndeclare(strict_types=1);") {
            $pretty = substr($pretty, 33);
        } elseif (substr($pretty, 5) === '<?php') {
            $pretty = substr($pretty, 7);
        }
        return $this->getCodeWrappedIntoNamespace($parsed, $pretty);
    }

Usage Example

Пример #1
0
 /**
  * Executes the pre-compile command.
  *
  * @param \Symfony\Component\Console\Input\InputInterface   $input
  * @param \Symfony\Component\Console\Output\OutputInterface $output
  *
  * @return int|null
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->validateCommand($input);
     $output->writeln('> Loading configuration file');
     $config = $input->getOption('config');
     $files = (new ConfigResolver())->getFileList($config);
     $output->writeLn('- Found ' . count($files) . ' files');
     $preloader = new ClassPreloader(new PrettyPrinter(), new Parser(new Lexer()), $this->getTraverser($input));
     $outputFile = $input->getOption('output');
     $handle = $preloader->prepareOutput($outputFile);
     $output->writeln('> Compiling classes');
     $count = 0;
     $countSkipped = 0;
     $comments = !$input->getOption('strip_comments');
     foreach ($files as $file) {
         $count++;
         try {
             $code = $preloader->getCode($file, $comments);
             $output->writeln('- Writing ' . $file);
             fwrite($handle, $code . "\n");
         } catch (SkipFileException $ex) {
             $countSkipped++;
             $output->writeln('- Skipping ' . $file);
         }
     }
     fclose($handle);
     $output->writeln("> Compiled loader written to {$outputFile}");
     $output->writeln('- Files: ' . ($count - $countSkipped) . '/' . $count . ' (skipped: ' . $countSkipped . ')');
     $output->writeln('- Filesize: ' . round(filesize($outputFile) / 1024) . ' kb');
 }
All Usage Examples Of ClassPreloader\ClassPreloader::getCode