CoffeeScriptFilter.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2012 OpenSky Project Inc
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Assetic\Filter;
  11. use Assetic\Asset\AssetInterface;
  12. use Assetic\Util\ProcessBuilder;
  13. /**
  14. * Compiles CoffeeScript into Javascript.
  15. *
  16. * @link http://jashkenas.github.com/coffee-script/
  17. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  18. */
  19. class CoffeeScriptFilter implements FilterInterface
  20. {
  21. private $coffeePath;
  22. private $nodePath;
  23. public function __construct($coffeePath = '/usr/bin/coffee', $nodePath = '/usr/bin/node')
  24. {
  25. $this->coffeePath = $coffeePath;
  26. $this->nodePath = $nodePath;
  27. }
  28. public function filterLoad(AssetInterface $asset)
  29. {
  30. $input = tempnam(sys_get_temp_dir(), 'assetic_coffeescript');
  31. file_put_contents($input, $asset->getContent());
  32. $pb = new ProcessBuilder(array(
  33. $this->nodePath,
  34. $this->coffeePath,
  35. '-cp',
  36. $input,
  37. ));
  38. $proc = $pb->getProcess();
  39. $code = $proc->run();
  40. unlink($input);
  41. if (0 < $code) {
  42. throw new \RuntimeException($proc->getErrorOutput());
  43. }
  44. $asset->setContent($proc->getOutput());
  45. }
  46. public function filterDump(AssetInterface $asset)
  47. {
  48. }
  49. }