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 12namespace Assetic\Filter; 13 14use Assetic\Asset\AssetInterface; 15use Assetic\Exception\FilterException; 16use Assetic\Util\FilesystemUtils; 17 18/** 19 * Parses CSS and adds vendor prefixes to rules using values from the Can I Use website 20 * 21 * @link https://github.com/ai/autoprefixer 22 * @author Alex Vasilenko <aa.vasilenko@gmail.com> 23 */ 24class AutoprefixerFilter extends BaseNodeFilter 25{ 26 /** 27 * @var string 28 */ 29 private $autoprefixerBin; 30 31 /** 32 * @var array 33 */ 34 private $browsers = array(); 35 36 public function __construct($autoprefixerBin) 37 { 38 $this->autoprefixerBin = $autoprefixerBin; 39 } 40 41 /** 42 * @param array $browsers 43 */ 44 public function setBrowsers(array $browsers) 45 { 46 $this->browsers = $browsers; 47 } 48 49 /** 50 * @param string $browser 51 */ 52 public function addBrowser($browser) 53 { 54 $this->browsers[] = $browser; 55 } 56 57 public function filterLoad(AssetInterface $asset) 58 { 59 $input = $asset->getContent(); 60 $pb = $this->createProcessBuilder(array($this->autoprefixerBin)); 61 62 $pb->setInput($input); 63 if ($this->browsers) { 64 $pb->add('-b')->add(implode(',', $this->browsers)); 65 } 66 67 $output = FilesystemUtils::createTemporaryFile('autoprefixer'); 68 $pb->add('-o')->add($output); 69 70 $proc = $pb->getProcess(); 71 if (0 !== $proc->run()) { 72 throw FilterException::fromProcess($proc)->setInput($asset->getContent()); 73 } 74 75 $asset->setContent(file_get_contents($output)); 76 unlink($output); 77 } 78 79 /** 80 * Filters an asset just before it's dumped. 81 * 82 * @param AssetInterface $asset An asset 83 */ 84 public function filterDump(AssetInterface $asset) 85 { 86 } 87} 88