sort.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. error_reporting(E_ALL);
  3. require realpath(dirname(__FILE__)).'/../lessc.inc.php';
  4. // sorts the selectors in stylesheet in order to normalize it for comparison
  5. $exe = array_shift($argv); // remove filename
  6. if (!$fname = array_shift($argv)) {
  7. $fname = "php://stdin";
  8. }
  9. class lesscNormalized extends lessc {
  10. public $numberPrecision = 3;
  11. public function compileValue($value) {
  12. if ($value[0] == "raw_color") {
  13. $value = $this->coerceColor($value);
  14. }
  15. return parent::compileValue($value);
  16. }
  17. }
  18. class SortingFormatter extends lessc_formatter_lessjs {
  19. function sortKey($block) {
  20. if (!isset($block->sortKey)) {
  21. sort($block->selectors, SORT_STRING);
  22. $block->sortKey = implode(",", $block->selectors);
  23. }
  24. return $block->sortKey;
  25. }
  26. function sortBlock($block) {
  27. usort($block->children, function($a, $b) {
  28. $sort = strcmp($this->sortKey($a), $this->sortKey($b));
  29. if ($sort == 0) {
  30. // TODO
  31. }
  32. return $sort;
  33. });
  34. }
  35. function block($block) {
  36. $this->sortBlock($block);
  37. return parent::block($block);
  38. }
  39. }
  40. $less = new lesscNormalized();
  41. $less->setFormatter(new SortingFormatter);
  42. echo $less->parse(file_get_contents($fname));