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 12namespace Assetic\Filter; 13 14use Assetic\Asset\AssetInterface; 15use Assetic\Exception\FilterException; 16use Assetic\Util\FilesystemUtils; 17 18/** 19 * Compiles CoffeeScript into Javascript. 20 * 21 * @link http://coffeescript.org/ 22 * @author Kris Wallsmith <kris.wallsmith@gmail.com> 23 */ 24class CoffeeScriptFilter extends BaseNodeFilter 25{ 26 private $coffeeBin; 27 private $nodeBin; 28 29 // coffee options 30 private $bare; 31 private $noHeader; 32 33 public function __construct($coffeeBin = '/usr/bin/coffee', $nodeBin = null) 34 { 35 $this->coffeeBin = $coffeeBin; 36 $this->nodeBin = $nodeBin; 37 } 38 39 public function setBare($bare) 40 { 41 $this->bare = $bare; 42 } 43 44 public function setNoHeader($noHeader) 45 { 46 $this->noHeader = $noHeader; 47 } 48 49 public function filterLoad(AssetInterface $asset) 50 { 51 $input = FilesystemUtils::createTemporaryFile('coffee'); 52 file_put_contents($input, $asset->getContent()); 53 54 $pb = $this->createProcessBuilder($this->nodeBin 55 ? array($this->nodeBin, $this->coffeeBin) 56 : array($this->coffeeBin)); 57 58 $pb->add('-cp'); 59 60 if ($this->bare) { 61 $pb->add('--bare'); 62 } 63 64 if ($this->noHeader) { 65 $pb->add('--no-header'); 66 } 67 68 $pb->add($input); 69 $proc = $pb->getProcess(); 70 $code = $proc->run(); 71 unlink($input); 72 73 if (0 !== $code) { 74 throw FilterException::fromProcess($proc)->setInput($asset->getContent()); 75 } 76 77 $asset->setContent($proc->getOutput()); 78 } 79 80 public function filterDump(AssetInterface $asset) 81 { 82 } 83} 84