CompassFilter.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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;
  11. use Assetic\Asset\AssetInterface;
  12. use Assetic\Filter\FilterInterface;
  13. use Assetic\Util\ProcessBuilder;
  14. /**
  15. * Loads Compass files.
  16. *
  17. * @author Maxime Thirouin <maxime.thirouin@gmail.com>
  18. */
  19. class CompassFilter implements FilterInterface
  20. {
  21. private $compassPath;
  22. private $scss;
  23. // sass options
  24. private $unixNewlines;
  25. private $debugInfo;
  26. private $cacheLocation;
  27. private $noCache;
  28. // compass options
  29. private $force;
  30. private $style;
  31. private $quiet;
  32. private $boring;
  33. private $noLineComments;
  34. private $imagesDir;
  35. private $javascriptsDir;
  36. // compass configuration file options
  37. private $plugins = array();
  38. private $loadPaths = array();
  39. private $httpPath;
  40. private $httpImagesPath;
  41. private $httpJavascriptsPath;
  42. public function __construct($compassPath = '/usr/bin/compass')
  43. {
  44. $this->compassPath = $compassPath;
  45. $this->cacheLocation = sys_get_temp_dir();
  46. if ('cli' !== php_sapi_name()) {
  47. $this->boring = true;
  48. }
  49. }
  50. public function setScss($scss)
  51. {
  52. $this->scss = $scss;
  53. }
  54. // sass options setters
  55. public function setUnixNewlines($unixNewlines)
  56. {
  57. $this->unixNewlines = $unixNewlines;
  58. }
  59. public function setDebugInfo($debugInfo)
  60. {
  61. $this->debugInfo = $debugInfo;
  62. }
  63. public function setCacheLocation($cacheLocation)
  64. {
  65. $this->cacheLocation = $cacheLocation;
  66. }
  67. public function setNoCache($noCache)
  68. {
  69. $this->noCache = $noCache;
  70. }
  71. // compass options setters
  72. public function setForce($force)
  73. {
  74. $this->force = $force;
  75. }
  76. public function setStyle($style)
  77. {
  78. $this->style = $style;
  79. }
  80. public function setQuiet($quiet)
  81. {
  82. $this->quiet = $quiet;
  83. }
  84. public function setBoring($boring)
  85. {
  86. $this->boring = $boring;
  87. }
  88. public function setNoLineComments($noLineComments)
  89. {
  90. $this->noLineComments = $noLineComments;
  91. }
  92. public function setImagesDir($imagesDir)
  93. {
  94. $this->imagesDir = $imagesDir;
  95. }
  96. public function setJavascriptsDir($javascriptsDir)
  97. {
  98. $this->javascriptsDir = $javascriptsDir;
  99. }
  100. // compass configuration file options setters
  101. public function setPlugins(array $plugins)
  102. {
  103. $this->plugins = $plugins;
  104. }
  105. public function addPlugin($plugin)
  106. {
  107. $this->plugins[] = $plugin;
  108. }
  109. public function addLoadPath($loadPath)
  110. {
  111. $this->loadPaths[] = $loadPath;
  112. }
  113. public function setHttpPath($httpPath)
  114. {
  115. $this->httpPath = $httpPath;
  116. }
  117. public function setHttpImagesPath($httpImagesPath)
  118. {
  119. $this->httpImagesPath = $httpImagesPath;
  120. }
  121. public function setHttpJavascriptsPath($httpJavascriptsPath)
  122. {
  123. $this->httpJavascriptsPath = $httpJavascriptsPath;
  124. }
  125. public function filterLoad(AssetInterface $asset)
  126. {
  127. $root = $asset->getSourceRoot();
  128. $path = $asset->getSourcePath();
  129. $loadPaths = $this->loadPaths;
  130. if ($root && $path) {
  131. $loadPaths[] = dirname($root.'/'.$path);
  132. }
  133. // compass does not seems to handle symlink, so we use realpath()
  134. $tempDir = realpath(sys_get_temp_dir());
  135. $pb = new ProcessBuilder(array(
  136. $this->compassPath,
  137. 'compile',
  138. $tempDir,
  139. ));
  140. $pb->inheritEnvironmentVariables();
  141. if ($this->force) {
  142. $pb->add('--force');
  143. }
  144. if ($this->style) {
  145. $pb->add('--output-style')->add($this->style);
  146. }
  147. if ($this->quiet) {
  148. $pb->add('--quiet');
  149. }
  150. if ($this->boring) {
  151. $pb->add('--boring');
  152. }
  153. if ($this->noLineComments) {
  154. $pb->add('--no-line-comments');
  155. }
  156. // these two options are not passed into the config file
  157. // because like this, compass adapts this to be xxx_dir or xxx_path
  158. // whether it's an absolute path or not
  159. if ($this->imagesDir) {
  160. $pb->add('--images-dir')->add($this->imagesDir);
  161. }
  162. if ($this->javascriptsDir) {
  163. $pb->add('--javascripts-dir')->add($this->javascriptsDir);
  164. }
  165. // options in config file
  166. $optionsConfig = array();
  167. if (!empty($loadPaths)) {
  168. $optionsConfig['additional_import_paths'] = $loadPaths;
  169. }
  170. if ($this->unixNewlines) {
  171. $optionsConfig['sass_options']['unix_newlines'] = true;
  172. }
  173. if ($this->debugInfo) {
  174. $optionsConfig['sass_options']['debug_info'] = true;
  175. }
  176. if ($this->cacheLocation) {
  177. $optionsConfig['sass_options']['cache_location'] = $this->cacheLocation;
  178. }
  179. if ($this->noCache) {
  180. $optionsConfig['sass_options']['no_cache'] = true;
  181. }
  182. if ($this->httpPath) {
  183. $optionsConfig['http_path'] = $this->httpPath;
  184. }
  185. if ($this->httpImagesPath) {
  186. $optionsConfig['http_images_path'] = $this->httpImagesPath;
  187. }
  188. if ($this->httpJavascriptsPath) {
  189. $optionsConfig['http_javascripts_path'] = $this->httpJavascriptsPath;
  190. }
  191. // options in configuration file
  192. if (count($optionsConfig)) {
  193. $config = array();
  194. foreach ($this->plugins as $plugin) {
  195. $config[] = sprintf("require '%s'", addcslashes($plugin, '\\'));
  196. }
  197. foreach ($optionsConfig as $name => $value) {
  198. if (!is_array($value)) {
  199. $config[] = sprintf('%s = "%s"', $name, addcslashes($value, '\\'));
  200. } elseif (!empty($value)) {
  201. $config[] = sprintf('%s = %s', $name, $this->formatArrayToRuby($value));
  202. }
  203. }
  204. $configFile = tempnam($tempDir, 'assetic_compass');
  205. file_put_contents($configFile, implode("\n", $config)."\n");
  206. $pb->add('--config')->add($configFile);
  207. }
  208. $pb->add('--sass-dir')->add('')->add('--css-dir')->add('');
  209. // compass choose the type (sass or scss from the filename)
  210. if (null !== $this->scss) {
  211. $type = $this->scss ? 'scss' : 'sass';
  212. } elseif ($path) {
  213. // FIXME: what if the extension is something else?
  214. $type = pathinfo($path, PATHINFO_EXTENSION);
  215. } else {
  216. $type = 'scss';
  217. }
  218. $tempName = tempnam($tempDir, 'assetic_compass');
  219. unlink($tempName); // FIXME: don't use tempnam() here
  220. // input
  221. $pb->add($input = $tempName.'.'.$type);
  222. file_put_contents($input, $asset->getContent());
  223. // output
  224. $output = $tempName.'.css';
  225. // it's not really usefull but... https://github.com/chriseppstein/compass/issues/376
  226. $pb->setEnv('HOME', sys_get_temp_dir());
  227. $proc = $pb->getProcess();
  228. $code = $proc->run();
  229. if (0 < $code) {
  230. unlink($input);
  231. if (isset($configFile)) {
  232. unlink($configFile);
  233. }
  234. throw new \RuntimeException($proc->getErrorOutput().'...'.$proc->getOutput());
  235. }
  236. $asset->setContent(file_get_contents($output));
  237. unlink($input);
  238. unlink($output);
  239. if (isset($configFile)) {
  240. unlink($configFile);
  241. }
  242. }
  243. public function filterDump(AssetInterface $asset)
  244. {
  245. }
  246. private function formatArrayToRuby($array)
  247. {
  248. $output = array();
  249. // does we have an associative array ?
  250. if (count(array_filter(array_keys($array), "is_numeric")) != count($array)) {
  251. foreach($array as $name => $value) {
  252. $output[] = sprintf(' :%s => "%s"', $name, addcslashes($value, '\\'));
  253. }
  254. $output = "{\n".implode(",\n", $output)."\n}";
  255. } else {
  256. foreach($array as $name => $value) {
  257. $output[] = sprintf(' "%s"', addcslashes($value, '\\'));
  258. }
  259. $output = "[\n".implode(",\n", $output)."\n]";
  260. }
  261. return $output;
  262. }
  263. }