ILIAS  release_5-2 Revision v5.2.25-18-g3f80b82851
UglifyJsFilter.php
Go to the documentation of this file.
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 
12 namespace Assetic\Filter;
13 
17 
25 {
26  private $uglifyjsBin;
27  private $nodeBin;
28 
29  private $noCopyright;
30  private $beautify;
31  private $unsafe;
32  private $mangle;
33  private $defines;
34 
39  public function __construct($uglifyjsBin = '/usr/bin/uglifyjs', $nodeBin = null)
40  {
41  $this->uglifyjsBin = $uglifyjsBin;
42  $this->nodeBin = $nodeBin;
43  }
44 
49  public function setNoCopyright($noCopyright)
50  {
51  $this->noCopyright = $noCopyright;
52  }
53 
58  public function setBeautify($beautify)
59  {
60  $this->beautify = $beautify;
61  }
62 
67  public function setUnsafe($unsafe)
68  {
69  $this->unsafe = $unsafe;
70  }
71 
76  public function setMangle($mangle)
77  {
78  $this->mangle = $mangle;
79  }
80 
81  public function setDefines(array $defines)
82  {
83  $this->defines = $defines;
84  }
85 
89  public function filterLoad(AssetInterface $asset)
90  {
91  }
92 
98  public function filterDump(AssetInterface $asset)
99  {
100  $pb = $this->createProcessBuilder(
101  $this->nodeBin
102  ? array($this->nodeBin, $this->uglifyjsBin)
103  : array($this->uglifyjsBin)
104  );
105 
106  if ($this->noCopyright) {
107  $pb->add('--no-copyright');
108  }
109 
110  if ($this->beautify) {
111  $pb->add('--beautify');
112  }
113 
114  if ($this->unsafe) {
115  $pb->add('--unsafe');
116  }
117 
118  if (false === $this->mangle) {
119  $pb->add('--no-mangle');
120  }
121 
122  if ($this->defines) {
123  foreach ($this->defines as $define) {
124  $pb->add('-d')->add($define);
125  }
126  }
127 
128  // input and output files
129  $input = FilesystemUtils::createTemporaryFile('uglifyjs_in');
131 
132  file_put_contents($input, $asset->getContent());
133  $pb->add('-o')->add($output)->add($input);
134 
135  $proc = $pb->getProcess();
136  $code = $proc->run();
137  unlink($input);
138 
139  if (0 !== $code) {
140  if (file_exists($output)) {
141  unlink($output);
142  }
143 
144  if (127 === $code) {
145  throw new \RuntimeException('Path to node executable could not be resolved.');
146  }
147 
148  throw FilterException::fromProcess($proc)->setInput($asset->getContent());
149  }
150 
151  if (!file_exists($output)) {
152  throw new \RuntimeException('Error creating output file.');
153  }
154 
155  $uglifiedJs = file_get_contents($output);
156  unlink($output);
157 
158  $asset->setContent($uglifiedJs);
159  }
160 }
static createTemporaryFile($prefix)
Creates a temporary file.
setNoCopyright($noCopyright)
Removes the first block of comments as well.
static fromProcess(Process $proc)
setUnsafe($unsafe)
Enable additional optimizations that are known to be unsafe in some situations.
$code
Definition: example_050.php:99
filterDump(AssetInterface $asset)
Run the asset through UglifyJs.
createProcessBuilder(array $arguments=array())
getContent()
Returns the loaded content of the current asset.
if(!is_dir( $entity_dir)) exit("Fatal Error ([A-Za-z0-9]+)\+" &#(? foreach( $entity_files as $file) $output
An asset has a mutable URL and content and can be loaded and dumped.
Create styles array
The data for the language used.
__construct($uglifyjsBin='/usr/bin/uglifyjs', $nodeBin=null)
filterLoad(AssetInterface $asset)
setBeautify($beautify)
Output indented code.
setContent($content)
Sets the content of the current asset.
setMangle($mangle)
Safely mangle variable and function names for greater file compress.