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

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

Prepare the output file and directory.
public prepareOutput ( string $output, boolean $strict = false ) : resource
$output string
$strict boolean
Результат resource
    public function prepareOutput($output, $strict = false)
    {
        if ($strict && version_compare(PHP_VERSION, '7') < 1) {
            throw new RuntimeException('Strict mode requires PHP 7 or greater.');
        }
        $dir = dirname($output);
        if (!is_dir($dir) && !mkdir($dir, 0777, true)) {
            throw new RuntimeException("Unable to create directory {$dir}.");
        }
        $handle = fopen($output, 'w');
        if (!$handle) {
            throw new RuntimeException("Unable to open {$output} for writing.");
        }
        if ($strict) {
            fwrite($handle, "<?php declare(strict_types=1);\n");
        } else {
            fwrite($handle, "<?php\n");
        }
        return $handle;
    }

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::prepareOutput