[ Index ]

PHP Cross Reference of phpBB-3.3.14-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-2022 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  
  10  use RuntimeException;
  11  use s9e\TextFormatter\Configurator\JavaScript\Minifier;
  12  
  13  class ClosureCompilerApplication extends Minifier
  14  {
  15      /**
  16      * @var string Command used to invoke the Closure Compiler application
  17      */
  18      public $command;
  19  
  20      /**
  21      * @var string Closure Compiler's compilation level
  22      */
  23      public $compilationLevel = 'ADVANCED_OPTIMIZATIONS';
  24  
  25      /**
  26      * @var bool Whether to exclude Closure Compiler's default externs
  27      */
  28      public $excludeDefaultExterns = true;
  29  
  30      /**
  31      * @var string Extra options to be passed to the Closure Compiler application
  32      */
  33      public $options = '--use_types_for_optimization';
  34  
  35      /**
  36      * Constructor
  37      *
  38      * @param string $command Command to execute
  39      */
  40  	public function __construct($command)
  41      {
  42          $this->command = $command;
  43      }
  44  
  45      /**
  46      * {@inheritdoc}
  47      */
  48  	public function getCacheDifferentiator()
  49      {
  50          $key = [
  51              $this->command,
  52              $this->compilationLevel,
  53              $this->excludeDefaultExterns,
  54              $this->options
  55          ];
  56          if ($this->excludeDefaultExterns)
  57          {
  58              $key[] = file_get_contents(__DIR__ . '/../externs.application.js');
  59          }
  60  
  61          return $key;
  62      }
  63  
  64      /**
  65      * Compile given JavaScript source via the Closure Compiler application
  66      *
  67      * @param  string $src JavaScript source
  68      * @return string      Compiled source
  69      */
  70  	public function minify($src)
  71      {
  72          $options = ($this->options) ? ' ' . $this->options : '';
  73  
  74          // Add our custom externs if default externs are disabled
  75          if ($this->excludeDefaultExterns && $this->compilationLevel === 'ADVANCED_OPTIMIZATIONS')
  76          {
  77              $options .= ' --externs ' . __DIR__ . '/../externs.application.js --env=CUSTOM';
  78          }
  79  
  80          $crc     = crc32($src);
  81          $inFile  = sys_get_temp_dir() . '/' . $crc . '.js';
  82          $outFile = sys_get_temp_dir() . '/' . $crc . '.min.js';
  83          file_put_contents($inFile, $src);
  84  
  85          $cmd = $this->command
  86               . ' --compilation_level ' . escapeshellarg($this->compilationLevel)
  87               . $options
  88               . ' --js ' . escapeshellarg($inFile)
  89               . ' --js_output_file ' . escapeshellarg($outFile);
  90  
  91          exec($cmd . ' 2>&1', $output, $return);
  92          unlink($inFile);
  93  
  94          if (file_exists($outFile))
  95          {
  96              $src = trim(file_get_contents($outFile));
  97              unlink($outFile);
  98          }
  99  
 100          if (!empty($return))
 101          {
 102              throw new RuntimeException('An error occured during minification: ' . implode("\n", $output));
 103          }
 104  
 105          return $src;
 106      }
 107  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1