Skip to content

Latest commit

 

History

History
95 lines (90 loc) · 2.85 KB

README.md

File metadata and controls

95 lines (90 loc) · 2.85 KB

yui-compressor-php-wrapper

A Simple PHP wrapper for yahoo's Yui Compressor

I started this project because I couldn't find a good PHP wrapper for the YUI-Compressor which was also easy usable with composer. I decided to share this project with others because I also wat to give something back to the PHP community. Feel free to use this project for whatever you like. If you strungle with a problem please create an issue. Of course I'm not perfect just like this project so if you've got improvements; share them with us!

I Started to built this project with gpbmike's PHP-YUI-Compressor project, and improved it the way I thought was the best.

##Usage ###Step 1. Throw the "yui-compressor-php-wrapper" folder in the "vendor" folder of your project ###Step 2. Require the project in your project's composer.json file.

{
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/dstollie/yui-compressor-php-wrapper"
    }
  ],
  "require": {
    "dstollie/yui-compressor-php-wrapper": "dev-master"
  }
}

###Step 3.

	try {
	  $compressor = new Compressor('path/to/yuicompressor.jar');
	  $compressor->addFile("test.js");
	  $compressor->setOption('outfile', 'outfile.js');
	  $compressor->addString("var x = 1 + 1;");
	  echo $compressor->compress();
	} catch (CompressorException $e) {
	  echo $e->getMessage();
	}

##Examples The help you a bit, here are some examples. These examples are also viewable in the "tests/test.php" file. ###The long way

try {
    $compressor = new Compressor('path/to/yuicompressor.jar');
    $compressor->addFile("test.js");
    $compressor->setOption('outfile', 'outfile.js');
    $compressor->addString("var x = 1 + 1;");
    echo $compressor->compress();
} catch (CompressorException $e) {
    echo $e->getMessage();
}

###The short way

try {
    $compressor = new Compressor('path/to/yuicompressor.jar');
    echo $compressor->compress('test.js', 'out.js');
} catch (CompressorException $e) {
    echo $e->getMessage();
}

###The css short way

try {
    $compressor = new Compressor('path/to/yuicompressor.jar');
    $compressor->setOption('type', 'css');
    $result = $compressor->compress('test.css', 'out.css');
    if($result === true) {
        echo "success";
    } else {
        echo $result;
    }
} catch (CompressorException $e) {
    echo $e->getMessage();
}

###The css short way without an output file

try {
    $compressor = new Compressor('path/to/yuicompressor.jar');
    $compressor->setOption('type', 'css');
    $result = $compressor->compress('test.css');
    if($result) {
        echo "raw content: " . $compressor->getCompressionOutput();
    } else {
        echo "error: " . $compressor->getCompressionOutput();
    }
} catch (CompressorException $e) {
    echo $e->getMessage();
}