SassFilter.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2011 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\Sass;
  11. use Assetic\Asset\AssetInterface;
  12. use Assetic\Filter\FilterInterface;
  13. use Assetic\Util\ProcessBuilder;
  14. /**
  15. * Loads SASS files.
  16. *
  17. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  18. */
  19. class SassFilter implements FilterInterface
  20. {
  21. const STYLE_NESTED = 'nested';
  22. const STYLE_EXPANDED = 'expanded';
  23. const STYLE_COMPACT = 'compact';
  24. const STYLE_COMPRESSED = 'compressed';
  25. private $sassPath;
  26. private $unixNewlines;
  27. private $scss;
  28. private $style;
  29. private $quiet;
  30. private $debugInfo;
  31. private $lineNumbers;
  32. private $loadPaths = array();
  33. private $cacheLocation;
  34. private $noCache;
  35. private $compass;
  36. public function __construct($sassPath = '/usr/bin/sass')
  37. {
  38. $this->sassPath = $sassPath;
  39. $this->cacheLocation = realpath(sys_get_temp_dir());
  40. }
  41. public function setUnixNewlines($unixNewlines)
  42. {
  43. $this->unixNewlines = $unixNewlines;
  44. }
  45. public function setScss($scss)
  46. {
  47. $this->scss = $scss;
  48. }
  49. public function setStyle($style)
  50. {
  51. $this->style = $style;
  52. }
  53. public function setQuiet($quiet)
  54. {
  55. $this->quiet = $quiet;
  56. }
  57. public function setDebugInfo($debugInfo)
  58. {
  59. $this->debugInfo = $debugInfo;
  60. }
  61. public function setLineNumbers($lineNumbers)
  62. {
  63. $this->lineNumbers = $lineNumbers;
  64. }
  65. public function addLoadPath($loadPath)
  66. {
  67. $this->loadPaths[] = $loadPath;
  68. }
  69. public function setCacheLocation($cacheLocation)
  70. {
  71. $this->cacheLocation = $cacheLocation;
  72. }
  73. public function setNoCache($noCache)
  74. {
  75. $this->noCache = $noCache;
  76. }
  77. public function setCompass($compass)
  78. {
  79. $this->compass = $compass;
  80. }
  81. public function filterLoad(AssetInterface $asset)
  82. {
  83. $pb = new ProcessBuilder();
  84. $pb
  85. ->inheritEnvironmentVariables()
  86. ->add($this->sassPath)
  87. ;
  88. $root = $asset->getSourceRoot();
  89. $path = $asset->getSourcePath();
  90. if ($root && $path) {
  91. $pb->add('--load-path')->add(dirname($root.'/'.$path));
  92. }
  93. if ($this->unixNewlines) {
  94. $pb->add('--unix-newlines');
  95. }
  96. if (true === $this->scss || (null === $this->scss && 'scss' == pathinfo($path, PATHINFO_EXTENSION))) {
  97. $pb->add('--scss');
  98. }
  99. if ($this->style) {
  100. $pb->add('--style')->add($this->style);
  101. }
  102. if ($this->quiet) {
  103. $pb->add('--quiet');
  104. }
  105. if ($this->debugInfo) {
  106. $pb->add('--debug-info');
  107. }
  108. if ($this->lineNumbers) {
  109. $pb->add('--line-numbers');
  110. }
  111. foreach ($this->loadPaths as $loadPath) {
  112. $pb->add('--load-path')->add($loadPath);
  113. }
  114. if ($this->cacheLocation) {
  115. $pb->add('--cache-location')->add($this->cacheLocation);
  116. }
  117. if ($this->noCache) {
  118. $pb->add('--no-cache');
  119. }
  120. if ($this->compass) {
  121. $pb->add('--compass');
  122. }
  123. // input
  124. $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_sass'));
  125. file_put_contents($input, $asset->getContent());
  126. $proc = $pb->getProcess();
  127. $code = $proc->run();
  128. unlink($input);
  129. if (0 < $code) {
  130. throw new \RuntimeException($proc->getErrorOutput());
  131. }
  132. $asset->setContent($proc->getOutput());
  133. }
  134. public function filterDump(AssetInterface $asset)
  135. {
  136. }
  137. }