1<?php
2
3/*
4 * This file is part of the Assetic package, an OpenSky project.
5 *
6 * (c) 2010-2014 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
12namespace Assetic\Filter\Yui;
13
14use Assetic\Asset\AssetInterface;
15use Assetic\Exception\FilterException;
16use Assetic\Filter\BaseProcessFilter;
17use Assetic\Util\FilesystemUtils;
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 */
25abstract class BaseCompressorFilter extends BaseProcessFilter
26{
27    private $jarPath;
28    private $javaPath;
29    private $charset;
30    private $lineBreak;
31    private $stackSize;
32
33    public function __construct($jarPath, $javaPath = '/usr/bin/java')
34    {
35        $this->jarPath = $jarPath;
36        $this->javaPath = $javaPath;
37    }
38
39    public function setCharset($charset)
40    {
41        $this->charset = $charset;
42    }
43
44    public function setLineBreak($lineBreak)
45    {
46        $this->lineBreak = $lineBreak;
47    }
48
49    public function setStackSize($stackSize)
50    {
51        $this->stackSize = $stackSize;
52    }
53
54    public function filterLoad(AssetInterface $asset)
55    {
56    }
57
58    /**
59     * Compresses a string.
60     *
61     * @param string $content The content to compress
62     * @param string $type    The type of content, either "js" or "css"
63     * @param array  $options An indexed array of additional options
64     *
65     * @return string The compressed content
66     */
67    protected function compress($content, $type, $options = array())
68    {
69        $pb = $this->createProcessBuilder(array($this->javaPath));
70
71        if (null !== $this->stackSize) {
72            $pb->add('-Xss'.$this->stackSize);
73        }
74
75        $pb->add('-jar')->add($this->jarPath);
76
77        foreach ($options as $option) {
78            $pb->add($option);
79        }
80
81        if (null !== $this->charset) {
82            $pb->add('--charset')->add($this->charset);
83        }
84
85        if (null !== $this->lineBreak) {
86            $pb->add('--line-break')->add($this->lineBreak);
87        }
88
89        // input and output files
90        $tempDir = FilesystemUtils::getTemporaryDirectory();
91        $input = tempnam($tempDir, 'assetic_yui_input');
92        $output = tempnam($tempDir, 'assetic_yui_output');
93        file_put_contents($input, $content);
94        $pb->add('-o')->add($output)->add('--type')->add($type)->add($input);
95
96        $proc = $pb->getProcess();
97        $code = $proc->run();
98        unlink($input);
99
100        if (0 !== $code) {
101            if (file_exists($output)) {
102                unlink($output);
103            }
104
105            throw FilterException::fromProcess($proc)->setInput($content);
106        }
107
108        if (!file_exists($output)) {
109            throw new \RuntimeException('Error creating output file.');
110        }
111
112        $retval = file_get_contents($output);
113        unlink($output);
114
115        return $retval;
116    }
117}
118