class.lessjs.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * @file
  4. * Contains 'lessjs' class; an abstraction layer for command line less.js.
  5. */
  6. /**
  7. * 'lessjs' class.
  8. */
  9. class Lessjs {
  10. /**
  11. * Base command is hardcoded here to reduce security vulnerability.
  12. *
  13. * @var string
  14. */
  15. const BASE_COMMAND = 'lessc';
  16. /**
  17. * Path to .less input file.
  18. *
  19. * @var null|string
  20. */
  21. protected $input_file;
  22. /**
  23. * @var string[] $include_paths
  24. *
  25. * @link http://lesscss.org/usage/#command-line-usage-include-paths
  26. */
  27. protected $include_paths = array();
  28. /**
  29. * @var string[] $modify_variables
  30. *
  31. * @link http://lesscss.org/usage/#command-line-usage-modify-variable
  32. */
  33. protected $modify_variables = array();
  34. protected $source_maps_enabled = FALSE;
  35. /**
  36. * @var null|string $source_map_rootpath
  37. *
  38. * @link http://lesscss.org/usage/#command-line-usage-source-map-rootpath
  39. */
  40. protected $source_map_rootpath = NULL;
  41. /**
  42. * @var null|string $source_map_basepath
  43. *
  44. * @link http://lesscss.org/usage/#command-line-usage-source-map-basepath
  45. */
  46. protected $source_map_basepath = NULL;
  47. /**
  48. * Constructor function for 'lessjs'.
  49. *
  50. * @param string $input_file
  51. * Path for .less file relative to getcwd().
  52. */
  53. private function __construct($input_file) {
  54. $this->input_file = $input_file;
  55. }
  56. public static function create($input_file = NULL) {
  57. return new self($input_file);
  58. }
  59. /**
  60. * Returns the version string from command line less.js.
  61. *
  62. * @return string|null
  63. * Version string from less.js, or null if no version found.
  64. */
  65. public static function version() {
  66. $version = NULL;
  67. if (function_exists('proc_open')) {
  68. try {
  69. $version_response = self::create(NULL)->proc_open(array('--version'));
  70. $version = preg_replace('/.*?([\d\.]+).*/', '$1', $version_response);
  71. }
  72. catch (Exception $e) {
  73. }
  74. }
  75. return $version;
  76. }
  77. /**
  78. * Add include path that will be set with '--include-path' argument.
  79. *
  80. * @link http://lesscss.org/usage/#command-line-usage-include-paths
  81. *
  82. * @param string $include_path
  83. * Path relative to getcwd().
  84. */
  85. public function include_path($include_path) {
  86. $this->include_paths[] = $include_path;
  87. }
  88. /**
  89. * Add LESS variable that will be set with the '--modify-var' argument.
  90. *
  91. * @param string $variable_name
  92. * The variable name.
  93. * @param string $variable_value
  94. * The variable value.
  95. */
  96. public function modify_var($variable_name, $variable_value) {
  97. $this->modify_variables[$variable_name] = $variable_value;
  98. }
  99. /**
  100. * Enable source maps for current file, and configure source map paths.
  101. *
  102. * @param bool $enabled
  103. * Set the source maps flag.
  104. * @param string $base_path
  105. * Leading value to be stripped from each source map URL.
  106. * @param string $root_path
  107. * Value to be prepended to each source map URL.
  108. *
  109. * @link http://lesscss.org/usage/#command-line-usage-source-map-rootpath
  110. * @link http://lesscss.org/usage/#command-line-usage-source-map-basepath
  111. */
  112. public function source_maps($enabled, $base_path = NULL, $root_path = NULL) {
  113. $this->source_maps_enabled = $enabled;
  114. $this->source_map_basepath = $base_path;
  115. $this->source_map_rootpath = $root_path;
  116. }
  117. /**
  118. * Provides list to command line arguments for execution.
  119. *
  120. * @return string[]
  121. * Array of command line arguments.
  122. */
  123. private function command_arguments() {
  124. $arguments = array();
  125. // Add include paths.
  126. if (count($this->include_paths) > 0) {
  127. $arguments[] = '--include-path=' . implode(PATH_SEPARATOR, array_map('escapeshellarg', $this->include_paths));
  128. // @link http://lesscss.org/usage/#command-line-usage-relative-urls
  129. $arguments[] = '--relative-urls';
  130. }
  131. // Add any defined variables.
  132. foreach ($this->modify_variables as $modify_variable_name => $modify_variable_value) {
  133. /**
  134. * @link http://lesscss.org/usage/#command-line-usage-modify-variable
  135. */
  136. $arguments[] = '--modify-var=' . escapeshellarg($modify_variable_name . '=' . $modify_variable_value);
  137. }
  138. // Set source map flags.
  139. if ($this->source_maps_enabled) {
  140. if (isset($this->source_map_rootpath)) {
  141. $arguments[] = '--source-map-rootpath=' . escapeshellarg($this->source_map_rootpath);
  142. }
  143. if (isset($this->source_map_basepath)) {
  144. $arguments[] = '--source-map-basepath=' . escapeshellarg($this->source_map_basepath);
  145. }
  146. /**
  147. * @link http://lesscss.org/usage/#command-line-usage-source-map-map-inline
  148. */
  149. $arguments[] = '--source-map-map-inline';
  150. }
  151. // Input file should be last argument.
  152. // @link http://lesscss.org/usage/#command-line-usage-command-line-usage
  153. $arguments[] = $this->input_file;
  154. return $arguments;
  155. }
  156. /**
  157. * Returns list of files that input file depends on.
  158. *
  159. * @return string[]
  160. * List of @import'ed files.
  161. */
  162. public function depends() {
  163. $output_key = 'depends';
  164. $depends_arguments = array();
  165. $depends_arguments[] = '--depends';
  166. $depends_arguments[] = drupal_realpath(LESS_DIRECTORY) . DIRECTORY_SEPARATOR . $output_key;
  167. $depends_files_spaced = $this->proc_open(array_merge($this->command_arguments(), $depends_arguments));
  168. // {$output_key}: /path/to/file/1 /path/to/file/2
  169. $depends_files_spaced = str_replace($output_key . ':', '', $depends_files_spaced);
  170. return explode(' ', trim($depends_files_spaced));
  171. }
  172. /**
  173. * Executes compilation of LESS input.
  174. *
  175. * @return string
  176. * Compiled CSS.
  177. */
  178. public function compile() {
  179. return $this->proc_open($this->command_arguments());
  180. }
  181. /**
  182. * Execute compilation command through proc_open().
  183. *
  184. * @param string[] $command_arguments
  185. *
  186. * @return null|string
  187. * @throws Exception
  188. *
  189. * @see proc_open()
  190. */
  191. private function proc_open(array $command_arguments = array()) {
  192. $output_data = NULL;
  193. $command = implode(' ', array_merge(array(self::BASE_COMMAND), $command_arguments));
  194. // Handles for data exchange.
  195. $pipes = array(
  196. 0 => NULL, // STDIN
  197. 1 => NULL, // STDOUT
  198. 2 => NULL, // STDERR
  199. );
  200. // Sets permissions on $pipes.
  201. $descriptors = array(
  202. 0 => array('pipe', 'r'), // STDIN
  203. 1 => array('pipe', 'w'), // STDOUT
  204. 2 => array('pipe', 'w'), // STDERR
  205. );
  206. try {
  207. $process = proc_open($command, $descriptors, $pipes);
  208. if (is_resource($process)) {
  209. fclose($pipes[0]); // fclose() on STDIN executes $command, if program is expecting input from STDIN.
  210. $output_data = stream_get_contents($pipes[1]);
  211. fclose($pipes[1]);
  212. $error = stream_get_contents($pipes[2]);
  213. fclose($pipes[2]);
  214. if (!empty($error)) {
  215. throw new Exception($error);
  216. }
  217. proc_close($process);
  218. }
  219. }
  220. catch (Exception $e) {
  221. throw $e;
  222. }
  223. return $output_data;
  224. }
  225. }