├── .gitignore ├── COPYING.txt ├── README.md ├── composer.json ├── composer.lock └── lib └── YUI ├── Compressor.php └── Exception.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | composer.phar 3 | -------------------------------------------------------------------------------- /COPYING.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013, Sam Stenvall 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | Redistributions in binary form must reproduce the above copyright notice, this 10 | list of conditions and the following disclaimer in the documentation and/or 11 | other materials provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 14 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 17 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 20 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 21 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | php-yui-compressor 2 | ================== 3 | 4 | A modern PHP wrapper for the YUI compressor. 5 | 6 | Installation 7 | ------------ 8 | 9 | Run `composer install` (install Composer if you haven't done so already). You will also need to have Java installed and available in your path (on Debian/Ubuntu-based systems you can install it using `sudo apt-get install default-jre`) 10 | 11 | Usage 12 | ----- 13 | 14 | The following snippet assumes you've included the Composer-generated autoloader. 15 | 16 | ```php 17 | setType(\YUI\Compressor::TYPE_CSS); 27 | $optimizedCss = $yui->compress($css); 28 | 29 | // Compress the JavaScript 30 | $yui->setType(\YUI\Compressor::TYPE_JS); 31 | $optimizedScript = $yui->compress($script); 32 | ``` 33 | 34 | ```php 35 | '/usr/bin/java', 41 | 'line-break'=>80, 42 | 'disable-optimizations'=>true, 43 | )); 44 | 45 | // Read the uncompressed contents 46 | $css = file_get_contents('styles.css'); 47 | $script = file_get_contents('script.js'); 48 | 49 | // The "disable-optimizations" option is JavaScript-specific so it won't apply 50 | // here... 51 | $yui->setType(\YUI\Compressor::TYPE_CSS); 52 | $optimizedCss = $yui->compress($css); 53 | 54 | // ...but here it will 55 | $yui->setType(\YUI\Compressor::TYPE_JS); 56 | $optimizedScript = $yui->compress($script); 57 | ``` 58 | 59 | Credits 60 | ------- 61 | 62 | Inspired by the work of gpbmike (https://github.com/gpbmike/PHP-YUI-Compressor) 63 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jalle19/php-yui-compressor", 3 | "type": "library", 4 | "description": "A modern PHP wrapper for the YUI compressor", 5 | "keywords": ["js", "css", "minify", "yui"], 6 | "homepage": "https://github.com/Jalle19/php-yui-compressor", 7 | "license": "BSD-2-Clause", 8 | "require": { 9 | "php": ">=5.3.0", 10 | "nervo/yuicompressor": "2.4.*" 11 | }, 12 | "autoload": { 13 | "psr-0": { 14 | "YUI": "lib/" 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" 5 | ], 6 | "hash": "7fa4a052139cbe543167f3e7dfdf8b8d", 7 | "packages": [ 8 | { 9 | "name": "nervo/yuicompressor", 10 | "version": "dev-master", 11 | "source": { 12 | "type": "git", 13 | "url": "https://github.com/nervo/yuicompressor.git", 14 | "reference": "2.4.7" 15 | }, 16 | "dist": { 17 | "type": "zip", 18 | "url": "https://api.github.com/repos/nervo/yuicompressor/zipball/2.4.7", 19 | "reference": "2.4.7", 20 | "shasum": "" 21 | }, 22 | "type": "library", 23 | "notification-url": "https://packagist.org/downloads/", 24 | "license": [ 25 | "BSD" 26 | ], 27 | "description": "YUI Compressor is an open source tool that supports the compression of both JavaScript and CSS files. The JavaScript compression removes comments and white-spaces as well as obfuscates local variables using the smallest possible variable name. CSS compression is done using a regular-expression-based CSS minifier.", 28 | "homepage": "https://github.com/yui/yuicompressor", 29 | "keywords": [ 30 | "java", 31 | "yui" 32 | ], 33 | "time": "2012-05-12 08:31:30" 34 | } 35 | ], 36 | "packages-dev": [ 37 | 38 | ], 39 | "aliases": [ 40 | 41 | ], 42 | "minimum-stability": "stable", 43 | "stability-flags": { 44 | "nervo/yuicompressor": 20 45 | }, 46 | "platform": { 47 | "php": ">=5.3.0" 48 | }, 49 | "platform-dev": [ 50 | 51 | ] 52 | } 53 | -------------------------------------------------------------------------------- /lib/YUI/Compressor.php: -------------------------------------------------------------------------------- 1 | 8 | * @copyright Copyright © Sam Stenvall 2013- 9 | * @license http://www.opensource.org/licenses/bsd-license.php New BSD License 10 | */ 11 | 12 | namespace YUI; 13 | 14 | class Compressor 15 | { 16 | 17 | const TYPE_CSS = 'css'; 18 | const TYPE_JS = 'js'; 19 | 20 | /** 21 | * @var string the absolute path to yuicompressor.jar 22 | */ 23 | private $_jarPath; 24 | 25 | /** 26 | * @var array the default options for the YUI compressor. In addition to 27 | * the parameters supported by the YUI compressor it includes "javaPath" 28 | * which is the path to the "java" binary (defaults to "java"). 29 | */ 30 | private $_options = array( 31 | 'javaPath'=>'java', 32 | 'type'=>self::TYPE_JS, 33 | 'charset'=>'UTF-8', 34 | 'line-break'=>false, 35 | 'verbose'=>false, 36 | 'nomunge'=>false, 37 | 'preserve-semi'=>false, 38 | 'disable-optimizations'=>false, 39 | ); 40 | 41 | /** 42 | * @var array the names of the JavaScript-specific compressor options 43 | */ 44 | private static $_javaScriptOptions = array( 45 | 'nomunge', 46 | 'preserve-semi', 47 | 'disable-optimizations', 48 | ); 49 | 50 | /** 51 | * Class constructor. The specified options will be merged with the 52 | * default ones. 53 | * @param array $options options for the YUI compressor. Default to an 54 | * empty array, meaning the default options will be used. 55 | * @throws Exception if an invalid option is supplied 56 | */ 57 | public function __construct($options = array()) 58 | { 59 | // The path to the JAR file will vary depending on whether this package 60 | // is installed along with other Composer packages or not 61 | $this->_jarPath = realpath(__DIR__ 62 | .'/../../vendor/nervo/yuicompressor/yuicompressor.jar'); 63 | if ($this->_jarPath === false) 64 | $this->_jarPath = realpath(__DIR__ 65 | .'/../../../../nervo/yuicompressor/yuicompressor.jar'); 66 | 67 | if ($this->_jarPath === false) 68 | throw new Exception('Failed to locate yuicompressor.jar'); 69 | 70 | // Check that all supplied options are valid 71 | foreach (array_keys($options) as $option) 72 | if (!array_key_exists($option, $this->_options)) 73 | throw new Exception('Invalid option: '.$option); 74 | 75 | $this->_options = array_merge($this->_options, $options); 76 | } 77 | 78 | /** 79 | * Sets the compressor type. Valid values are TYPE_JS and TYPE_CSS. This 80 | * method can be used to change the compressor type after the class has 81 | * been instantiated. 82 | * @param string $type the type 83 | * @throws Exception if the type is invalid 84 | */ 85 | public function setType($type) 86 | { 87 | if ($type === self::TYPE_CSS || $type === self::TYPE_JS) 88 | $this->_options['type'] = $type; 89 | else 90 | throw new Exception('Invalid type: '.$type); 91 | } 92 | 93 | /** 94 | * Compresses the specified data and returns it 95 | * @param string $data the data 96 | * @return string the compressed data 97 | * @throws Exception if the compression fails 98 | */ 99 | public function compress($data) 100 | { 101 | // Construct the command 102 | $cmd = $this->_options['javaPath'].' -jar '.escapeshellarg($this->_jarPath); 103 | $cmd .= ' --charset '.$this->_options['charset']; 104 | $cmd .= ' --type '.$this->_options['type']; 105 | 106 | if ($this->_options['line-break'] !== false) 107 | $cmd .= ' --line-break '.(int) $this->_options['line-break']; 108 | 109 | if ($this->_options['verbose']) 110 | $cmd .= " -v"; 111 | 112 | // Javascript-specific options 113 | if ($this->_options['type'] === self::TYPE_JS) 114 | foreach (self::$_javaScriptOptions as $option) 115 | if ($this->_options[$option]) 116 | $cmd .= ' --'.$option; 117 | 118 | // Run the command 119 | $pipes = array(); 120 | $descriptors = array( 121 | 0=>array('pipe', 'r'), 122 | 1=>array('pipe', 'w'), 123 | 2=>array('pipe', 'w'), 124 | ); 125 | 126 | $process = proc_open($cmd, $descriptors, $pipes); 127 | 128 | if (is_resource($process)) 129 | { 130 | // Write the data we want to compress to STDIN 131 | fwrite($pipes[0], $data); 132 | fclose($pipes[0]); 133 | 134 | // Get the compressed data and eventual error from STDOUT and STDERR 135 | $compressedData = stream_get_contents($pipes[1]); 136 | $error = stream_get_contents($pipes[2]); 137 | fclose($pipes[1]); 138 | fclose($pipes[2]); 139 | 140 | $return = proc_close($process); 141 | 142 | // Throw an exception if compression fails 143 | if ($return === 0) 144 | return $compressedData; 145 | else 146 | throw new Exception('Failed to compress data: '.$error, $return); 147 | } 148 | 149 | throw new Exception('Failed to open a process'); 150 | } 151 | 152 | } 153 | -------------------------------------------------------------------------------- /lib/YUI/Exception.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright © Sam Stenvall 2013- 8 | * @license http://www.opensource.org/licenses/bsd-license.php New BSD License 9 | */ 10 | namespace YUI; 11 | 12 | class Exception extends \Exception { 13 | 14 | } 15 | --------------------------------------------------------------------------------