From 7e8ff9a9106bd362439fc8a1c0a88e6c4b3a5537 Mon Sep 17 00:00:00 2001 From: davidsteinsland Date: Thu, 14 Feb 2013 14:04:15 +0100 Subject: [PATCH 1/2] Fixed a couple of things --- yuicompressor.php | 116 +++++++++++++++++++++++++--------------------- 1 file changed, 64 insertions(+), 52 deletions(-) diff --git a/yuicompressor.php b/yuicompressor.php index aef0d36..20f78a3 100644 --- a/yuicompressor.php +++ b/yuicompressor.php @@ -2,62 +2,88 @@ class YUICompressor { - - // absolute path to YUI jar file. - private static $JAR_PATH; - private static $TEMP_FILES_DIR; - private $options = array('type' => 'js', - 'linebreak' => false, - 'verbose' => false, - 'nomunge' => false, - 'semi' => false, - 'nooptimize' => false); + const DEFAULT_JAR_PATH = 'yuicompressor.jar'; + const DEFAULT_TMP_DIR = '/tmp/yuicompressor'; + + private $JAR_PATH; + private $TEMP_FILES_DIR; + private $options = array('type' => 'js', + 'linebreak' => false, + 'verbose' => false, + 'nomunge' => false, + 'semi' => false, + 'nooptimize' => false); private $files = array(); private $string = ''; // construct with a path to the YUI jar and a path to a place to put temporary files - function __construct($JAR_PATH, $TEMP_FILES_DIR, $options = array()) - { - $this->JAR_PATH = $JAR_PATH; - $this->TEMP_FILES_DIR = $TEMP_FILES_DIR; - + function __construct($JAR_PATH = null, $TEMP_FILES_DIR = null, $options = array()) + { + $this->JAR_PATH = realpath ( $JAR_PATH ?: self::DEFAULT_JAR_PATH ); + $this->TEMP_FILES_DIR = realpath ( $TEMP_FILES_DIR ?: self::DEFAULT_TMP_DIR ); + + if ( ! file_exists ( $this->JAR_PATH) ) + throw new Exception ( "Cannot find the JAR file: " . $this->JAR_PATH ); + if ( ! file_exists ( $this->TEMP_FILES_DIR) ) + throw new Exception ( "Cannot find the specified directory: " . $this->TEMP_FILES_DIR); + foreach ($options as $option => $value) { $this->setOption($option, $value); } } - // set one of the YUI compressor options - function setOption($option, $value) + /** + * Sets an option to the YUICompressor + * @see http://yui.github.com/yuicompressor + */ + public function setOption ($option, $value) { $this->options[$option] = $value; } - // add a file (absolute path) to be compressed - function addFile($file) - { - array_push($this->files, $file); + /** + * Adds a file which should be compressed. + * Path may be relative or absolute + */ + public function addFile ($file) + { + $file = realpath ($file); + + if ( ! file_exists ($file)) + throw new Exception ( "Cannot locate file: " . $file); + + array_push($this->files, $file); + + $this->string .= file_get_contents ($file); } - // add a strong to be compressed - function addString($string) + /** + * Used to pass a string instead of a file, + * i.e. with file_get_contents() + */ + public function addString ($string) { $this->string .= ' ' . $string; } - // the meat and potatoes, executes the compression command in shell - function compress() + /** + * Compresses the given files and returns the compressed + * data as a string. + */ + public function compress() { - - // read the input - foreach ($this->files as $file) { - $this->string .= file_get_contents($file) or die("Cannot read from uploaded file"); - } + if ( strlen ( $this->string) == 0 ) + throw new Exception ("Invalid State: Cannot compress when no data is given."); // create single file from all input $input_hash = sha1($this->string); - $file = $this->TEMP_FILES_DIR . '/' . $input_hash . '.txt'; - $fh = fopen($file, 'w') or die("Can't create new file"); + $file = $this->TEMP_FILES_DIR . DIRECTORY_SEPARATOR . $input_hash . '.txt'; + $fh = fopen($file, 'w'); + + if ( ! $fh ) + throw new Exception ( "Cannot write data to temp file" ); + fwrite($fh, $this->string); fclose($fh); @@ -68,38 +94,24 @@ function compress() $cmd .= " --type " . (strtolower($this->options['type']) == "css" ? "css" : "js"); // and add options as needed - if ($this->options['linebreak'] && intval($this->options['linebreak']) > 0) { + if ($this->options['linebreak'] && intval($this->options['linebreak']) > 0) $cmd .= ' --line-break ' . intval($this->options['linebreak']); - } - if ($this->options['verbose']) { + if ($this->options['verbose']) $cmd .= " -v"; - } - if ($this->options['nomunge']) { + if ($this->options['nomunge']) $cmd .= ' --nomunge'; - } - if ($this->options['semi']) { + if ($this->options['semi']) $cmd .= ' --preserve-semi'; - } - if ($this->options['nooptimize']) { + if ($this->options['nooptimize']) $cmd .= ' --disable-optimizations'; - } - // execute the command exec($cmd . ' 2>&1', $raw_output); - - // add line breaks to show errors in an intelligible manner - $flattened_output = implode("\n", $raw_output); - - // clean up (remove temp file) unlink($file); - // return compressed output - return $flattened_output; + return $raw_output; } } - -?> \ No newline at end of file From 6dce9ac5b46f92233a02dd99dafcc1e3f8978c6c Mon Sep 17 00:00:00 2001 From: davidsteinsland Date: Thu, 14 Feb 2013 14:10:29 +0100 Subject: [PATCH 2/2] Fixed whitespace issues --- yuicompressor.php | 124 +++++++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 62 deletions(-) diff --git a/yuicompressor.php b/yuicompressor.php index 20f78a3..6da5652 100644 --- a/yuicompressor.php +++ b/yuicompressor.php @@ -3,103 +3,103 @@ class YUICompressor { const DEFAULT_JAR_PATH = 'yuicompressor.jar'; - const DEFAULT_TMP_DIR = '/tmp/yuicompressor'; + const DEFAULT_TMP_DIR = '/tmp/yuicompressor'; + + private $JAR_PATH; + private $TEMP_FILES_DIR; + private $options = [ + 'type' => 'js', + 'linebreak' => false, + 'verbose' => false, + 'nomunge' => false, + 'semi' => false, + 'nooptimize' => false + ]; + + private $files = array(); + private $string = ''; - private $JAR_PATH; - private $TEMP_FILES_DIR; - private $options = array('type' => 'js', - 'linebreak' => false, - 'verbose' => false, - 'nomunge' => false, - 'semi' => false, - 'nooptimize' => false); - private $files = array(); - private $string = ''; - - // construct with a path to the YUI jar and a path to a place to put temporary files - function __construct($JAR_PATH = null, $TEMP_FILES_DIR = null, $options = array()) + public function __construct($JAR_PATH = null, $TEMP_FILES_DIR = null, $options = array()) { $this->JAR_PATH = realpath ( $JAR_PATH ?: self::DEFAULT_JAR_PATH ); - $this->TEMP_FILES_DIR = realpath ( $TEMP_FILES_DIR ?: self::DEFAULT_TMP_DIR ); + $this->TEMP_FILES_DIR = realpath ( $TEMP_FILES_DIR ?: self::DEFAULT_TMP_DIR ); if ( ! file_exists ( $this->JAR_PATH) ) throw new Exception ( "Cannot find the JAR file: " . $this->JAR_PATH ); if ( ! file_exists ( $this->TEMP_FILES_DIR) ) throw new Exception ( "Cannot find the specified directory: " . $this->TEMP_FILES_DIR); - - foreach ($options as $option => $value) - { - $this->setOption($option, $value); - } - } + + foreach ($options as $option => $value) + $this->setOption($option, $value); + } /** * Sets an option to the YUICompressor * @see http://yui.github.com/yuicompressor */ - public function setOption ($option, $value) - { - $this->options[$option] = $value; - } + public function setOption ($option, $value) + { + $this->options[$option] = $value; + } /** * Adds a file which should be compressed. * Path may be relative or absolute */ - public function addFile ($file) + public function addFile ($file) { $file = realpath ($file); if ( ! file_exists ($file)) throw new Exception ( "Cannot locate file: " . $file); - + array_push($this->files, $file); - $this->string .= file_get_contents ($file); - } - + } + /** * Used to pass a string instead of a file, * i.e. with file_get_contents() */ - public function addString ($string) - { - $this->string .= ' ' . $string; - } - + public function addString ($string) + { + $this->string .= ' ' . $string; + } + /** * Compresses the given files and returns the compressed * data as a string. */ - public function compress() - { + public function compress() + { if ( strlen ( $this->string) == 0 ) throw new Exception ("Invalid State: Cannot compress when no data is given."); - - // create single file from all input - $input_hash = sha1($this->string); - $file = $this->TEMP_FILES_DIR . DIRECTORY_SEPARATOR . $input_hash . '.txt'; + + // create single file from all input + $input_hash = sha1($this->string); + $file = $this->TEMP_FILES_DIR . DIRECTORY_SEPARATOR . $input_hash . '.txt'; + $fh = fopen($file, 'w'); - + if ( ! $fh ) throw new Exception ( "Cannot write data to temp file" ); + + fwrite($fh, $this->string); + fclose($fh); - fwrite($fh, $this->string); - fclose($fh); - // start with basic command - $cmd = "java -Xmx32m -jar " . escapeshellarg($this->JAR_PATH) . ' ' . escapeshellarg($file) . " --charset UTF-8"; - - // set the file type - $cmd .= " --type " . (strtolower($this->options['type']) == "css" ? "css" : "js"); - - // and add options as needed - if ($this->options['linebreak'] && intval($this->options['linebreak']) > 0) - $cmd .= ' --line-break ' . intval($this->options['linebreak']); + $cmd = "java -Xmx32m -jar " . escapeshellarg($this->JAR_PATH) . ' ' . escapeshellarg($file) . " --charset UTF-8"; + + // set the file type + $cmd .= " --type " . (strtolower($this->options['type']) == "css" ? "css" : "js"); + + // and add options as needed + if ($this->options['linebreak'] && intval($this->options['linebreak']) > 0) + $cmd .= ' --line-break ' . intval($this->options['linebreak']); - if ($this->options['verbose']) - $cmd .= " -v"; - + if ($this->options['verbose']) + $cmd .= " -v"; + if ($this->options['nomunge']) $cmd .= ' --nomunge'; @@ -108,10 +108,10 @@ public function compress() if ($this->options['nooptimize']) $cmd .= ' --disable-optimizations'; - - exec($cmd . ' 2>&1', $raw_output); - unlink($file); - - return $raw_output; - } + + exec($cmd . ' 2>&1', $raw_output); + unlink($file); + + return $raw_output; + } }