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;
13
14use Assetic\Asset\AssetInterface;
15use Assetic\Exception\FilterException;
16use Assetic\Util\FilesystemUtils;
17
18/**
19 * Filter for the Google Closure Stylesheets Compiler JAR.
20 *
21 * @link http://code.google.com/p/closure-stylesheets/
22 * @author Matthias Krauser <matthias@krauser.eu>
23 */
24class GssFilter extends BaseProcessFilter
25{
26    private $jarPath;
27    private $javaPath;
28    private $allowUnrecognizedFunctions;
29    private $allowedNonStandardFunctions;
30    private $copyrightNotice;
31    private $define;
32    private $gssFunctionMapProvider;
33    private $inputOrientation;
34    private $outputOrientation;
35    private $prettyPrint;
36
37    public function __construct($jarPath, $javaPath = '/usr/bin/java')
38    {
39        $this->jarPath = $jarPath;
40        $this->javaPath = $javaPath;
41    }
42
43    public function setAllowUnrecognizedFunctions($allowUnrecognizedFunctions)
44    {
45        $this->allowUnrecognizedFunctions = $allowUnrecognizedFunctions;
46    }
47
48    public function setAllowedNonStandardFunctions($allowNonStandardFunctions)
49    {
50        $this->allowedNonStandardFunctions = $allowNonStandardFunctions;
51    }
52
53    public function setCopyrightNotice($copyrightNotice)
54    {
55        $this->copyrightNotice = $copyrightNotice;
56    }
57
58    public function setDefine($define)
59    {
60        $this->define = $define;
61    }
62
63    public function setGssFunctionMapProvider($gssFunctionMapProvider)
64    {
65        $this->gssFunctionMapProvider = $gssFunctionMapProvider;
66    }
67
68    public function setInputOrientation($inputOrientation)
69    {
70        $this->inputOrientation = $inputOrientation;
71    }
72
73    public function setOutputOrientation($outputOrientation)
74    {
75        $this->outputOrientation = $outputOrientation;
76    }
77
78    public function setPrettyPrint($prettyPrint)
79    {
80        $this->prettyPrint = $prettyPrint;
81    }
82
83    public function filterLoad(AssetInterface $asset)
84    {
85        $cleanup = array();
86
87        $pb = $this->createProcessBuilder(array(
88            $this->javaPath,
89            '-jar',
90            $this->jarPath,
91        ));
92
93        if (null !== $this->allowUnrecognizedFunctions) {
94            $pb->add('--allow-unrecognized-functions');
95        }
96
97        if (null !== $this->allowedNonStandardFunctions) {
98            $pb->add('--allowed_non_standard_functions')->add($this->allowedNonStandardFunctions);
99        }
100
101        if (null !== $this->copyrightNotice) {
102            $pb->add('--copyright-notice')->add($this->copyrightNotice);
103        }
104
105        if (null !== $this->define) {
106            $pb->add('--define')->add($this->define);
107        }
108
109        if (null !== $this->gssFunctionMapProvider) {
110            $pb->add('--gss-function-map-provider')->add($this->gssFunctionMapProvider);
111        }
112
113        if (null !== $this->inputOrientation) {
114            $pb->add('--input-orientation')->add($this->inputOrientation);
115        }
116
117        if (null !== $this->outputOrientation) {
118            $pb->add('--output-orientation')->add($this->outputOrientation);
119        }
120
121        if (null !== $this->prettyPrint) {
122            $pb->add('--pretty-print');
123        }
124
125        $pb->add($cleanup[] = $input = FilesystemUtils::createTemporaryFile('gss'));
126        file_put_contents($input, $asset->getContent());
127
128        $proc = $pb->getProcess();
129        $code = $proc->run();
130        array_map('unlink', $cleanup);
131
132        if (0 !== $code) {
133            throw FilterException::fromProcess($proc)->setInput($asset->getContent());
134        }
135
136        $asset->setContent($proc->getOutput());
137    }
138
139    public function filterDump(AssetInterface $asset)
140    {
141    }
142}
143