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