Return to Snippet

Revision: 37786
at January 21, 2011 18:39 by iroybot


Updated Code
<?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->tmpname = "joined-".time().".js";
		$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();
		$tmp = file_get_contents('/static.scripts.xml');
		// 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;
			if (file_exists($_SERVER["DOCUMENT_ROOT"]."/js/$filename")) {
				$jsString .= file_get_contents($_SERVER["DOCUMENT_ROOT"]."/js/$filename"); // join them together
			} else {
				echo "file <samp>$filename</samp> not found!";
			}
		}
		if (!is_dir($_SERVER["DOCUMENT_ROOT"].$this->tmppath)) {
			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;
		if (file_exists($_SERVER["DOCUMENT_ROOT"].$this->tmppath.$this->tmpname)) {
			$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";
			exec($compilerCommand, $return, $code);
			if ($code != 0) {
				return sprintf("Fuck! Something went wrong: %s (%s)", join('<br />', $return), $code);
			} else {
				return "compiled successfully";
			}
		} else {
			return "tempfile not found.";
		}
	}
}

Revision: 37785
at December 17, 2010 02:44 by iroybot


Updated Code
<?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>jquery.actions.js</item>
	<item>jquery-ui-1.8.6.custom.min.js</item>
	<item>pointbar.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->tmpname = "joined-".time().".js";
		$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();
		$tmp = file_get_contents('/static.scripts.xml');
		// 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;
			if (file_exists($_SERVER["DOCUMENT_ROOT"]."/js/$filename")) {
				$jsString .= file_get_contents($_SERVER["DOCUMENT_ROOT"]."/js/$filename"); // join them together
			} else {
				echo "file <samp>$filename</samp> not found!";
			}
		}
		if (!is_dir($_SERVER["DOCUMENT_ROOT"].$this->tmppath)) {
			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;
		if (file_exists($_SERVER["DOCUMENT_ROOT"].$this->tmppath.$this->tmpname)) {
			$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";
			exec($compilerCommand, $return, $code);
			if ($code != 0) {
				return sprintf("Fuck! Something went wrong: %s (%s)", join('<br />', $return), $code);
			} else {
				return "compiled successfully";
			}
		} else {
			return "tempfile not found.";
		}
	}
}

Revision: 37784
at December 17, 2010 02:43 by iroybot


Updated Code
<?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>jquery.actions.js</item>
	<item>jquery-ui-1.8.6.custom.min.js</item>
	<item>pointbar.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 Royal_JSCompiler() {
		$this->tmpname = "joined-".time().".js";
		$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();
		$tmp = file_get_contents('/static.scripts.xml');
		// 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;
			if (file_exists($_SERVER["DOCUMENT_ROOT"]."/js/$filename")) {
				$jsString .= file_get_contents($_SERVER["DOCUMENT_ROOT"]."/js/$filename"); // join them together
			} else {
				echo "file <samp>$filename</samp> not found!";
			}
		}
		if (!is_dir($_SERVER["DOCUMENT_ROOT"].$this->tmppath)) {
			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;
		if (file_exists($_SERVER["DOCUMENT_ROOT"].$this->tmppath.$this->tmpname)) {
			$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";
			exec($compilerCommand, $return, $code);
			if ($code != 0) {
				return sprintf("Fuck! Something went wrong: %s (%s)", join('<br />', $return), $code);
			} else {
				return "compiled successfully";
			}
		} else {
			return "tempfile not found.";
		}
	}
}

Revision: 37783
at December 17, 2010 02:41 by iroybot


Initial Code
<?php

class Royal_JSCompiler {
	public $scriptpath = '/js/';
	public $tmppath = '/js/cache/';
	public $tmpname;
	public $compiler = '/js/jscompiler/compiler.jar';
	public $output = '/js/scripts.min.js';
	
	public function Royal_JSCompiler() {
		$this->tmpname = "joined-".time().".js";
		$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();
		$tmp = file_get_contents('/static.scripts.xml');
		// 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;
			if (file_exists($_SERVER["DOCUMENT_ROOT"]."/js/$filename")) {
				$jsString .= file_get_contents($_SERVER["DOCUMENT_ROOT"]."/js/$filename"); // join them together
			} else {
				echo "file <samp>$filename</samp> not found!";
			}
		}
		if (!is_dir($_SERVER["DOCUMENT_ROOT"].$this->tmppath)) {
			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;
		if (file_exists($_SERVER["DOCUMENT_ROOT"].$this->tmppath.$this->tmpname)) {
			$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";
			exec($compilerCommand, $return, $code);
			if ($code != 0) {
				return sprintf("Fuck! Something went wrong: %s (%s)", join('<br />', $return), $code);
			} else {
				return "compiled successfully";
			}
		} else {
			return "tempfile not found.";
		}
	}
}

Initial URL


Initial Description
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.

Initial Title
Compiling JS with Closure Compiler and PHP

Initial Tags


Initial Language
PHP