Completed
Push — master ( 40af63...ba54cf )
by Josh
03:58
created

ClosureCompilerApplication::minify()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6.0038

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 36
ccs 20
cts 21
cp 0.9524
rs 8.9777
c 0
b 0
f 0
cc 6
nc 16
nop 1
crap 6.0038
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
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 8
	public function __construct($command)
41
	{
42 8
		$this->command = $command;
43
	}
44
45
	/**
46
	* {@inheritdoc}
47
	*/
48 6
	public function getCacheDifferentiator()
49
	{
50
		$key = [
51 6
			$this->command,
52 6
			$this->compilationLevel,
53 6
			$this->excludeDefaultExterns,
54 6
			$this->options
55
		];
56 6
		if ($this->excludeDefaultExterns)
57
		{
58 6
			$key[] = file_get_contents(__DIR__ . '/../externs.application.js');
59
		}
60
61 6
		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 2
	public function minify($src)
71
	{
72 2
		$options = ($this->options) ? ' ' . $this->options : '';
73
74
		// Add our custom externs if default externs are disabled
75 2
		if ($this->excludeDefaultExterns && $this->compilationLevel === 'ADVANCED_OPTIMIZATIONS')
76
		{
77 1
			$options .= ' --externs ' . __DIR__ . '/../externs.application.js --env=CUSTOM';
78
		}
79
80 2
		$crc     = crc32($src);
81 2
		$inFile  = sys_get_temp_dir() . '/' . $crc . '.js';
82 2
		$outFile = sys_get_temp_dir() . '/' . $crc . '.min.js';
83 2
		file_put_contents($inFile, $src);
84
85 2
		$cmd = $this->command
86 2
		     . ' --compilation_level ' . escapeshellarg($this->compilationLevel)
87 2
		     . $options
88 2
		     . ' --js ' . escapeshellarg($inFile)
89 2
		     . ' --js_output_file ' . escapeshellarg($outFile);
90
91 2
		exec($cmd . ' 2>&1', $output, $return);
92 2
		unlink($inFile);
93
94 2
		if (file_exists($outFile))
95
		{
96 2
			$src = trim(file_get_contents($outFile));
97 2
			unlink($outFile);
98
		}
99
100 2
		if (!empty($return))
101
		{
102
			throw new RuntimeException('An error occured during minification: ' . implode("\n", $output));
103
		}
104
105 2
		return $src;
106
	}
107
}