[7575] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * This file is part of the Assetic package, an OpenSky project. |
---|
| 5 | * |
---|
| 6 | * (c) 2010-2012 OpenSky Project Inc |
---|
| 7 | * |
---|
| 8 | * For the full copyright and license information, please view the LICENSE |
---|
| 9 | * file that was distributed with this source code. |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | namespace Assetic\Filter\Yui; |
---|
| 13 | |
---|
| 14 | use Assetic\Asset\AssetInterface; |
---|
| 15 | use Assetic\Filter\FilterInterface; |
---|
| 16 | use Assetic\Exception\FilterException; |
---|
| 17 | use Symfony\Component\Process\ProcessBuilder; |
---|
| 18 | |
---|
| 19 | /** |
---|
| 20 | * Base YUI compressor filter. |
---|
| 21 | * |
---|
| 22 | * @link http://developer.yahoo.com/yui/compressor/ |
---|
| 23 | * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
---|
| 24 | */ |
---|
| 25 | abstract class BaseCompressorFilter implements FilterInterface |
---|
| 26 | { |
---|
| 27 | private $jarPath; |
---|
| 28 | private $javaPath; |
---|
| 29 | private $charset; |
---|
| 30 | private $lineBreak; |
---|
| 31 | |
---|
| 32 | public function __construct($jarPath, $javaPath = '/usr/bin/java') |
---|
| 33 | { |
---|
| 34 | $this->jarPath = $jarPath; |
---|
| 35 | $this->javaPath = $javaPath; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | public function setCharset($charset) |
---|
| 39 | { |
---|
| 40 | $this->charset = $charset; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | public function setLineBreak($lineBreak) |
---|
| 44 | { |
---|
| 45 | $this->lineBreak = $lineBreak; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | public function filterLoad(AssetInterface $asset) |
---|
| 49 | { |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * Compresses a string. |
---|
| 54 | * |
---|
| 55 | * @param string $content The content to compress |
---|
| 56 | * @param string $type The type of content, either "js" or "css" |
---|
| 57 | * @param array $options An indexed array of additional options |
---|
| 58 | * |
---|
| 59 | * @return string The compressed content |
---|
| 60 | */ |
---|
| 61 | protected function compress($content, $type, $options = array()) |
---|
| 62 | { |
---|
| 63 | $pb = new ProcessBuilder(array( |
---|
| 64 | $this->javaPath, |
---|
| 65 | '-jar', |
---|
| 66 | $this->jarPath, |
---|
| 67 | )); |
---|
| 68 | |
---|
| 69 | foreach ($options as $option) { |
---|
| 70 | $pb->add($option); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | if (null !== $this->charset) { |
---|
| 74 | $pb->add('--charset')->add($this->charset); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | if (null !== $this->lineBreak) { |
---|
| 78 | $pb->add('--line-break')->add($this->lineBreak); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | // input and output files |
---|
| 82 | $tempDir = realpath(sys_get_temp_dir()); |
---|
| 83 | $input = tempnam($tempDir, 'YUI-IN-'); |
---|
| 84 | $output = tempnam($tempDir, 'YUI-OUT-'); |
---|
| 85 | file_put_contents($input, $content); |
---|
| 86 | $pb->add('-o')->add($output)->add('--type')->add($type)->add($input); |
---|
| 87 | |
---|
| 88 | $proc = $pb->getProcess(); |
---|
| 89 | $code = $proc->run(); |
---|
| 90 | unlink($input); |
---|
| 91 | |
---|
| 92 | if (0 < $code) { |
---|
| 93 | if (file_exists($output)) { |
---|
| 94 | unlink($output); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | throw FilterException::fromProcess($proc)->setInput($content); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | if (!file_exists($output)) { |
---|
| 101 | throw new \RuntimeException('Error creating output file.'); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | $retval = file_get_contents($output); |
---|
| 105 | unlink($output); |
---|
| 106 | |
---|
| 107 | return $retval; |
---|
| 108 | } |
---|
| 109 | } |
---|