├── README.md └── yuicompressor.php /README.md: -------------------------------------------------------------------------------- 1 | **I do not actively maintain this repository. There are a couple of forks that might work for you.** 2 | 3 | # How to use 4 | 5 | // INCLUDE COMPRESSOR CLASS 6 | include('yuicompressor.php'); 7 | 8 | // INVOKE CLASS 9 | $yui = new YUICompressor(JAR_PATH, TEMP_FILES_DIR, $options); 10 | 11 | // ADD FILES 12 | $yui->addFile($absolute_path_to_file); 13 | 14 | // ADD STRING 15 | $yui->addString($string); 16 | 17 | // COMPRESS 18 | $code = $yui->compress(); 19 | -------------------------------------------------------------------------------- /yuicompressor.php: -------------------------------------------------------------------------------- 1 | 'js', 10 | 'linebreak' => false, 11 | 'verbose' => false, 12 | 'nomunge' => false, 13 | 'semi' => false, 14 | 'nooptimize' => false); 15 | private $files = array(); 16 | private $string = ''; 17 | 18 | // construct with a path to the YUI jar and a path to a place to put temporary files 19 | function __construct($JAR_PATH, $TEMP_FILES_DIR, $options = array()) 20 | { 21 | $this->JAR_PATH = $JAR_PATH; 22 | $this->TEMP_FILES_DIR = $TEMP_FILES_DIR; 23 | 24 | foreach ($options as $option => $value) 25 | { 26 | $this->setOption($option, $value); 27 | } 28 | } 29 | 30 | // set one of the YUI compressor options 31 | function setOption($option, $value) 32 | { 33 | $this->options[$option] = $value; 34 | } 35 | 36 | // add a file (absolute path) to be compressed 37 | function addFile($file) 38 | { 39 | array_push($this->files, $file); 40 | } 41 | 42 | // add a strong to be compressed 43 | function addString($string) 44 | { 45 | $this->string .= ' ' . $string; 46 | } 47 | 48 | // the meat and potatoes, executes the compression command in shell 49 | function compress() 50 | { 51 | 52 | // read the input 53 | foreach ($this->files as $file) { 54 | $this->string .= file_get_contents($file) or die("Cannot read from uploaded file"); 55 | } 56 | 57 | // create single file from all input 58 | $input_hash = sha1($this->string); 59 | $file = $this->TEMP_FILES_DIR . '/' . $input_hash . '.txt'; 60 | $fh = fopen($file, 'w') or die("Can't create new file"); 61 | fwrite($fh, $this->string); 62 | fclose($fh); 63 | 64 | // start with basic command 65 | $cmd = "java -Xmx32m -jar " . escapeshellarg($this->JAR_PATH) . ' ' . escapeshellarg($file) . " --charset UTF-8"; 66 | 67 | // set the file type 68 | $cmd .= " --type " . (strtolower($this->options['type']) == "css" ? "css" : "js"); 69 | 70 | // and add options as needed 71 | if ($this->options['linebreak'] && intval($this->options['linebreak']) > 0) { 72 | $cmd .= ' --line-break ' . intval($this->options['linebreak']); 73 | } 74 | 75 | if ($this->options['verbose']) { 76 | $cmd .= " -v"; 77 | } 78 | 79 | if ($this->options['nomunge']) { 80 | $cmd .= ' --nomunge'; 81 | } 82 | 83 | if ($this->options['semi']) { 84 | $cmd .= ' --preserve-semi'; 85 | } 86 | 87 | if ($this->options['nooptimize']) { 88 | $cmd .= ' --disable-optimizations'; 89 | } 90 | 91 | // execute the command 92 | exec($cmd . ' 2>&1', $raw_output); 93 | 94 | // add line breaks to show errors in an intelligible manner 95 | $flattened_output = implode("\n", $raw_output); 96 | 97 | // clean up (remove temp file) 98 | unlink($file); 99 | 100 | // return compressed output 101 | return $flattened_output; 102 | } 103 | } 104 | 105 | ?> --------------------------------------------------------------------------------