1<?php 2 3namespace Assetic\Filter; 4 5use Assetic\Asset\AssetInterface; 6use Assetic\Exception\FilterException; 7use Assetic\Util\FilesystemUtils; 8 9/** 10 * Compiles JSX (for use with React) into JavaScript. 11 * 12 * @link http://facebook.github.io/react/docs/jsx-in-depth.html 13 * @author Douglas Greenshields <dgreenshields@gmail.com> 14 */ 15class ReactJsxFilter extends BaseNodeFilter 16{ 17 private $jsxBin; 18 private $nodeBin; 19 20 public function __construct($jsxBin = '/usr/bin/jsx', $nodeBin = null) 21 { 22 $this->jsxBin = $jsxBin; 23 $this->nodeBin = $nodeBin; 24 } 25 26 public function filterLoad(AssetInterface $asset) 27 { 28 $builder = $this->createProcessBuilder($this->nodeBin 29 ? array($this->nodeBin, $this->jsxBin) 30 : array($this->jsxBin)); 31 32 $inputDir = FilesystemUtils::createThrowAwayDirectory('jsx_in'); 33 $inputFile = $inputDir.DIRECTORY_SEPARATOR.'asset.js'; 34 $outputDir = FilesystemUtils::createThrowAwayDirectory('jsx_out'); 35 $outputFile = $outputDir.DIRECTORY_SEPARATOR.'asset.js'; 36 37 // create the asset file 38 file_put_contents($inputFile, $asset->getContent()); 39 40 $builder 41 ->add($inputDir) 42 ->add($outputDir) 43 ->add('--no-cache-dir') 44 ; 45 46 $proc = $builder->getProcess(); 47 $code = $proc->run(); 48 49 // remove the input directory and asset file 50 unlink($inputFile); 51 rmdir($inputDir); 52 53 if (0 !== $code) { 54 if (file_exists($outputFile)) { 55 unlink($outputFile); 56 } 57 58 if (file_exists($outputDir)) { 59 rmdir($outputDir); 60 } 61 62 throw FilterException::fromProcess($proc); 63 } 64 65 $asset->setContent(file_get_contents($outputFile)); 66 67 // remove the output directory and processed asset file 68 unlink($outputFile); 69 rmdir($outputDir); 70 } 71 72 public function filterDump(AssetInterface $asset) 73 { 74 } 75} 76