SassFilter.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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(array($this->sassPath));
  84. $root = $asset->getSourceRoot();
  85. $path = $asset->getSourcePath();
  86. if ($root && $path) {
  87. $pb->add('--load-path')->add(dirname($root.'/'.$path));
  88. }
  89. if ($this->unixNewlines) {
  90. $pb->add('--unix-newlines');
  91. }
  92. if (true === $this->scss || (null === $this->scss && 'scss' == pathinfo($path, PATHINFO_EXTENSION))) {
  93. $pb->add('--scss');
  94. }
  95. if ($this->style) {
  96. $pb->add('--style')->add($this->style);
  97. }
  98. if ($this->quiet) {
  99. $pb->add('--quiet');
  100. }
  101. if ($this->debugInfo) {
  102. $pb->add('--debug-info');
  103. }
  104. if ($this->lineNumbers) {
  105. $pb->add('--line-numbers');
  106. }
  107. foreach ($this->loadPaths as $loadPath) {
  108. $pb->add('--load-path')->add($loadPath);
  109. }
  110. if ($this->cacheLocation) {
  111. $pb->add('--cache-location')->add($this->cacheLocation);
  112. }
  113. if ($this->noCache) {
  114. $pb->add('--no-cache');
  115. }
  116. if ($this->compass) {
  117. $pb->add('--compass');
  118. }
  119. // input
  120. $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_sass'));
  121. file_put_contents($input, $asset->getContent());
  122. $proc = $pb->getProcess();
  123. $code = $proc->run();
  124. unlink($input);
  125. if (0 < $code) {
  126. throw new \RuntimeException($proc->getErrorOutput());
  127. }
  128. $asset->setContent($proc->getOutput());
  129. }
  130. public function filterDump(AssetInterface $asset)
  131. {
  132. }
  133. }