Environment.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Environment
  4. *
  5. * @package Less
  6. * @subpackage environment
  7. */
  8. class Less_Environment {
  9. // public $paths = array(); // option - unmodified - paths to search for imports on
  10. //public static $files = array(); // list of files that have been imported, used for import-once
  11. //public $rootpath; // option - rootpath to append to URL's
  12. //public static $strictImports = null; // option -
  13. //public $insecure; // option - whether to allow imports from insecure ssl hosts
  14. //public $processImports; // option - whether to process imports. if false then imports will not be imported
  15. //public $javascriptEnabled; // option - whether JavaScript is enabled. if undefined, defaults to true
  16. //public $useFileCache; // browser only - whether to use the per file session cache
  17. public $currentFileInfo; // information about the current file - for error reporting and importing and making urls relative etc.
  18. public $importMultiple = false; // whether we are currently importing multiple copies
  19. /**
  20. * @var array
  21. */
  22. public $frames = array();
  23. /**
  24. * @var array
  25. */
  26. public $mediaBlocks = array();
  27. /**
  28. * @var array
  29. */
  30. public $mediaPath = array();
  31. public static $parensStack = 0;
  32. public static $tabLevel = 0;
  33. public static $lastRule = false;
  34. public static $_outputMap;
  35. public static $mixin_stack = 0;
  36. /**
  37. * @var array
  38. */
  39. public $functions = array();
  40. public function Init() {
  41. self::$parensStack = 0;
  42. self::$tabLevel = 0;
  43. self::$lastRule = false;
  44. self::$mixin_stack = 0;
  45. if ( Less_Parser::$options['compress'] ) {
  46. Less_Environment::$_outputMap = array(
  47. ',' => ',',
  48. ': ' => ':',
  49. '' => '',
  50. ' ' => ' ',
  51. ':' => ' :',
  52. '+' => '+',
  53. '~' => '~',
  54. '>' => '>',
  55. '|' => '|',
  56. '^' => '^',
  57. '^^' => '^^'
  58. );
  59. } else {
  60. Less_Environment::$_outputMap = array(
  61. ',' => ', ',
  62. ': ' => ': ',
  63. '' => '',
  64. ' ' => ' ',
  65. ':' => ' :',
  66. '+' => ' + ',
  67. '~' => ' ~ ',
  68. '>' => ' > ',
  69. '|' => '|',
  70. '^' => ' ^ ',
  71. '^^' => ' ^^ '
  72. );
  73. }
  74. }
  75. public function copyEvalEnv( $frames = array() ) {
  76. $new_env = new Less_Environment();
  77. $new_env->frames = $frames;
  78. return $new_env;
  79. }
  80. public static function isMathOn() {
  81. return !Less_Parser::$options['strictMath'] || Less_Environment::$parensStack;
  82. }
  83. public static function isPathRelative( $path ) {
  84. return !preg_match( '/^(?:[a-z-]+:|\/)/', $path );
  85. }
  86. /**
  87. * Canonicalize a path by resolving references to '/./', '/../'
  88. * Does not remove leading "../"
  89. * @param string path or url
  90. * @return string Canonicalized path
  91. *
  92. */
  93. public static function normalizePath( $path ) {
  94. $segments = explode( '/', $path );
  95. $segments = array_reverse( $segments );
  96. $path = array();
  97. $path_len = 0;
  98. while ( $segments ) {
  99. $segment = array_pop( $segments );
  100. switch ( $segment ) {
  101. case '.':
  102. break;
  103. case '..':
  104. if ( !$path_len || ( $path[$path_len - 1] === '..' ) ) {
  105. $path[] = $segment;
  106. $path_len++;
  107. } else {
  108. array_pop( $path );
  109. $path_len--;
  110. }
  111. break;
  112. default:
  113. $path[] = $segment;
  114. $path_len++;
  115. break;
  116. }
  117. }
  118. return implode( '/', $path );
  119. }
  120. public function unshiftFrame( $frame ) {
  121. array_unshift( $this->frames, $frame );
  122. }
  123. public function shiftFrame() {
  124. return array_shift( $this->frames );
  125. }
  126. }