[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/JavaScript/Minifiers/ -> ClosureCompilerApplication.php (source)

   1  <?php
   2  
   3  /*
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2019 The s9e Authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\JavaScript\Minifiers;
   9  use RuntimeException;
  10  use s9e\TextFormatter\Configurator\JavaScript\Minifier;
  11  class ClosureCompilerApplication extends Minifier
  12  {
  13      public $closureCompilerBin;
  14      public $command;
  15      public $compilationLevel = 'ADVANCED_OPTIMIZATIONS';
  16      public $excludeDefaultExterns = \true;
  17      public $javaBin = 'java';
  18      public $options = '--use_types_for_optimization';
  19  	public function __construct($filepathOrCommand = \null)
  20      {
  21          if (isset($filepathOrCommand))
  22              if (\file_exists($filepathOrCommand) && \substr($filepathOrCommand, -4) === '.jar')
  23                  $this->closureCompilerBin = $filepathOrCommand;
  24              else
  25                  $this->command = $filepathOrCommand;
  26      }
  27  	public function getCacheDifferentiator()
  28      {
  29          $key = [
  30              $this->compilationLevel,
  31              $this->excludeDefaultExterns,
  32              $this->options
  33          ];
  34          if (isset($this->closureCompilerBin))
  35              $key[] = $this->getClosureCompilerBinHash();
  36          if ($this->excludeDefaultExterns)
  37              $key[] = \file_get_contents(__DIR__ . '/../externs.application.js');
  38          return $key;
  39      }
  40  	public function minify($src)
  41      {
  42          $this->testFilepaths();
  43          $options = ($this->options) ? ' ' . $this->options : '';
  44          if ($this->excludeDefaultExterns && $this->compilationLevel === 'ADVANCED_OPTIMIZATIONS')
  45              $options .= ' --externs ' . __DIR__ . '/../externs.application.js --env=CUSTOM';
  46          $crc     = \crc32($src);
  47          $inFile  = \sys_get_temp_dir() . '/' . $crc . '.js';
  48          $outFile = \sys_get_temp_dir() . '/' . $crc . '.min.js';
  49          \file_put_contents($inFile, $src);
  50          if (isset($this->command))
  51              $cmd = $this->command;
  52          else
  53              $cmd = \escapeshellcmd($this->javaBin) . ' -jar ' . \escapeshellarg($this->closureCompilerBin);
  54          $cmd .= ' --compilation_level ' . \escapeshellarg($this->compilationLevel)
  55               . $options
  56               . ' --js ' . \escapeshellarg($inFile)
  57               . ' --js_output_file ' . \escapeshellarg($outFile);
  58          \exec($cmd . ' 2>&1', $output, $return);
  59          \unlink($inFile);
  60          if (\file_exists($outFile))
  61          {
  62              $src = \trim(\file_get_contents($outFile));
  63              \unlink($outFile);
  64          }
  65          if (!empty($return))
  66              throw new RuntimeException('An error occured during minification: ' . \implode("\n", $output));
  67          return $src;
  68      }
  69  	protected function getClosureCompilerBinHash()
  70      {
  71          static $cache = [];
  72          if (!isset($cache[$this->closureCompilerBin]))
  73              $cache[$this->closureCompilerBin] = \md5_file($this->closureCompilerBin);
  74          return $cache[$this->closureCompilerBin];
  75      }
  76  	protected function testFilepaths()
  77      {
  78          if (isset($this->command))
  79              return;
  80          if (!isset($this->closureCompilerBin))
  81              throw new RuntimeException('No path set for Closure Compiler');
  82          if (!\file_exists($this->closureCompilerBin))
  83              throw new RuntimeException('Cannot find Closure Compiler at ' . $this->closureCompilerBin);
  84      }
  85  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1