source: trunk/prototype/library/Assetic/Filter/UglifyJsFilter.php @ 7575

Revision 7575, 3.6 KB checked in by angelo, 12 years ago (diff)

Ticket #3197 - Reduzir tempo de carregamento do modulo Expresso Mail

Line 
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
12namespace Assetic\Filter;
13
14use Assetic\Asset\AssetInterface;
15use Assetic\Exception\FilterException;
16use Symfony\Component\Process\ProcessBuilder;
17
18/**
19 * UglifyJs filter.
20 *
21 * @link https://github.com/mishoo/UglifyJS
22 * @author André Roaldseth <andre@roaldseth.net>
23 */
24class UglifyJsFilter implements FilterInterface
25{
26    private $uglifyJsPath;
27    private $nodeJsPath;
28
29    private $noCopyright;
30    private $beautify;
31    private $unsafe;
32    private $mangle;
33
34    /**
35     * @param string $uglifyJsPath Absolute path to the uglifyjs executable
36     * @param string $nodeJsPath   Absolute path to the folder containg node.js executable
37     */
38    public function __construct($uglifyJsPath, $nodeJsPath = null)
39    {
40        $this->uglifyJsPath = $uglifyJsPath;
41        $this->nodeJsPath = $nodeJsPath;
42    }
43
44    /**
45     * Removes the first block of comments as well
46     * @param bool $noCopyright True to enable
47     */
48    public function setNoCopyright($noCopyright)
49    {
50        $this->noCopyright = $noCopyright;
51    }
52
53    /**
54     * Output indented code
55     * @param bool $beautify True to enable
56     */
57    public function setBeautify($beautify)
58    {
59        $this->beautify = $beautify;
60    }
61
62    /**
63     * Enable additional optimizations that are known to be unsafe in some situations.
64     * @param bool $unsafe True to enable
65     */
66    public function setUnsafe($unsafe)
67    {
68        $this->unsafe = $unsafe;
69    }
70
71    /**
72     * Safely mangle variable and function names for greater file compress.
73     * @param bool $mangle True to enable
74     */
75    public function setMangle($mangle)
76    {
77        $this->mangle = $mangle;
78    }
79
80    /**
81     * @see Assetic\Filter\FilterInterface::filterLoad()
82     */
83    public function filterLoad(AssetInterface $asset)
84    {
85    }
86
87    /**
88     * Run the asset through UglifyJs
89     *
90     * @see Assetic\Filter\FilterInterface::filterDump()
91     */
92    public function filterDump(AssetInterface $asset)
93    {
94        $executables = array();
95
96        if ($this->nodeJsPath !== null) {
97            $executables[] = $this->nodeJsPath;
98        }
99
100        $executables[] = $this->uglifyJsPath;
101
102        $pb = new ProcessBuilder($executables);
103
104        if ($this->noCopyright) {
105            $pb->add('--no-copyright');
106        }
107
108        if ($this->beautify) {
109            $pb->add('--beautify');
110        }
111
112        if ($this->unsafe) {
113            $pb->add('--unsafe');
114        }
115
116        if ($this->mangle) {
117            $pb->add('--mangle');
118        }
119
120        // input and output files
121        $input = tempnam(sys_get_temp_dir(), 'input');
122        $output = tempnam(sys_get_temp_dir(), 'output');
123
124        file_put_contents($input, $asset->getContent());
125        $pb->add('-o')->add($output)->add($input);
126
127        $proc = $pb->getProcess();
128        $code = $proc->run();
129        unlink($input);
130
131        if (0 < $code) {
132            if (file_exists($output)) {
133                unlink($output);
134            }
135
136            if (127 === $code) {
137                throw new \RuntimeException('Path to node executable could not be resolved.');
138            }
139
140            throw FilterException::fromProcess($proc)->setInput($asset->getContent());
141        }
142
143        if (!file_exists($output)) {
144            throw new \RuntimeException('Error creating output file.');
145        }
146
147        $uglifiedJs = file_get_contents($output);
148        unlink($output);
149
150        $asset->setContent($uglifiedJs);
151    }
152}
Note: See TracBrowser for help on using the repository browser.