| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Minify_ClosureCompiler |
| 5 |
* @package Minify |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Run Closure Compiler via NailGun |
| 10 |
* |
| 11 |
* @package Minify |
| 12 |
* @author Elan Ruusamäe <[email protected]> |
| 13 |
* @link https://github.com/martylamb/nailgun |
| 14 |
*/ |
| 15 |
class Minify_NailgunClosureCompiler extends Minify_ClosureCompiler |
| 16 |
{ |
| 17 |
const NG_SERVER = 'com.martiansoftware.nailgun.NGServer'; |
| 18 |
const CC_MAIN = 'com.google.javascript.jscomp.CommandLineRunner'; |
| 19 |
|
| 20 |
/** |
| 21 |
* For some reasons Nailgun thinks that it's server |
| 22 |
* broke the connection and returns 227 instead of 0 |
| 23 |
* We'll just handle this here instead of fixing |
| 24 |
* the nailgun client itself. |
| 25 |
* |
| 26 |
* It also sometimes breaks on 229 on the devbox. |
| 27 |
* To complete this whole madness and made future |
| 28 |
* 'fixes' easier I added this nice little array... |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
private static $NG_EXIT_CODES = array(0, 227, 229); |
| 32 |
|
| 33 |
/** |
| 34 |
* Filepath of "ng" executable (from Nailgun package) |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public static $ngExecutable = 'ng'; |
| 39 |
|
| 40 |
/** |
| 41 |
* Filepath of the Nailgun jar file. |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
public static $ngJarFile; |
| 46 |
|
| 47 |
/** |
| 48 |
* Get command to launch NailGun server. |
| 49 |
* |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
protected function getServerCommandLine() |
| 53 |
{ |
| 54 |
$this->checkJar(self::$ngJarFile); |
| 55 |
$this->checkJar(self::$jarFile); |
| 56 |
|
| 57 |
$classPath = array( |
| 58 |
self::$ngJarFile, |
| 59 |
self::$jarFile, |
| 60 |
); |
| 61 |
|
| 62 |
// The command for the server that should show up in the process list |
| 63 |
$server = array( |
| 64 |
self::$javaExecutable, |
| 65 |
'-server', |
| 66 |
'-cp', implode(':', $classPath), |
| 67 |
self::NG_SERVER, |
| 68 |
); |
| 69 |
|
| 70 |
return $server; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @return array |
| 75 |
* @throws Minify_ClosureCompiler_Exception |
| 76 |
*/ |
| 77 |
protected function getCompilerCommandLine() |
| 78 |
{ |
| 79 |
$server = array( |
| 80 |
self::$ngExecutable, |
| 81 |
escapeshellarg(self::CC_MAIN) |
| 82 |
); |
| 83 |
|
| 84 |
return $server; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @param string $tmpFile |
| 89 |
* @param array $options |
| 90 |
* @return string |
| 91 |
* @throws Minify_ClosureCompiler_Exception |
| 92 |
*/ |
| 93 |
protected function compile($tmpFile, $options) |
| 94 |
{ |
| 95 |
$this->startServer(); |
| 96 |
|
| 97 |
$command = $this->getCommand($options, $tmpFile); |
| 98 |
|
| 99 |
return implode("\n", $this->shell($command, self::$NG_EXIT_CODES)); |
| 100 |
} |
| 101 |
|
| 102 |
private function startServer() |
| 103 |
{ |
| 104 |
$serverCommand = implode(' ', $this->getServerCommandLine()); |
| 105 |
$psCommand = $this->shell("ps -o cmd= -C " . self::$javaExecutable); |
| 106 |
if (in_array($serverCommand, $psCommand, true)) { |
| 107 |
// already started! |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
$this->shell("$serverCommand </dev/null >/dev/null 2>/dev/null & sleep 10"); |
| 112 |
} |
| 113 |
} |
| 114 |
|