/ Published in: PHP
ported to PHP5 from this tutorial: http://bohuco.net/blog/2010/09/use-google-closure-compiler-local-with-php/
since I had problems running the compiler on mutliple files, I did the joining myself.
since I had problems running the compiler on mutliple files, I did the joining myself.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /** my scripts and the order of the scripts are stored in a simple XML file (static.scripts.xml): <group> <item>jquery.js</item> <item>jquery.easing.js</item> <item>scrollable.js</item> <item>scrollable.autoscroll.js</item> <item>pointbar.js</item> <item>jquery.actions.js</item> </group> **/ class JSCompiler { public $scriptpath = '/js/'; public $tmppath = '/js/cache/'; public $tmpname; public $compiler = '/js/jscompiler/compiler.jar'; public $output = '/js/scripts.min.js'; public function JSCompiler() { $this->output = $_SERVER["DOCUMENT_ROOT"].$this->output; $this->getScripts(); } /** * reads all Javascript files from XML, joins them and saves a temp file in /js/cache * @return void */ public function getScripts() { $scripts = new DOMDocument(); // load script names $scripts->loadXML($tmp); // get each files contents $xpath = new DOMXPath($scripts); $res = $xpath->query("//item"); $jsString = ''; foreach($res as $item) { $filename = $item->nodeValue; } else { echo "file <samp>$filename</samp> not found!"; } } echo $this->tmppath." doesn't exist"; } else { file_put_contents($_SERVER["DOCUMENT_ROOT"].$this->tmppath.$this->tmpname, $jsString); // save temp. file } } /** * fires the command to run Google Closure Compiler * @uses the temporary file created by getScipts() * @return string error message */ public function compile($output = '', $level = '') { if ($output === '') $output = $this->output; if ($level === '') $level = 'ADVANCED_OPTIMIZATIONS'; $compiler = $this->compiler; $compilerCommand = "/usr/bin/java -jar $compiler --compilation_level $level --jscomp_dev_mode START --js " . $_SERVER["DOCUMENT_ROOT"].$this->tmppath.$this->tmpname . " --js_output_file $output 2>&1"; if ($code != 0) { } else { return "compiled successfully"; } } else { return "tempfile not found."; } } }