LessFilter.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * Loads LESS files.
  15. *
  16. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  17. */
  18. class LessFilter implements FilterInterface
  19. {
  20. private $nodeBin;
  21. private $nodePaths;
  22. private $compress;
  23. /**
  24. * Constructor.
  25. *
  26. * @param string $nodeBin The path to the node binary
  27. * @param array $nodePaths An array of node paths
  28. */
  29. public function __construct($nodeBin = '/usr/bin/node', array $nodePaths = array())
  30. {
  31. $this->nodeBin = $nodeBin;
  32. $this->nodePaths = $nodePaths;
  33. }
  34. public function setCompress($compress)
  35. {
  36. $this->compress = $compress;
  37. }
  38. public function filterLoad(AssetInterface $asset)
  39. {
  40. static $format = <<<'EOF'
  41. var less = require('less');
  42. var sys = require(process.binding('natives').util ? 'util' : 'sys');
  43. new(less.Parser)(%s).parse(%s, function(e, tree) {
  44. if (e) {
  45. less.writeError(e);
  46. process.exit(2);
  47. }
  48. try {
  49. sys.print(tree.toCSS(%s));
  50. } catch (e) {
  51. less.writeError(e);
  52. process.exit(3);
  53. }
  54. });
  55. EOF;
  56. $root = $asset->getSourceRoot();
  57. $path = $asset->getSourcePath();
  58. // parser options
  59. $parserOptions = array();
  60. if ($root && $path) {
  61. $parserOptions['paths'] = array(dirname($root.'/'.$path));
  62. $parserOptions['filename'] = basename($path);
  63. }
  64. // tree options
  65. $treeOptions = array();
  66. if (null !== $this->compress) {
  67. $treeOptions['compress'] = $this->compress;
  68. }
  69. $pb = new ProcessBuilder();
  70. $pb->inheritEnvironmentVariables();
  71. // node.js configuration
  72. if (0 < count($this->nodePaths)) {
  73. $pb->setEnv('NODE_PATH', implode(':', $this->nodePaths));
  74. }
  75. $pb->add($this->nodeBin)->add($input = tempnam(sys_get_temp_dir(), 'assetic_less'));
  76. file_put_contents($input, sprintf($format,
  77. json_encode($parserOptions),
  78. json_encode($asset->getContent()),
  79. json_encode($treeOptions)
  80. ));
  81. $proc = $pb->getProcess();
  82. $code = $proc->run();
  83. unlink($input);
  84. if (0 < $code) {
  85. throw new \RuntimeException($proc->getErrorOutput());
  86. }
  87. $asset->setContent($proc->getOutput());
  88. }
  89. public function filterDump(AssetInterface $asset)
  90. {
  91. }
  92. }