4
* This file is part of the Assetic package, an OpenSky project.
6
* (c) 2010-2013 OpenSky Project Inc
8
* For the full copyright and license information, please view the LICENSE
9
* file that was distributed with this source code.
12
namespace Assetic\Filter;
14
use Assetic\Asset\AssetInterface;
15
use Assetic\Exception\FilterException;
20
* @link http://lisperator.net/uglifyjs
21
* @author Kris Wallsmith <kris.wallsmith@gmail.com>
23
class UglifyJs2Filter extends BaseNodeFilter
34
public function __construct($uglifyjsBin = '/usr/bin/uglifyjs', $nodeBin = null)
36
$this->uglifyjsBin = $uglifyjsBin;
37
$this->nodeBin = $nodeBin;
40
public function setCompress($compress)
42
$this->compress = $compress;
45
public function setBeautify($beautify)
47
$this->beautify = $beautify;
50
public function setMangle($mangle)
52
$this->mangle = $mangle;
55
public function setScrewIe8($screwIe8)
57
$this->screwIe8 = $screwIe8;
60
public function setComments($comments)
62
$this->comments = $comments;
65
public function setWrap($wrap)
70
public function filterLoad(AssetInterface $asset)
74
public function filterDump(AssetInterface $asset)
76
$pb = $this->createProcessBuilder($this->nodeBin
77
? array($this->nodeBin, $this->uglifyjsBin)
78
: array($this->uglifyjsBin));
80
if ($this->compress) {
81
$pb->add('--compress');
84
if ($this->beautify) {
85
$pb->add('--beautify');
92
if ($this->screwIe8) {
93
$pb->add('--screw-ie8');
96
if ($this->comments) {
97
$pb->add('--comments')->add(true === $this->comments ? 'all' : $this->comments);
101
$pb->add('--wrap')->add($this->wrap);
104
// input and output files
105
$input = tempnam(sys_get_temp_dir(), 'input');
106
$output = tempnam(sys_get_temp_dir(), 'output');
108
file_put_contents($input, $asset->getContent());
109
$pb->add('-o')->add($output)->add($input);
111
$proc = $pb->getProcess();
112
$code = $proc->run();
116
if (file_exists($output)) {
121
throw new \RuntimeException('Path to node executable could not be resolved.');
124
throw FilterException::fromProcess($proc)->setInput($asset->getContent());
127
if (!file_exists($output)) {
128
throw new \RuntimeException('Error creating output file.');
131
$asset->setContent(file_get_contents($output));