~ubuntu-branches/debian/stretch/assetic/stretch

« back to all changes in this revision

Viewing changes to src/Assetic/Filter/UglifyJs2Filter.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-04-21 12:48:05 UTC
  • Revision ID: package-import@ubuntu.com-20140421124805-y9ri97838g33fo9z
Tags: upstream-1.1.2
Import upstream version 1.1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/*
 
4
 * This file is part of the Assetic package, an OpenSky project.
 
5
 *
 
6
 * (c) 2010-2013 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;
 
13
 
 
14
use Assetic\Asset\AssetInterface;
 
15
use Assetic\Exception\FilterException;
 
16
 
 
17
/**
 
18
 * UglifyJs2 filter.
 
19
 *
 
20
 * @link http://lisperator.net/uglifyjs
 
21
 * @author Kris Wallsmith <kris.wallsmith@gmail.com>
 
22
 */
 
23
class UglifyJs2Filter extends BaseNodeFilter
 
24
{
 
25
    private $uglifyjsBin;
 
26
    private $nodeBin;
 
27
    private $compress;
 
28
    private $beautify;
 
29
    private $mangle;
 
30
    private $screwIe8;
 
31
    private $comments;
 
32
    private $wrap;
 
33
 
 
34
    public function __construct($uglifyjsBin = '/usr/bin/uglifyjs', $nodeBin = null)
 
35
    {
 
36
        $this->uglifyjsBin = $uglifyjsBin;
 
37
        $this->nodeBin = $nodeBin;
 
38
    }
 
39
 
 
40
    public function setCompress($compress)
 
41
    {
 
42
        $this->compress = $compress;
 
43
    }
 
44
 
 
45
    public function setBeautify($beautify)
 
46
    {
 
47
        $this->beautify = $beautify;
 
48
    }
 
49
 
 
50
    public function setMangle($mangle)
 
51
    {
 
52
        $this->mangle = $mangle;
 
53
    }
 
54
 
 
55
    public function setScrewIe8($screwIe8)
 
56
    {
 
57
        $this->screwIe8 = $screwIe8;
 
58
    }
 
59
 
 
60
    public function setComments($comments)
 
61
    {
 
62
        $this->comments = $comments;
 
63
    }
 
64
    
 
65
    public function setWrap($wrap)
 
66
    {
 
67
        $this->wrap = $wrap;
 
68
    }
 
69
 
 
70
    public function filterLoad(AssetInterface $asset)
 
71
    {
 
72
    }
 
73
 
 
74
    public function filterDump(AssetInterface $asset)
 
75
    {
 
76
        $pb = $this->createProcessBuilder($this->nodeBin
 
77
            ? array($this->nodeBin, $this->uglifyjsBin)
 
78
            : array($this->uglifyjsBin));
 
79
 
 
80
        if ($this->compress) {
 
81
            $pb->add('--compress');
 
82
        }
 
83
 
 
84
        if ($this->beautify) {
 
85
            $pb->add('--beautify');
 
86
        }
 
87
 
 
88
        if ($this->mangle) {
 
89
            $pb->add('--mangle');
 
90
        }
 
91
 
 
92
        if ($this->screwIe8) {
 
93
            $pb->add('--screw-ie8');
 
94
        }
 
95
 
 
96
        if ($this->comments) {
 
97
            $pb->add('--comments')->add(true === $this->comments ? 'all' : $this->comments);
 
98
        }
 
99
        
 
100
        if ($this->wrap) {
 
101
            $pb->add('--wrap')->add($this->wrap);
 
102
        }
 
103
 
 
104
        // input and output files
 
105
        $input = tempnam(sys_get_temp_dir(), 'input');
 
106
        $output = tempnam(sys_get_temp_dir(), 'output');
 
107
 
 
108
        file_put_contents($input, $asset->getContent());
 
109
        $pb->add('-o')->add($output)->add($input);
 
110
 
 
111
        $proc = $pb->getProcess();
 
112
        $code = $proc->run();
 
113
        unlink($input);
 
114
 
 
115
        if (0 !== $code) {
 
116
            if (file_exists($output)) {
 
117
                unlink($output);
 
118
            }
 
119
 
 
120
            if (127 === $code) {
 
121
                throw new \RuntimeException('Path to node executable could not be resolved.');
 
122
            }
 
123
 
 
124
            throw FilterException::fromProcess($proc)->setInput($asset->getContent());
 
125
        }
 
126
 
 
127
        if (!file_exists($output)) {
 
128
            throw new \RuntimeException('Error creating output file.');
 
129
        }
 
130
 
 
131
        $asset->setContent(file_get_contents($output));
 
132
 
 
133
        unlink($output);
 
134
    }
 
135
}