class.lessjs.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. try {
  68. $version_response = self::create(NULL)->proc_open(array('--version'));
  69. $version = preg_replace('/.*?([\d\.]+).*/', '$1', $version_response);
  70. }
  71. catch (Exception $e) {
  72. }
  73. return $version;
  74. }
  75. /**
  76. * Add include path that will be set with '--include-path' argument.
  77. *
  78. * @link http://lesscss.org/usage/#command-line-usage-include-paths
  79. *
  80. * @param string $include_path
  81. * Path relative to getcwd().
  82. */
  83. public function include_path($include_path) {
  84. $this->include_paths[] = $include_path;
  85. }
  86. /**
  87. * Add LESS variable that will be set with the '--modify-var' argument.
  88. *
  89. * @param string $variable_name
  90. * The variable name.
  91. * @param string $variable_value
  92. * The variable value.
  93. */
  94. public function modify_var($variable_name, $variable_value) {
  95. $this->modify_variables[$variable_name] = $variable_value;
  96. }
  97. /**
  98. * Enable source maps for current file, and configure source map paths.
  99. *
  100. * @param bool $enabled
  101. * Set the source maps flag.
  102. * @param string $base_path
  103. * Leading value to be stripped from each source map URL.
  104. * @param string $root_path
  105. * Value to be prepended to each source map URL.
  106. *
  107. * @link http://lesscss.org/usage/#command-line-usage-source-map-rootpath
  108. * @link http://lesscss.org/usage/#command-line-usage-source-map-basepath
  109. */
  110. public function source_maps($enabled, $base_path = NULL, $root_path = NULL) {
  111. $this->source_maps_enabled = $enabled;
  112. $this->source_map_basepath = $base_path;
  113. $this->source_map_rootpath = $root_path;
  114. }
  115. /**
  116. * Provides list to command line arguments for execution.
  117. *
  118. * @return string[]
  119. * Array of command line arguments.
  120. */
  121. private function command_arguments() {
  122. $arguments = array();
  123. // Add include paths.
  124. if (count($this->include_paths) > 0) {
  125. $arguments[] = '--include-path=' . implode(PATH_SEPARATOR, array_map('escapeshellarg', $this->include_paths));
  126. // @link http://lesscss.org/usage/#command-line-usage-relative-urls
  127. $arguments[] = '--relative-urls';
  128. }
  129. // Add any defined variables.
  130. foreach ($this->modify_variables as $modify_variable_name => $modify_variable_value) {
  131. /**
  132. * @link http://lesscss.org/usage/#command-line-usage-modify-variable
  133. */
  134. $arguments[] = '--modify-var=' . escapeshellarg($modify_variable_name . '=' . $modify_variable_value);
  135. }
  136. // Set source map flags.
  137. if ($this->source_maps_enabled) {
  138. if (isset($this->source_map_rootpath)) {
  139. $arguments[] = '--source-map-rootpath=' . escapeshellarg($this->source_map_rootpath);
  140. }
  141. if (isset($this->source_map_basepath)) {
  142. $arguments[] = '--source-map-basepath=' . escapeshellarg($this->source_map_basepath);
  143. }
  144. /**
  145. * @link http://lesscss.org/usage/#command-line-usage-source-map-map-inline
  146. */
  147. $arguments[] = '--source-map-map-inline';
  148. }
  149. // Input file should be last argument.
  150. // @link http://lesscss.org/usage/#command-line-usage-command-line-usage
  151. $arguments[] = $this->input_file;
  152. return $arguments;
  153. }
  154. /**
  155. * Returns list of files that input file depends on.
  156. *
  157. * @return string[]
  158. * List of @import'ed files.
  159. */
  160. public function depends() {
  161. $output_key = 'depends';
  162. $depends_arguments = array();
  163. $depends_arguments[] = '--depends';
  164. $depends_arguments[] = drupal_realpath(LESS_DIRECTORY) . DIRECTORY_SEPARATOR . $output_key;
  165. $depends_files_spaced = $this->proc_open(array_merge($this->command_arguments(), $depends_arguments));
  166. // {$output_key}: /path/to/file/1 /path/to/file/2
  167. $depends_files_spaced = str_replace($output_key . ':', '', $depends_files_spaced);
  168. return explode(' ', trim($depends_files_spaced));
  169. }
  170. /**
  171. * Executes compilation of LESS input.
  172. *
  173. * @return string
  174. * Compiled CSS.
  175. */
  176. public function compile() {
  177. return $this->proc_open($this->command_arguments());
  178. }
  179. /**
  180. * Execute compilation command through proc_open().
  181. *
  182. * @param string[] $command_arguments
  183. *
  184. * @return null|string
  185. * @throws Exception
  186. *
  187. * @see proc_open()
  188. */
  189. private function proc_open(array $command_arguments = array()) {
  190. $output_data = NULL;
  191. $command = implode(' ', array_merge(array(self::BASE_COMMAND), $command_arguments));
  192. // Handles for data exchange.
  193. $pipes = array(
  194. 0 => NULL, // STDIN
  195. 1 => NULL, // STDOUT
  196. 2 => NULL, // STDERR
  197. );
  198. // Sets permissions on $pipes.
  199. $descriptors = array(
  200. 0 => array('pipe', 'r'), // STDIN
  201. 1 => array('pipe', 'w'), // STDOUT
  202. 2 => array('pipe', 'w'), // STDERR
  203. );
  204. try {
  205. $process = proc_open($command, $descriptors, $pipes);
  206. if (is_resource($process)) {
  207. fclose($pipes[0]); // fclose() on STDIN executes $command, if program is expecting input from STDIN.
  208. $output_data = stream_get_contents($pipes[1]);
  209. fclose($pipes[1]);
  210. $error = stream_get_contents($pipes[2]);
  211. fclose($pipes[2]);
  212. if (!empty($error)) {
  213. throw new Exception($error);
  214. }
  215. proc_close($process);
  216. }
  217. }
  218. catch (Exception $e) {
  219. throw $e;
  220. }
  221. return $output_data;
  222. }
  223. }