@@ -22,82 +22,82 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class Util |
| 24 | 24 | { |
| 25 | - /** |
|
| 26 | - * Asserts that `value` falls within `range` (inclusive), leaving |
|
| 27 | - * room for slight floating-point errors. |
|
| 28 | - * |
|
| 29 | - * @param string $name The name of the value. Used in the error message. |
|
| 30 | - * @param \ScssPhp\ScssPhp\Base\Range $range Range of values. |
|
| 31 | - * @param array $value The value to check. |
|
| 32 | - * @param string $unit The unit of the value. Used in error reporting. |
|
| 33 | - * |
|
| 34 | - * @return mixed `value` adjusted to fall within range, if it was outside by a floating-point margin. |
|
| 35 | - * |
|
| 36 | - * @throws \ScssPhp\ScssPhp\Exception\RangeException |
|
| 37 | - */ |
|
| 38 | - public static function checkRange($name, Range $range, $value, $unit = '') |
|
| 39 | - { |
|
| 40 | - $val = $value[1]; |
|
| 41 | - $grace = new Range(-0.00001, 0.00001); |
|
| 25 | + /** |
|
| 26 | + * Asserts that `value` falls within `range` (inclusive), leaving |
|
| 27 | + * room for slight floating-point errors. |
|
| 28 | + * |
|
| 29 | + * @param string $name The name of the value. Used in the error message. |
|
| 30 | + * @param \ScssPhp\ScssPhp\Base\Range $range Range of values. |
|
| 31 | + * @param array $value The value to check. |
|
| 32 | + * @param string $unit The unit of the value. Used in error reporting. |
|
| 33 | + * |
|
| 34 | + * @return mixed `value` adjusted to fall within range, if it was outside by a floating-point margin. |
|
| 35 | + * |
|
| 36 | + * @throws \ScssPhp\ScssPhp\Exception\RangeException |
|
| 37 | + */ |
|
| 38 | + public static function checkRange($name, Range $range, $value, $unit = '') |
|
| 39 | + { |
|
| 40 | + $val = $value[1]; |
|
| 41 | + $grace = new Range(-0.00001, 0.00001); |
|
| 42 | 42 | |
| 43 | - if (! \is_numeric($val)) { |
|
| 44 | - throw new RangeException("$name {$val} is not a number."); |
|
| 45 | - } |
|
| 43 | + if (! \is_numeric($val)) { |
|
| 44 | + throw new RangeException("$name {$val} is not a number."); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - if ($range->includes($val)) { |
|
| 48 | - return $val; |
|
| 49 | - } |
|
| 47 | + if ($range->includes($val)) { |
|
| 48 | + return $val; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - if ($grace->includes($val - $range->first)) { |
|
| 52 | - return $range->first; |
|
| 53 | - } |
|
| 51 | + if ($grace->includes($val - $range->first)) { |
|
| 52 | + return $range->first; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - if ($grace->includes($val - $range->last)) { |
|
| 56 | - return $range->last; |
|
| 57 | - } |
|
| 55 | + if ($grace->includes($val - $range->last)) { |
|
| 56 | + return $range->last; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - throw new RangeException("$name {$val} must be between {$range->first} and {$range->last}$unit"); |
|
| 60 | - } |
|
| 59 | + throw new RangeException("$name {$val} must be between {$range->first} and {$range->last}$unit"); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Encode URI component |
|
| 64 | - * |
|
| 65 | - * @param string $string |
|
| 66 | - * |
|
| 67 | - * @return string |
|
| 68 | - */ |
|
| 69 | - public static function encodeURIComponent($string) |
|
| 70 | - { |
|
| 71 | - $revert = ['%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')']; |
|
| 62 | + /** |
|
| 63 | + * Encode URI component |
|
| 64 | + * |
|
| 65 | + * @param string $string |
|
| 66 | + * |
|
| 67 | + * @return string |
|
| 68 | + */ |
|
| 69 | + public static function encodeURIComponent($string) |
|
| 70 | + { |
|
| 71 | + $revert = ['%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')']; |
|
| 72 | 72 | |
| 73 | - return strtr(rawurlencode($string), $revert); |
|
| 74 | - } |
|
| 73 | + return strtr(rawurlencode($string), $revert); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * mb_chr() wrapper |
|
| 78 | - * |
|
| 79 | - * @param integer $code |
|
| 80 | - * |
|
| 81 | - * @return string |
|
| 82 | - */ |
|
| 83 | - public static function mbChr($code) |
|
| 84 | - { |
|
| 85 | - // Use the native implementation if available. |
|
| 86 | - if (\function_exists('mb_chr')) { |
|
| 87 | - return mb_chr($code, 'UTF-8'); |
|
| 88 | - } |
|
| 76 | + /** |
|
| 77 | + * mb_chr() wrapper |
|
| 78 | + * |
|
| 79 | + * @param integer $code |
|
| 80 | + * |
|
| 81 | + * @return string |
|
| 82 | + */ |
|
| 83 | + public static function mbChr($code) |
|
| 84 | + { |
|
| 85 | + // Use the native implementation if available. |
|
| 86 | + if (\function_exists('mb_chr')) { |
|
| 87 | + return mb_chr($code, 'UTF-8'); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - if (0x80 > $code %= 0x200000) { |
|
| 91 | - $s = \chr($code); |
|
| 92 | - } elseif (0x800 > $code) { |
|
| 93 | - $s = \chr(0xC0 | $code >> 6) . \chr(0x80 | $code & 0x3F); |
|
| 94 | - } elseif (0x10000 > $code) { |
|
| 95 | - $s = \chr(0xE0 | $code >> 12) . \chr(0x80 | $code >> 6 & 0x3F) . \chr(0x80 | $code & 0x3F); |
|
| 96 | - } else { |
|
| 97 | - $s = \chr(0xF0 | $code >> 18) . \chr(0x80 | $code >> 12 & 0x3F) |
|
| 98 | - . \chr(0x80 | $code >> 6 & 0x3F) . \chr(0x80 | $code & 0x3F); |
|
| 99 | - } |
|
| 90 | + if (0x80 > $code %= 0x200000) { |
|
| 91 | + $s = \chr($code); |
|
| 92 | + } elseif (0x800 > $code) { |
|
| 93 | + $s = \chr(0xC0 | $code >> 6) . \chr(0x80 | $code & 0x3F); |
|
| 94 | + } elseif (0x10000 > $code) { |
|
| 95 | + $s = \chr(0xE0 | $code >> 12) . \chr(0x80 | $code >> 6 & 0x3F) . \chr(0x80 | $code & 0x3F); |
|
| 96 | + } else { |
|
| 97 | + $s = \chr(0xF0 | $code >> 18) . \chr(0x80 | $code >> 12 & 0x3F) |
|
| 98 | + . \chr(0x80 | $code >> 6 & 0x3F) . \chr(0x80 | $code & 0x3F); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - return $s; |
|
| 102 | - } |
|
| 101 | + return $s; |
|
| 102 | + } |
|
| 103 | 103 | } |
@@ -40,7 +40,7 @@ |
||
| 40 | 40 | $val = $value[1]; |
| 41 | 41 | $grace = new Range(-0.00001, 0.00001); |
| 42 | 42 | |
| 43 | - if (! \is_numeric($val)) { |
|
| 43 | + if (!\is_numeric($val)) { |
|
| 44 | 44 | throw new RangeException("$name {$val} is not a number."); |
| 45 | 45 | } |
| 46 | 46 | |
@@ -39,113 +39,113 @@ |
||
| 39 | 39 | */ |
| 40 | 40 | class Base64VLQ |
| 41 | 41 | { |
| 42 | - // A Base64 VLQ digit can represent 5 bits, so it is base-32. |
|
| 43 | - const VLQ_BASE_SHIFT = 5; |
|
| 44 | - |
|
| 45 | - // A mask of bits for a VLQ digit (11111), 31 decimal. |
|
| 46 | - const VLQ_BASE_MASK = 31; |
|
| 47 | - |
|
| 48 | - // The continuation bit is the 6th bit. |
|
| 49 | - const VLQ_CONTINUATION_BIT = 32; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Returns the VLQ encoded value. |
|
| 53 | - * |
|
| 54 | - * @param integer $value |
|
| 55 | - * |
|
| 56 | - * @return string |
|
| 57 | - */ |
|
| 58 | - public static function encode($value) |
|
| 59 | - { |
|
| 60 | - $encoded = ''; |
|
| 61 | - $vlq = self::toVLQSigned($value); |
|
| 62 | - |
|
| 63 | - do { |
|
| 64 | - $digit = $vlq & self::VLQ_BASE_MASK; |
|
| 65 | - |
|
| 66 | - //$vlq >>>= self::VLQ_BASE_SHIFT; // unsigned right shift |
|
| 67 | - $vlq = (($vlq >> 1) & PHP_INT_MAX) >> (self::VLQ_BASE_SHIFT - 1); |
|
| 68 | - |
|
| 69 | - if ($vlq > 0) { |
|
| 70 | - $digit |= self::VLQ_CONTINUATION_BIT; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $encoded .= Base64::encode($digit); |
|
| 74 | - } while ($vlq > 0); |
|
| 75 | - |
|
| 76 | - return $encoded; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Decodes VLQValue. |
|
| 81 | - * |
|
| 82 | - * @param string $str |
|
| 83 | - * @param integer $index |
|
| 84 | - * |
|
| 85 | - * @return integer |
|
| 86 | - */ |
|
| 87 | - public static function decode($str, &$index) |
|
| 88 | - { |
|
| 89 | - $result = 0; |
|
| 90 | - $shift = 0; |
|
| 91 | - |
|
| 92 | - do { |
|
| 93 | - $c = $str[$index++]; |
|
| 94 | - $digit = Base64::decode($c); |
|
| 95 | - $continuation = ($digit & self::VLQ_CONTINUATION_BIT) != 0; |
|
| 96 | - $digit &= self::VLQ_BASE_MASK; |
|
| 97 | - $result = $result + ($digit << $shift); |
|
| 98 | - $shift = $shift + self::VLQ_BASE_SHIFT; |
|
| 99 | - } while ($continuation); |
|
| 100 | - |
|
| 101 | - return self::fromVLQSigned($result); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Converts from a two-complement value to a value where the sign bit is |
|
| 106 | - * is placed in the least significant bit. For example, as decimals: |
|
| 107 | - * 1 becomes 2 (10 binary), -1 becomes 3 (11 binary) |
|
| 108 | - * 2 becomes 4 (100 binary), -2 becomes 5 (101 binary) |
|
| 109 | - * |
|
| 110 | - * @param integer $value |
|
| 111 | - * |
|
| 112 | - * @return integer |
|
| 113 | - */ |
|
| 114 | - private static function toVLQSigned($value) |
|
| 115 | - { |
|
| 116 | - if ($value < 0) { |
|
| 117 | - return ((-$value) << 1) + 1; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return ($value << 1) + 0; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Converts to a two-complement value from a value where the sign bit is |
|
| 125 | - * is placed in the least significant bit. For example, as decimals: |
|
| 126 | - * 2 (10 binary) becomes 1, 3 (11 binary) becomes -1 |
|
| 127 | - * 4 (100 binary) becomes 2, 5 (101 binary) becomes -2 |
|
| 128 | - * |
|
| 129 | - * @param integer $value |
|
| 130 | - * |
|
| 131 | - * @return integer |
|
| 132 | - */ |
|
| 133 | - private static function fromVLQSigned($value) |
|
| 134 | - { |
|
| 135 | - $negate = ($value & 1) === 1; |
|
| 136 | - |
|
| 137 | - //$value >>>= 1; // unsigned right shift |
|
| 138 | - $value = ($value >> 1) & PHP_INT_MAX; |
|
| 139 | - |
|
| 140 | - if (! $negate) { |
|
| 141 | - return $value; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - // We need to OR 0x80000000 here to ensure the 32nd bit (the sign bit) is |
|
| 145 | - // always set for negative numbers. If `value` were 1, (meaning `negate` is |
|
| 146 | - // true and all other bits were zeros), `value` would now be 0. -0 is just |
|
| 147 | - // 0, and doesn't flip the 32nd bit as intended. All positive numbers will |
|
| 148 | - // successfully flip the 32nd bit without issue, so it's a noop for them. |
|
| 149 | - return -$value | 0x80000000; |
|
| 150 | - } |
|
| 42 | + // A Base64 VLQ digit can represent 5 bits, so it is base-32. |
|
| 43 | + const VLQ_BASE_SHIFT = 5; |
|
| 44 | + |
|
| 45 | + // A mask of bits for a VLQ digit (11111), 31 decimal. |
|
| 46 | + const VLQ_BASE_MASK = 31; |
|
| 47 | + |
|
| 48 | + // The continuation bit is the 6th bit. |
|
| 49 | + const VLQ_CONTINUATION_BIT = 32; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Returns the VLQ encoded value. |
|
| 53 | + * |
|
| 54 | + * @param integer $value |
|
| 55 | + * |
|
| 56 | + * @return string |
|
| 57 | + */ |
|
| 58 | + public static function encode($value) |
|
| 59 | + { |
|
| 60 | + $encoded = ''; |
|
| 61 | + $vlq = self::toVLQSigned($value); |
|
| 62 | + |
|
| 63 | + do { |
|
| 64 | + $digit = $vlq & self::VLQ_BASE_MASK; |
|
| 65 | + |
|
| 66 | + //$vlq >>>= self::VLQ_BASE_SHIFT; // unsigned right shift |
|
| 67 | + $vlq = (($vlq >> 1) & PHP_INT_MAX) >> (self::VLQ_BASE_SHIFT - 1); |
|
| 68 | + |
|
| 69 | + if ($vlq > 0) { |
|
| 70 | + $digit |= self::VLQ_CONTINUATION_BIT; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $encoded .= Base64::encode($digit); |
|
| 74 | + } while ($vlq > 0); |
|
| 75 | + |
|
| 76 | + return $encoded; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Decodes VLQValue. |
|
| 81 | + * |
|
| 82 | + * @param string $str |
|
| 83 | + * @param integer $index |
|
| 84 | + * |
|
| 85 | + * @return integer |
|
| 86 | + */ |
|
| 87 | + public static function decode($str, &$index) |
|
| 88 | + { |
|
| 89 | + $result = 0; |
|
| 90 | + $shift = 0; |
|
| 91 | + |
|
| 92 | + do { |
|
| 93 | + $c = $str[$index++]; |
|
| 94 | + $digit = Base64::decode($c); |
|
| 95 | + $continuation = ($digit & self::VLQ_CONTINUATION_BIT) != 0; |
|
| 96 | + $digit &= self::VLQ_BASE_MASK; |
|
| 97 | + $result = $result + ($digit << $shift); |
|
| 98 | + $shift = $shift + self::VLQ_BASE_SHIFT; |
|
| 99 | + } while ($continuation); |
|
| 100 | + |
|
| 101 | + return self::fromVLQSigned($result); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Converts from a two-complement value to a value where the sign bit is |
|
| 106 | + * is placed in the least significant bit. For example, as decimals: |
|
| 107 | + * 1 becomes 2 (10 binary), -1 becomes 3 (11 binary) |
|
| 108 | + * 2 becomes 4 (100 binary), -2 becomes 5 (101 binary) |
|
| 109 | + * |
|
| 110 | + * @param integer $value |
|
| 111 | + * |
|
| 112 | + * @return integer |
|
| 113 | + */ |
|
| 114 | + private static function toVLQSigned($value) |
|
| 115 | + { |
|
| 116 | + if ($value < 0) { |
|
| 117 | + return ((-$value) << 1) + 1; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return ($value << 1) + 0; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Converts to a two-complement value from a value where the sign bit is |
|
| 125 | + * is placed in the least significant bit. For example, as decimals: |
|
| 126 | + * 2 (10 binary) becomes 1, 3 (11 binary) becomes -1 |
|
| 127 | + * 4 (100 binary) becomes 2, 5 (101 binary) becomes -2 |
|
| 128 | + * |
|
| 129 | + * @param integer $value |
|
| 130 | + * |
|
| 131 | + * @return integer |
|
| 132 | + */ |
|
| 133 | + private static function fromVLQSigned($value) |
|
| 134 | + { |
|
| 135 | + $negate = ($value & 1) === 1; |
|
| 136 | + |
|
| 137 | + //$value >>>= 1; // unsigned right shift |
|
| 138 | + $value = ($value >> 1) & PHP_INT_MAX; |
|
| 139 | + |
|
| 140 | + if (! $negate) { |
|
| 141 | + return $value; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + // We need to OR 0x80000000 here to ensure the 32nd bit (the sign bit) is |
|
| 145 | + // always set for negative numbers. If `value` were 1, (meaning `negate` is |
|
| 146 | + // true and all other bits were zeros), `value` would now be 0. -0 is just |
|
| 147 | + // 0, and doesn't flip the 32nd bit as intended. All positive numbers will |
|
| 148 | + // successfully flip the 32nd bit without issue, so it's a noop for them. |
|
| 149 | + return -$value | 0x80000000; |
|
| 150 | + } |
|
| 151 | 151 | } |
@@ -137,7 +137,7 @@ |
||
| 137 | 137 | //$value >>>= 1; // unsigned right shift |
| 138 | 138 | $value = ($value >> 1) & PHP_INT_MAX; |
| 139 | 139 | |
| 140 | - if (! $negate) { |
|
| 140 | + if (!$negate) { |
|
| 141 | 141 | return $value; |
| 142 | 142 | } |
| 143 | 143 | |
@@ -19,167 +19,167 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Base64 |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - private static $encodingMap = [ |
|
| 26 | - 0 => 'A', |
|
| 27 | - 1 => 'B', |
|
| 28 | - 2 => 'C', |
|
| 29 | - 3 => 'D', |
|
| 30 | - 4 => 'E', |
|
| 31 | - 5 => 'F', |
|
| 32 | - 6 => 'G', |
|
| 33 | - 7 => 'H', |
|
| 34 | - 8 => 'I', |
|
| 35 | - 9 => 'J', |
|
| 36 | - 10 => 'K', |
|
| 37 | - 11 => 'L', |
|
| 38 | - 12 => 'M', |
|
| 39 | - 13 => 'N', |
|
| 40 | - 14 => 'O', |
|
| 41 | - 15 => 'P', |
|
| 42 | - 16 => 'Q', |
|
| 43 | - 17 => 'R', |
|
| 44 | - 18 => 'S', |
|
| 45 | - 19 => 'T', |
|
| 46 | - 20 => 'U', |
|
| 47 | - 21 => 'V', |
|
| 48 | - 22 => 'W', |
|
| 49 | - 23 => 'X', |
|
| 50 | - 24 => 'Y', |
|
| 51 | - 25 => 'Z', |
|
| 52 | - 26 => 'a', |
|
| 53 | - 27 => 'b', |
|
| 54 | - 28 => 'c', |
|
| 55 | - 29 => 'd', |
|
| 56 | - 30 => 'e', |
|
| 57 | - 31 => 'f', |
|
| 58 | - 32 => 'g', |
|
| 59 | - 33 => 'h', |
|
| 60 | - 34 => 'i', |
|
| 61 | - 35 => 'j', |
|
| 62 | - 36 => 'k', |
|
| 63 | - 37 => 'l', |
|
| 64 | - 38 => 'm', |
|
| 65 | - 39 => 'n', |
|
| 66 | - 40 => 'o', |
|
| 67 | - 41 => 'p', |
|
| 68 | - 42 => 'q', |
|
| 69 | - 43 => 'r', |
|
| 70 | - 44 => 's', |
|
| 71 | - 45 => 't', |
|
| 72 | - 46 => 'u', |
|
| 73 | - 47 => 'v', |
|
| 74 | - 48 => 'w', |
|
| 75 | - 49 => 'x', |
|
| 76 | - 50 => 'y', |
|
| 77 | - 51 => 'z', |
|
| 78 | - 52 => '0', |
|
| 79 | - 53 => '1', |
|
| 80 | - 54 => '2', |
|
| 81 | - 55 => '3', |
|
| 82 | - 56 => '4', |
|
| 83 | - 57 => '5', |
|
| 84 | - 58 => '6', |
|
| 85 | - 59 => '7', |
|
| 86 | - 60 => '8', |
|
| 87 | - 61 => '9', |
|
| 88 | - 62 => '+', |
|
| 89 | - 63 => '/', |
|
| 90 | - ]; |
|
| 22 | + /** |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + private static $encodingMap = [ |
|
| 26 | + 0 => 'A', |
|
| 27 | + 1 => 'B', |
|
| 28 | + 2 => 'C', |
|
| 29 | + 3 => 'D', |
|
| 30 | + 4 => 'E', |
|
| 31 | + 5 => 'F', |
|
| 32 | + 6 => 'G', |
|
| 33 | + 7 => 'H', |
|
| 34 | + 8 => 'I', |
|
| 35 | + 9 => 'J', |
|
| 36 | + 10 => 'K', |
|
| 37 | + 11 => 'L', |
|
| 38 | + 12 => 'M', |
|
| 39 | + 13 => 'N', |
|
| 40 | + 14 => 'O', |
|
| 41 | + 15 => 'P', |
|
| 42 | + 16 => 'Q', |
|
| 43 | + 17 => 'R', |
|
| 44 | + 18 => 'S', |
|
| 45 | + 19 => 'T', |
|
| 46 | + 20 => 'U', |
|
| 47 | + 21 => 'V', |
|
| 48 | + 22 => 'W', |
|
| 49 | + 23 => 'X', |
|
| 50 | + 24 => 'Y', |
|
| 51 | + 25 => 'Z', |
|
| 52 | + 26 => 'a', |
|
| 53 | + 27 => 'b', |
|
| 54 | + 28 => 'c', |
|
| 55 | + 29 => 'd', |
|
| 56 | + 30 => 'e', |
|
| 57 | + 31 => 'f', |
|
| 58 | + 32 => 'g', |
|
| 59 | + 33 => 'h', |
|
| 60 | + 34 => 'i', |
|
| 61 | + 35 => 'j', |
|
| 62 | + 36 => 'k', |
|
| 63 | + 37 => 'l', |
|
| 64 | + 38 => 'm', |
|
| 65 | + 39 => 'n', |
|
| 66 | + 40 => 'o', |
|
| 67 | + 41 => 'p', |
|
| 68 | + 42 => 'q', |
|
| 69 | + 43 => 'r', |
|
| 70 | + 44 => 's', |
|
| 71 | + 45 => 't', |
|
| 72 | + 46 => 'u', |
|
| 73 | + 47 => 'v', |
|
| 74 | + 48 => 'w', |
|
| 75 | + 49 => 'x', |
|
| 76 | + 50 => 'y', |
|
| 77 | + 51 => 'z', |
|
| 78 | + 52 => '0', |
|
| 79 | + 53 => '1', |
|
| 80 | + 54 => '2', |
|
| 81 | + 55 => '3', |
|
| 82 | + 56 => '4', |
|
| 83 | + 57 => '5', |
|
| 84 | + 58 => '6', |
|
| 85 | + 59 => '7', |
|
| 86 | + 60 => '8', |
|
| 87 | + 61 => '9', |
|
| 88 | + 62 => '+', |
|
| 89 | + 63 => '/', |
|
| 90 | + ]; |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * @var array |
|
| 94 | - */ |
|
| 95 | - private static $decodingMap = [ |
|
| 96 | - 'A' => 0, |
|
| 97 | - 'B' => 1, |
|
| 98 | - 'C' => 2, |
|
| 99 | - 'D' => 3, |
|
| 100 | - 'E' => 4, |
|
| 101 | - 'F' => 5, |
|
| 102 | - 'G' => 6, |
|
| 103 | - 'H' => 7, |
|
| 104 | - 'I' => 8, |
|
| 105 | - 'J' => 9, |
|
| 106 | - 'K' => 10, |
|
| 107 | - 'L' => 11, |
|
| 108 | - 'M' => 12, |
|
| 109 | - 'N' => 13, |
|
| 110 | - 'O' => 14, |
|
| 111 | - 'P' => 15, |
|
| 112 | - 'Q' => 16, |
|
| 113 | - 'R' => 17, |
|
| 114 | - 'S' => 18, |
|
| 115 | - 'T' => 19, |
|
| 116 | - 'U' => 20, |
|
| 117 | - 'V' => 21, |
|
| 118 | - 'W' => 22, |
|
| 119 | - 'X' => 23, |
|
| 120 | - 'Y' => 24, |
|
| 121 | - 'Z' => 25, |
|
| 122 | - 'a' => 26, |
|
| 123 | - 'b' => 27, |
|
| 124 | - 'c' => 28, |
|
| 125 | - 'd' => 29, |
|
| 126 | - 'e' => 30, |
|
| 127 | - 'f' => 31, |
|
| 128 | - 'g' => 32, |
|
| 129 | - 'h' => 33, |
|
| 130 | - 'i' => 34, |
|
| 131 | - 'j' => 35, |
|
| 132 | - 'k' => 36, |
|
| 133 | - 'l' => 37, |
|
| 134 | - 'm' => 38, |
|
| 135 | - 'n' => 39, |
|
| 136 | - 'o' => 40, |
|
| 137 | - 'p' => 41, |
|
| 138 | - 'q' => 42, |
|
| 139 | - 'r' => 43, |
|
| 140 | - 's' => 44, |
|
| 141 | - 't' => 45, |
|
| 142 | - 'u' => 46, |
|
| 143 | - 'v' => 47, |
|
| 144 | - 'w' => 48, |
|
| 145 | - 'x' => 49, |
|
| 146 | - 'y' => 50, |
|
| 147 | - 'z' => 51, |
|
| 148 | - 0 => 52, |
|
| 149 | - 1 => 53, |
|
| 150 | - 2 => 54, |
|
| 151 | - 3 => 55, |
|
| 152 | - 4 => 56, |
|
| 153 | - 5 => 57, |
|
| 154 | - 6 => 58, |
|
| 155 | - 7 => 59, |
|
| 156 | - 8 => 60, |
|
| 157 | - 9 => 61, |
|
| 158 | - '+' => 62, |
|
| 159 | - '/' => 63, |
|
| 160 | - ]; |
|
| 92 | + /** |
|
| 93 | + * @var array |
|
| 94 | + */ |
|
| 95 | + private static $decodingMap = [ |
|
| 96 | + 'A' => 0, |
|
| 97 | + 'B' => 1, |
|
| 98 | + 'C' => 2, |
|
| 99 | + 'D' => 3, |
|
| 100 | + 'E' => 4, |
|
| 101 | + 'F' => 5, |
|
| 102 | + 'G' => 6, |
|
| 103 | + 'H' => 7, |
|
| 104 | + 'I' => 8, |
|
| 105 | + 'J' => 9, |
|
| 106 | + 'K' => 10, |
|
| 107 | + 'L' => 11, |
|
| 108 | + 'M' => 12, |
|
| 109 | + 'N' => 13, |
|
| 110 | + 'O' => 14, |
|
| 111 | + 'P' => 15, |
|
| 112 | + 'Q' => 16, |
|
| 113 | + 'R' => 17, |
|
| 114 | + 'S' => 18, |
|
| 115 | + 'T' => 19, |
|
| 116 | + 'U' => 20, |
|
| 117 | + 'V' => 21, |
|
| 118 | + 'W' => 22, |
|
| 119 | + 'X' => 23, |
|
| 120 | + 'Y' => 24, |
|
| 121 | + 'Z' => 25, |
|
| 122 | + 'a' => 26, |
|
| 123 | + 'b' => 27, |
|
| 124 | + 'c' => 28, |
|
| 125 | + 'd' => 29, |
|
| 126 | + 'e' => 30, |
|
| 127 | + 'f' => 31, |
|
| 128 | + 'g' => 32, |
|
| 129 | + 'h' => 33, |
|
| 130 | + 'i' => 34, |
|
| 131 | + 'j' => 35, |
|
| 132 | + 'k' => 36, |
|
| 133 | + 'l' => 37, |
|
| 134 | + 'm' => 38, |
|
| 135 | + 'n' => 39, |
|
| 136 | + 'o' => 40, |
|
| 137 | + 'p' => 41, |
|
| 138 | + 'q' => 42, |
|
| 139 | + 'r' => 43, |
|
| 140 | + 's' => 44, |
|
| 141 | + 't' => 45, |
|
| 142 | + 'u' => 46, |
|
| 143 | + 'v' => 47, |
|
| 144 | + 'w' => 48, |
|
| 145 | + 'x' => 49, |
|
| 146 | + 'y' => 50, |
|
| 147 | + 'z' => 51, |
|
| 148 | + 0 => 52, |
|
| 149 | + 1 => 53, |
|
| 150 | + 2 => 54, |
|
| 151 | + 3 => 55, |
|
| 152 | + 4 => 56, |
|
| 153 | + 5 => 57, |
|
| 154 | + 6 => 58, |
|
| 155 | + 7 => 59, |
|
| 156 | + 8 => 60, |
|
| 157 | + 9 => 61, |
|
| 158 | + '+' => 62, |
|
| 159 | + '/' => 63, |
|
| 160 | + ]; |
|
| 161 | 161 | |
| 162 | - /** |
|
| 163 | - * Convert to base64 |
|
| 164 | - * |
|
| 165 | - * @param integer $value |
|
| 166 | - * |
|
| 167 | - * @return string |
|
| 168 | - */ |
|
| 169 | - public static function encode($value) |
|
| 170 | - { |
|
| 171 | - return self::$encodingMap[$value]; |
|
| 172 | - } |
|
| 162 | + /** |
|
| 163 | + * Convert to base64 |
|
| 164 | + * |
|
| 165 | + * @param integer $value |
|
| 166 | + * |
|
| 167 | + * @return string |
|
| 168 | + */ |
|
| 169 | + public static function encode($value) |
|
| 170 | + { |
|
| 171 | + return self::$encodingMap[$value]; |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - /** |
|
| 175 | - * Convert from base64 |
|
| 176 | - * |
|
| 177 | - * @param string $value |
|
| 178 | - * |
|
| 179 | - * @return integer |
|
| 180 | - */ |
|
| 181 | - public static function decode($value) |
|
| 182 | - { |
|
| 183 | - return self::$decodingMap[$value]; |
|
| 184 | - } |
|
| 174 | + /** |
|
| 175 | + * Convert from base64 |
|
| 176 | + * |
|
| 177 | + * @param string $value |
|
| 178 | + * |
|
| 179 | + * @return integer |
|
| 180 | + */ |
|
| 181 | + public static function decode($value) |
|
| 182 | + { |
|
| 183 | + return self::$decodingMap[$value]; |
|
| 184 | + } |
|
| 185 | 185 | } |
@@ -24,326 +24,326 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | class SourceMapGenerator |
| 26 | 26 | { |
| 27 | - /** |
|
| 28 | - * What version of source map does the generator generate? |
|
| 29 | - */ |
|
| 30 | - const VERSION = 3; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Array of default options |
|
| 34 | - * |
|
| 35 | - * @var array |
|
| 36 | - */ |
|
| 37 | - protected $defaultOptions = [ |
|
| 38 | - // an optional source root, useful for relocating source files |
|
| 39 | - // on a server or removing repeated values in the 'sources' entry. |
|
| 40 | - // This value is prepended to the individual entries in the 'source' field. |
|
| 41 | - 'sourceRoot' => '', |
|
| 42 | - |
|
| 43 | - // an optional name of the generated code that this source map is associated with. |
|
| 44 | - 'sourceMapFilename' => null, |
|
| 45 | - |
|
| 46 | - // url of the map |
|
| 47 | - 'sourceMapURL' => null, |
|
| 48 | - |
|
| 49 | - // absolute path to a file to write the map to |
|
| 50 | - 'sourceMapWriteTo' => null, |
|
| 51 | - |
|
| 52 | - // output source contents? |
|
| 53 | - 'outputSourceFiles' => false, |
|
| 54 | - |
|
| 55 | - // base path for filename normalization |
|
| 56 | - 'sourceMapRootpath' => '', |
|
| 57 | - |
|
| 58 | - // base path for filename normalization |
|
| 59 | - 'sourceMapBasepath' => '' |
|
| 60 | - ]; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * The base64 VLQ encoder |
|
| 64 | - * |
|
| 65 | - * @var \ScssPhp\ScssPhp\SourceMap\Base64VLQ |
|
| 66 | - */ |
|
| 67 | - protected $encoder; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Array of mappings |
|
| 71 | - * |
|
| 72 | - * @var array |
|
| 73 | - */ |
|
| 74 | - protected $mappings = []; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Array of contents map |
|
| 78 | - * |
|
| 79 | - * @var array |
|
| 80 | - */ |
|
| 81 | - protected $contentsMap = []; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * File to content map |
|
| 85 | - * |
|
| 86 | - * @var array |
|
| 87 | - */ |
|
| 88 | - protected $sources = []; |
|
| 89 | - protected $sourceKeys = []; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @var array |
|
| 93 | - */ |
|
| 94 | - private $options; |
|
| 95 | - |
|
| 96 | - public function __construct(array $options = []) |
|
| 97 | - { |
|
| 98 | - $this->options = array_merge($this->defaultOptions, $options); |
|
| 99 | - $this->encoder = new Base64VLQ(); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Adds a mapping |
|
| 104 | - * |
|
| 105 | - * @param integer $generatedLine The line number in generated file |
|
| 106 | - * @param integer $generatedColumn The column number in generated file |
|
| 107 | - * @param integer $originalLine The line number in original file |
|
| 108 | - * @param integer $originalColumn The column number in original file |
|
| 109 | - * @param string $sourceFile The original source file |
|
| 110 | - */ |
|
| 111 | - public function addMapping($generatedLine, $generatedColumn, $originalLine, $originalColumn, $sourceFile) |
|
| 112 | - { |
|
| 113 | - $this->mappings[] = [ |
|
| 114 | - 'generated_line' => $generatedLine, |
|
| 115 | - 'generated_column' => $generatedColumn, |
|
| 116 | - 'original_line' => $originalLine, |
|
| 117 | - 'original_column' => $originalColumn, |
|
| 118 | - 'source_file' => $sourceFile |
|
| 119 | - ]; |
|
| 120 | - |
|
| 121 | - $this->sources[$sourceFile] = $sourceFile; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Saves the source map to a file |
|
| 126 | - * |
|
| 127 | - * @param string $content The content to write |
|
| 128 | - * |
|
| 129 | - * @return string |
|
| 130 | - * |
|
| 131 | - * @throws \ScssPhp\ScssPhp\Exception\CompilerException If the file could not be saved |
|
| 132 | - */ |
|
| 133 | - public function saveMap($content) |
|
| 134 | - { |
|
| 135 | - $file = $this->options['sourceMapWriteTo']; |
|
| 136 | - $dir = \dirname($file); |
|
| 137 | - |
|
| 138 | - // directory does not exist |
|
| 139 | - if (! is_dir($dir)) { |
|
| 140 | - // FIXME: create the dir automatically? |
|
| 141 | - throw new CompilerException( |
|
| 142 | - sprintf('The directory "%s" does not exist. Cannot save the source map.', $dir) |
|
| 143 | - ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // FIXME: proper saving, with dir write check! |
|
| 147 | - if (file_put_contents($file, $content) === false) { |
|
| 148 | - throw new CompilerException(sprintf('Cannot save the source map to "%s"', $file)); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - return $this->options['sourceMapURL']; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Generates the JSON source map |
|
| 156 | - * |
|
| 157 | - * @return string |
|
| 158 | - * |
|
| 159 | - * @see https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit# |
|
| 160 | - */ |
|
| 161 | - public function generateJson() |
|
| 162 | - { |
|
| 163 | - $sourceMap = []; |
|
| 164 | - $mappings = $this->generateMappings(); |
|
| 165 | - |
|
| 166 | - // File version (always the first entry in the object) and must be a positive integer. |
|
| 167 | - $sourceMap['version'] = self::VERSION; |
|
| 168 | - |
|
| 169 | - // An optional name of the generated code that this source map is associated with. |
|
| 170 | - $file = $this->options['sourceMapFilename']; |
|
| 171 | - |
|
| 172 | - if ($file) { |
|
| 173 | - $sourceMap['file'] = $file; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - // An optional source root, useful for relocating source files on a server or removing repeated values in the |
|
| 177 | - // 'sources' entry. This value is prepended to the individual entries in the 'source' field. |
|
| 178 | - $root = $this->options['sourceRoot']; |
|
| 179 | - |
|
| 180 | - if ($root) { |
|
| 181 | - $sourceMap['sourceRoot'] = $root; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - // A list of original sources used by the 'mappings' entry. |
|
| 185 | - $sourceMap['sources'] = []; |
|
| 186 | - |
|
| 187 | - foreach ($this->sources as $sourceUri => $sourceFilename) { |
|
| 188 | - $sourceMap['sources'][] = $this->normalizeFilename($sourceFilename); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - // A list of symbol names used by the 'mappings' entry. |
|
| 192 | - $sourceMap['names'] = []; |
|
| 193 | - |
|
| 194 | - // A string with the encoded mapping data. |
|
| 195 | - $sourceMap['mappings'] = $mappings; |
|
| 196 | - |
|
| 197 | - if ($this->options['outputSourceFiles']) { |
|
| 198 | - // An optional list of source content, useful when the 'source' can't be hosted. |
|
| 199 | - // The contents are listed in the same order as the sources above. |
|
| 200 | - // 'null' may be used if some original sources should be retrieved by name. |
|
| 201 | - $sourceMap['sourcesContent'] = $this->getSourcesContent(); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - // less.js compat fixes |
|
| 205 | - if (\count($sourceMap['sources']) && empty($sourceMap['sourceRoot'])) { |
|
| 206 | - unset($sourceMap['sourceRoot']); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return json_encode($sourceMap, JSON_UNESCAPED_SLASHES); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Returns the sources contents |
|
| 214 | - * |
|
| 215 | - * @return array|null |
|
| 216 | - */ |
|
| 217 | - protected function getSourcesContent() |
|
| 218 | - { |
|
| 219 | - if (empty($this->sources)) { |
|
| 220 | - return null; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - $content = []; |
|
| 224 | - |
|
| 225 | - foreach ($this->sources as $sourceFile) { |
|
| 226 | - $content[] = file_get_contents($sourceFile); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - return $content; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Generates the mappings string |
|
| 234 | - * |
|
| 235 | - * @return string |
|
| 236 | - */ |
|
| 237 | - public function generateMappings() |
|
| 238 | - { |
|
| 239 | - if (! \count($this->mappings)) { |
|
| 240 | - return ''; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - $this->sourceKeys = array_flip(array_keys($this->sources)); |
|
| 244 | - |
|
| 245 | - // group mappings by generated line number. |
|
| 246 | - $groupedMap = $groupedMapEncoded = []; |
|
| 247 | - |
|
| 248 | - foreach ($this->mappings as $m) { |
|
| 249 | - $groupedMap[$m['generated_line']][] = $m; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - ksort($groupedMap); |
|
| 253 | - |
|
| 254 | - $lastGeneratedLine = $lastOriginalIndex = $lastOriginalLine = $lastOriginalColumn = 0; |
|
| 255 | - |
|
| 256 | - foreach ($groupedMap as $lineNumber => $lineMap) { |
|
| 257 | - while (++$lastGeneratedLine < $lineNumber) { |
|
| 258 | - $groupedMapEncoded[] = ';'; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - $lineMapEncoded = []; |
|
| 262 | - $lastGeneratedColumn = 0; |
|
| 263 | - |
|
| 264 | - foreach ($lineMap as $m) { |
|
| 265 | - $mapEncoded = $this->encoder->encode($m['generated_column'] - $lastGeneratedColumn); |
|
| 266 | - $lastGeneratedColumn = $m['generated_column']; |
|
| 267 | - |
|
| 268 | - // find the index |
|
| 269 | - if ($m['source_file']) { |
|
| 270 | - $index = $this->findFileIndex($m['source_file']); |
|
| 271 | - |
|
| 272 | - if ($index !== false) { |
|
| 273 | - $mapEncoded .= $this->encoder->encode($index - $lastOriginalIndex); |
|
| 274 | - $lastOriginalIndex = $index; |
|
| 275 | - // lines are stored 0-based in SourceMap spec version 3 |
|
| 276 | - $mapEncoded .= $this->encoder->encode($m['original_line'] - 1 - $lastOriginalLine); |
|
| 277 | - $lastOriginalLine = $m['original_line'] - 1; |
|
| 278 | - $mapEncoded .= $this->encoder->encode($m['original_column'] - $lastOriginalColumn); |
|
| 279 | - $lastOriginalColumn = $m['original_column']; |
|
| 280 | - } |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - $lineMapEncoded[] = $mapEncoded; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - $groupedMapEncoded[] = implode(',', $lineMapEncoded) . ';'; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - return rtrim(implode($groupedMapEncoded), ';'); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Finds the index for the filename |
|
| 294 | - * |
|
| 295 | - * @param string $filename |
|
| 296 | - * |
|
| 297 | - * @return integer|false |
|
| 298 | - */ |
|
| 299 | - protected function findFileIndex($filename) |
|
| 300 | - { |
|
| 301 | - return $this->sourceKeys[$filename]; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Normalize filename |
|
| 306 | - * |
|
| 307 | - * @param string $filename |
|
| 308 | - * |
|
| 309 | - * @return string |
|
| 310 | - */ |
|
| 311 | - protected function normalizeFilename($filename) |
|
| 312 | - { |
|
| 313 | - $filename = $this->fixWindowsPath($filename); |
|
| 314 | - $rootpath = $this->options['sourceMapRootpath']; |
|
| 315 | - $basePath = $this->options['sourceMapBasepath']; |
|
| 316 | - |
|
| 317 | - // "Trim" the 'sourceMapBasepath' from the output filename. |
|
| 318 | - if (\strlen($basePath) && strpos($filename, $basePath) === 0) { |
|
| 319 | - $filename = substr($filename, \strlen($basePath)); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - // Remove extra leading path separators. |
|
| 323 | - if (strpos($filename, '\\') === 0 || strpos($filename, '/') === 0) { |
|
| 324 | - $filename = substr($filename, 1); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - return $rootpath . $filename; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Fix windows paths |
|
| 332 | - * |
|
| 333 | - * @param string $path |
|
| 334 | - * @param boolean $addEndSlash |
|
| 335 | - * |
|
| 336 | - * @return string |
|
| 337 | - */ |
|
| 338 | - public function fixWindowsPath($path, $addEndSlash = false) |
|
| 339 | - { |
|
| 340 | - $slash = ($addEndSlash) ? '/' : ''; |
|
| 341 | - |
|
| 342 | - if (! empty($path)) { |
|
| 343 | - $path = str_replace('\\', '/', $path); |
|
| 344 | - $path = rtrim($path, '/') . $slash; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - return $path; |
|
| 348 | - } |
|
| 27 | + /** |
|
| 28 | + * What version of source map does the generator generate? |
|
| 29 | + */ |
|
| 30 | + const VERSION = 3; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Array of default options |
|
| 34 | + * |
|
| 35 | + * @var array |
|
| 36 | + */ |
|
| 37 | + protected $defaultOptions = [ |
|
| 38 | + // an optional source root, useful for relocating source files |
|
| 39 | + // on a server or removing repeated values in the 'sources' entry. |
|
| 40 | + // This value is prepended to the individual entries in the 'source' field. |
|
| 41 | + 'sourceRoot' => '', |
|
| 42 | + |
|
| 43 | + // an optional name of the generated code that this source map is associated with. |
|
| 44 | + 'sourceMapFilename' => null, |
|
| 45 | + |
|
| 46 | + // url of the map |
|
| 47 | + 'sourceMapURL' => null, |
|
| 48 | + |
|
| 49 | + // absolute path to a file to write the map to |
|
| 50 | + 'sourceMapWriteTo' => null, |
|
| 51 | + |
|
| 52 | + // output source contents? |
|
| 53 | + 'outputSourceFiles' => false, |
|
| 54 | + |
|
| 55 | + // base path for filename normalization |
|
| 56 | + 'sourceMapRootpath' => '', |
|
| 57 | + |
|
| 58 | + // base path for filename normalization |
|
| 59 | + 'sourceMapBasepath' => '' |
|
| 60 | + ]; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * The base64 VLQ encoder |
|
| 64 | + * |
|
| 65 | + * @var \ScssPhp\ScssPhp\SourceMap\Base64VLQ |
|
| 66 | + */ |
|
| 67 | + protected $encoder; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Array of mappings |
|
| 71 | + * |
|
| 72 | + * @var array |
|
| 73 | + */ |
|
| 74 | + protected $mappings = []; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Array of contents map |
|
| 78 | + * |
|
| 79 | + * @var array |
|
| 80 | + */ |
|
| 81 | + protected $contentsMap = []; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * File to content map |
|
| 85 | + * |
|
| 86 | + * @var array |
|
| 87 | + */ |
|
| 88 | + protected $sources = []; |
|
| 89 | + protected $sourceKeys = []; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @var array |
|
| 93 | + */ |
|
| 94 | + private $options; |
|
| 95 | + |
|
| 96 | + public function __construct(array $options = []) |
|
| 97 | + { |
|
| 98 | + $this->options = array_merge($this->defaultOptions, $options); |
|
| 99 | + $this->encoder = new Base64VLQ(); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Adds a mapping |
|
| 104 | + * |
|
| 105 | + * @param integer $generatedLine The line number in generated file |
|
| 106 | + * @param integer $generatedColumn The column number in generated file |
|
| 107 | + * @param integer $originalLine The line number in original file |
|
| 108 | + * @param integer $originalColumn The column number in original file |
|
| 109 | + * @param string $sourceFile The original source file |
|
| 110 | + */ |
|
| 111 | + public function addMapping($generatedLine, $generatedColumn, $originalLine, $originalColumn, $sourceFile) |
|
| 112 | + { |
|
| 113 | + $this->mappings[] = [ |
|
| 114 | + 'generated_line' => $generatedLine, |
|
| 115 | + 'generated_column' => $generatedColumn, |
|
| 116 | + 'original_line' => $originalLine, |
|
| 117 | + 'original_column' => $originalColumn, |
|
| 118 | + 'source_file' => $sourceFile |
|
| 119 | + ]; |
|
| 120 | + |
|
| 121 | + $this->sources[$sourceFile] = $sourceFile; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Saves the source map to a file |
|
| 126 | + * |
|
| 127 | + * @param string $content The content to write |
|
| 128 | + * |
|
| 129 | + * @return string |
|
| 130 | + * |
|
| 131 | + * @throws \ScssPhp\ScssPhp\Exception\CompilerException If the file could not be saved |
|
| 132 | + */ |
|
| 133 | + public function saveMap($content) |
|
| 134 | + { |
|
| 135 | + $file = $this->options['sourceMapWriteTo']; |
|
| 136 | + $dir = \dirname($file); |
|
| 137 | + |
|
| 138 | + // directory does not exist |
|
| 139 | + if (! is_dir($dir)) { |
|
| 140 | + // FIXME: create the dir automatically? |
|
| 141 | + throw new CompilerException( |
|
| 142 | + sprintf('The directory "%s" does not exist. Cannot save the source map.', $dir) |
|
| 143 | + ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + // FIXME: proper saving, with dir write check! |
|
| 147 | + if (file_put_contents($file, $content) === false) { |
|
| 148 | + throw new CompilerException(sprintf('Cannot save the source map to "%s"', $file)); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + return $this->options['sourceMapURL']; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Generates the JSON source map |
|
| 156 | + * |
|
| 157 | + * @return string |
|
| 158 | + * |
|
| 159 | + * @see https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit# |
|
| 160 | + */ |
|
| 161 | + public function generateJson() |
|
| 162 | + { |
|
| 163 | + $sourceMap = []; |
|
| 164 | + $mappings = $this->generateMappings(); |
|
| 165 | + |
|
| 166 | + // File version (always the first entry in the object) and must be a positive integer. |
|
| 167 | + $sourceMap['version'] = self::VERSION; |
|
| 168 | + |
|
| 169 | + // An optional name of the generated code that this source map is associated with. |
|
| 170 | + $file = $this->options['sourceMapFilename']; |
|
| 171 | + |
|
| 172 | + if ($file) { |
|
| 173 | + $sourceMap['file'] = $file; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + // An optional source root, useful for relocating source files on a server or removing repeated values in the |
|
| 177 | + // 'sources' entry. This value is prepended to the individual entries in the 'source' field. |
|
| 178 | + $root = $this->options['sourceRoot']; |
|
| 179 | + |
|
| 180 | + if ($root) { |
|
| 181 | + $sourceMap['sourceRoot'] = $root; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + // A list of original sources used by the 'mappings' entry. |
|
| 185 | + $sourceMap['sources'] = []; |
|
| 186 | + |
|
| 187 | + foreach ($this->sources as $sourceUri => $sourceFilename) { |
|
| 188 | + $sourceMap['sources'][] = $this->normalizeFilename($sourceFilename); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + // A list of symbol names used by the 'mappings' entry. |
|
| 192 | + $sourceMap['names'] = []; |
|
| 193 | + |
|
| 194 | + // A string with the encoded mapping data. |
|
| 195 | + $sourceMap['mappings'] = $mappings; |
|
| 196 | + |
|
| 197 | + if ($this->options['outputSourceFiles']) { |
|
| 198 | + // An optional list of source content, useful when the 'source' can't be hosted. |
|
| 199 | + // The contents are listed in the same order as the sources above. |
|
| 200 | + // 'null' may be used if some original sources should be retrieved by name. |
|
| 201 | + $sourceMap['sourcesContent'] = $this->getSourcesContent(); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + // less.js compat fixes |
|
| 205 | + if (\count($sourceMap['sources']) && empty($sourceMap['sourceRoot'])) { |
|
| 206 | + unset($sourceMap['sourceRoot']); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return json_encode($sourceMap, JSON_UNESCAPED_SLASHES); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Returns the sources contents |
|
| 214 | + * |
|
| 215 | + * @return array|null |
|
| 216 | + */ |
|
| 217 | + protected function getSourcesContent() |
|
| 218 | + { |
|
| 219 | + if (empty($this->sources)) { |
|
| 220 | + return null; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + $content = []; |
|
| 224 | + |
|
| 225 | + foreach ($this->sources as $sourceFile) { |
|
| 226 | + $content[] = file_get_contents($sourceFile); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + return $content; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Generates the mappings string |
|
| 234 | + * |
|
| 235 | + * @return string |
|
| 236 | + */ |
|
| 237 | + public function generateMappings() |
|
| 238 | + { |
|
| 239 | + if (! \count($this->mappings)) { |
|
| 240 | + return ''; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + $this->sourceKeys = array_flip(array_keys($this->sources)); |
|
| 244 | + |
|
| 245 | + // group mappings by generated line number. |
|
| 246 | + $groupedMap = $groupedMapEncoded = []; |
|
| 247 | + |
|
| 248 | + foreach ($this->mappings as $m) { |
|
| 249 | + $groupedMap[$m['generated_line']][] = $m; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + ksort($groupedMap); |
|
| 253 | + |
|
| 254 | + $lastGeneratedLine = $lastOriginalIndex = $lastOriginalLine = $lastOriginalColumn = 0; |
|
| 255 | + |
|
| 256 | + foreach ($groupedMap as $lineNumber => $lineMap) { |
|
| 257 | + while (++$lastGeneratedLine < $lineNumber) { |
|
| 258 | + $groupedMapEncoded[] = ';'; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + $lineMapEncoded = []; |
|
| 262 | + $lastGeneratedColumn = 0; |
|
| 263 | + |
|
| 264 | + foreach ($lineMap as $m) { |
|
| 265 | + $mapEncoded = $this->encoder->encode($m['generated_column'] - $lastGeneratedColumn); |
|
| 266 | + $lastGeneratedColumn = $m['generated_column']; |
|
| 267 | + |
|
| 268 | + // find the index |
|
| 269 | + if ($m['source_file']) { |
|
| 270 | + $index = $this->findFileIndex($m['source_file']); |
|
| 271 | + |
|
| 272 | + if ($index !== false) { |
|
| 273 | + $mapEncoded .= $this->encoder->encode($index - $lastOriginalIndex); |
|
| 274 | + $lastOriginalIndex = $index; |
|
| 275 | + // lines are stored 0-based in SourceMap spec version 3 |
|
| 276 | + $mapEncoded .= $this->encoder->encode($m['original_line'] - 1 - $lastOriginalLine); |
|
| 277 | + $lastOriginalLine = $m['original_line'] - 1; |
|
| 278 | + $mapEncoded .= $this->encoder->encode($m['original_column'] - $lastOriginalColumn); |
|
| 279 | + $lastOriginalColumn = $m['original_column']; |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + $lineMapEncoded[] = $mapEncoded; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + $groupedMapEncoded[] = implode(',', $lineMapEncoded) . ';'; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + return rtrim(implode($groupedMapEncoded), ';'); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Finds the index for the filename |
|
| 294 | + * |
|
| 295 | + * @param string $filename |
|
| 296 | + * |
|
| 297 | + * @return integer|false |
|
| 298 | + */ |
|
| 299 | + protected function findFileIndex($filename) |
|
| 300 | + { |
|
| 301 | + return $this->sourceKeys[$filename]; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Normalize filename |
|
| 306 | + * |
|
| 307 | + * @param string $filename |
|
| 308 | + * |
|
| 309 | + * @return string |
|
| 310 | + */ |
|
| 311 | + protected function normalizeFilename($filename) |
|
| 312 | + { |
|
| 313 | + $filename = $this->fixWindowsPath($filename); |
|
| 314 | + $rootpath = $this->options['sourceMapRootpath']; |
|
| 315 | + $basePath = $this->options['sourceMapBasepath']; |
|
| 316 | + |
|
| 317 | + // "Trim" the 'sourceMapBasepath' from the output filename. |
|
| 318 | + if (\strlen($basePath) && strpos($filename, $basePath) === 0) { |
|
| 319 | + $filename = substr($filename, \strlen($basePath)); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + // Remove extra leading path separators. |
|
| 323 | + if (strpos($filename, '\\') === 0 || strpos($filename, '/') === 0) { |
|
| 324 | + $filename = substr($filename, 1); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + return $rootpath . $filename; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Fix windows paths |
|
| 332 | + * |
|
| 333 | + * @param string $path |
|
| 334 | + * @param boolean $addEndSlash |
|
| 335 | + * |
|
| 336 | + * @return string |
|
| 337 | + */ |
|
| 338 | + public function fixWindowsPath($path, $addEndSlash = false) |
|
| 339 | + { |
|
| 340 | + $slash = ($addEndSlash) ? '/' : ''; |
|
| 341 | + |
|
| 342 | + if (! empty($path)) { |
|
| 343 | + $path = str_replace('\\', '/', $path); |
|
| 344 | + $path = rtrim($path, '/') . $slash; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + return $path; |
|
| 348 | + } |
|
| 349 | 349 | } |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | $dir = \dirname($file); |
| 137 | 137 | |
| 138 | 138 | // directory does not exist |
| 139 | - if (! is_dir($dir)) { |
|
| 139 | + if (!is_dir($dir)) { |
|
| 140 | 140 | // FIXME: create the dir automatically? |
| 141 | 141 | throw new CompilerException( |
| 142 | 142 | sprintf('The directory "%s" does not exist. Cannot save the source map.', $dir) |
@@ -236,7 +236,7 @@ discard block |
||
| 236 | 236 | */ |
| 237 | 237 | public function generateMappings() |
| 238 | 238 | { |
| 239 | - if (! \count($this->mappings)) { |
|
| 239 | + if (!\count($this->mappings)) { |
|
| 240 | 240 | return ''; |
| 241 | 241 | } |
| 242 | 242 | |
@@ -339,7 +339,7 @@ discard block |
||
| 339 | 339 | { |
| 340 | 340 | $slash = ($addEndSlash) ? '/' : ''; |
| 341 | 341 | |
| 342 | - if (! empty($path)) { |
|
| 342 | + if (!empty($path)) { |
|
| 343 | 343 | $path = str_replace('\\', '/', $path); |
| 344 | 344 | $path = rtrim($path, '/') . $slash; |
| 345 | 345 | } |
@@ -29,367 +29,367 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | class Number extends Node implements \ArrayAccess |
| 31 | 31 | { |
| 32 | - const PRECISION = 10; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var integer |
|
| 36 | - * @deprecated use {Number::PRECISION} instead to read the precision. Configuring it is not supported anymore. |
|
| 37 | - */ |
|
| 38 | - public static $precision = self::PRECISION; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @see http://www.w3.org/TR/2012/WD-css3-values-20120308/ |
|
| 42 | - * |
|
| 43 | - * @var array |
|
| 44 | - */ |
|
| 45 | - protected static $unitTable = [ |
|
| 46 | - 'in' => [ |
|
| 47 | - 'in' => 1, |
|
| 48 | - 'pc' => 6, |
|
| 49 | - 'pt' => 72, |
|
| 50 | - 'px' => 96, |
|
| 51 | - 'cm' => 2.54, |
|
| 52 | - 'mm' => 25.4, |
|
| 53 | - 'q' => 101.6, |
|
| 54 | - ], |
|
| 55 | - 'turn' => [ |
|
| 56 | - 'deg' => 360, |
|
| 57 | - 'grad' => 400, |
|
| 58 | - 'rad' => 6.28318530717958647692528676, // 2 * M_PI |
|
| 59 | - 'turn' => 1, |
|
| 60 | - ], |
|
| 61 | - 's' => [ |
|
| 62 | - 's' => 1, |
|
| 63 | - 'ms' => 1000, |
|
| 64 | - ], |
|
| 65 | - 'Hz' => [ |
|
| 66 | - 'Hz' => 1, |
|
| 67 | - 'kHz' => 0.001, |
|
| 68 | - ], |
|
| 69 | - 'dpi' => [ |
|
| 70 | - 'dpi' => 1, |
|
| 71 | - 'dpcm' => 1 / 2.54, |
|
| 72 | - 'dppx' => 1 / 96, |
|
| 73 | - ], |
|
| 74 | - ]; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @var integer|float |
|
| 78 | - */ |
|
| 79 | - public $dimension; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @var array |
|
| 83 | - */ |
|
| 84 | - public $units; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Initialize number |
|
| 88 | - * |
|
| 89 | - * @param mixed $dimension |
|
| 90 | - * @param mixed $initialUnit |
|
| 91 | - */ |
|
| 92 | - public function __construct($dimension, $initialUnit) |
|
| 93 | - { |
|
| 94 | - $this->type = Type::T_NUMBER; |
|
| 95 | - $this->dimension = $dimension; |
|
| 96 | - $this->units = \is_array($initialUnit) |
|
| 97 | - ? $initialUnit |
|
| 98 | - : ($initialUnit ? [$initialUnit => 1] |
|
| 99 | - : []); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Coerce number to target units |
|
| 104 | - * |
|
| 105 | - * @param array $units |
|
| 106 | - * |
|
| 107 | - * @return \ScssPhp\ScssPhp\Node\Number |
|
| 108 | - */ |
|
| 109 | - public function coerce($units) |
|
| 110 | - { |
|
| 111 | - if ($this->unitless()) { |
|
| 112 | - return new Number($this->dimension, $units); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $dimension = $this->dimension; |
|
| 116 | - |
|
| 117 | - if (\count($units)) { |
|
| 118 | - $baseUnit = array_keys($units); |
|
| 119 | - $baseUnit = reset($baseUnit); |
|
| 120 | - $baseUnit = $this->findBaseUnit($baseUnit); |
|
| 121 | - if ($baseUnit && isset(static::$unitTable[$baseUnit])) { |
|
| 122 | - foreach (static::$unitTable[$baseUnit] as $unit => $conv) { |
|
| 123 | - $from = isset($this->units[$unit]) ? $this->units[$unit] : 0; |
|
| 124 | - $to = isset($units[$unit]) ? $units[$unit] : 0; |
|
| 125 | - $factor = pow($conv, $from - $to); |
|
| 126 | - $dimension /= $factor; |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - return new Number($dimension, $units); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Normalize number |
|
| 136 | - * |
|
| 137 | - * @return \ScssPhp\ScssPhp\Node\Number |
|
| 138 | - */ |
|
| 139 | - public function normalize() |
|
| 140 | - { |
|
| 141 | - $dimension = $this->dimension; |
|
| 142 | - $units = []; |
|
| 143 | - |
|
| 144 | - $this->normalizeUnits($dimension, $units); |
|
| 145 | - |
|
| 146 | - return new Number($dimension, $units); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * {@inheritdoc} |
|
| 151 | - */ |
|
| 152 | - public function offsetExists($offset) |
|
| 153 | - { |
|
| 154 | - if ($offset === -3) { |
|
| 155 | - return ! \is_null($this->sourceColumn); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - if ($offset === -2) { |
|
| 159 | - return ! \is_null($this->sourceLine); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - if ( |
|
| 163 | - $offset === -1 || |
|
| 164 | - $offset === 0 || |
|
| 165 | - $offset === 1 || |
|
| 166 | - $offset === 2 |
|
| 167 | - ) { |
|
| 168 | - return true; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - return false; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * {@inheritdoc} |
|
| 176 | - */ |
|
| 177 | - public function offsetGet($offset) |
|
| 178 | - { |
|
| 179 | - switch ($offset) { |
|
| 180 | - case -3: |
|
| 181 | - return $this->sourceColumn; |
|
| 182 | - |
|
| 183 | - case -2: |
|
| 184 | - return $this->sourceLine; |
|
| 185 | - |
|
| 186 | - case -1: |
|
| 187 | - return $this->sourceIndex; |
|
| 188 | - |
|
| 189 | - case 0: |
|
| 190 | - return $this->type; |
|
| 191 | - |
|
| 192 | - case 1: |
|
| 193 | - return $this->dimension; |
|
| 194 | - |
|
| 195 | - case 2: |
|
| 196 | - return $this->units; |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * {@inheritdoc} |
|
| 202 | - */ |
|
| 203 | - public function offsetSet($offset, $value) |
|
| 204 | - { |
|
| 205 | - if ($offset === 1) { |
|
| 206 | - $this->dimension = $value; |
|
| 207 | - } elseif ($offset === 2) { |
|
| 208 | - $this->units = $value; |
|
| 209 | - } elseif ($offset == -1) { |
|
| 210 | - $this->sourceIndex = $value; |
|
| 211 | - } elseif ($offset == -2) { |
|
| 212 | - $this->sourceLine = $value; |
|
| 213 | - } elseif ($offset == -3) { |
|
| 214 | - $this->sourceColumn = $value; |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * {@inheritdoc} |
|
| 220 | - */ |
|
| 221 | - public function offsetUnset($offset) |
|
| 222 | - { |
|
| 223 | - if ($offset === 1) { |
|
| 224 | - $this->dimension = null; |
|
| 225 | - } elseif ($offset === 2) { |
|
| 226 | - $this->units = null; |
|
| 227 | - } elseif ($offset === -1) { |
|
| 228 | - $this->sourceIndex = null; |
|
| 229 | - } elseif ($offset === -2) { |
|
| 230 | - $this->sourceLine = null; |
|
| 231 | - } elseif ($offset === -3) { |
|
| 232 | - $this->sourceColumn = null; |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Returns true if the number is unitless |
|
| 238 | - * |
|
| 239 | - * @return boolean |
|
| 240 | - */ |
|
| 241 | - public function unitless() |
|
| 242 | - { |
|
| 243 | - return ! array_sum($this->units); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Test if a number can be normalized in a base unit |
|
| 248 | - * ie if its units are homogeneous |
|
| 249 | - * |
|
| 250 | - * @return boolean |
|
| 251 | - */ |
|
| 252 | - public function isNormalizable() |
|
| 253 | - { |
|
| 254 | - if ($this->unitless()) { |
|
| 255 | - return false; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - $baseUnit = null; |
|
| 259 | - |
|
| 260 | - foreach ($this->units as $unit => $exp) { |
|
| 261 | - $b = $this->findBaseUnit($unit); |
|
| 262 | - |
|
| 263 | - if (\is_null($baseUnit)) { |
|
| 264 | - $baseUnit = $b; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - if (\is_null($b) or $b !== $baseUnit) { |
|
| 268 | - return false; |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - return $baseUnit; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Returns unit(s) as the product of numerator units divided by the product of denominator units |
|
| 277 | - * |
|
| 278 | - * @return string |
|
| 279 | - */ |
|
| 280 | - public function unitStr() |
|
| 281 | - { |
|
| 282 | - $numerators = []; |
|
| 283 | - $denominators = []; |
|
| 284 | - |
|
| 285 | - foreach ($this->units as $unit => $unitSize) { |
|
| 286 | - if ($unitSize > 0) { |
|
| 287 | - $numerators = array_pad($numerators, \count($numerators) + $unitSize, $unit); |
|
| 288 | - continue; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - if ($unitSize < 0) { |
|
| 292 | - $denominators = array_pad($denominators, \count($denominators) - $unitSize, $unit); |
|
| 293 | - continue; |
|
| 294 | - } |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - return implode('*', $numerators) . (\count($denominators) ? '/' . implode('*', $denominators) : ''); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Output number |
|
| 302 | - * |
|
| 303 | - * @param \ScssPhp\ScssPhp\Compiler $compiler |
|
| 304 | - * |
|
| 305 | - * @return string |
|
| 306 | - */ |
|
| 307 | - public function output(Compiler $compiler = null) |
|
| 308 | - { |
|
| 309 | - $dimension = round($this->dimension, self::PRECISION); |
|
| 310 | - |
|
| 311 | - $units = array_filter($this->units, function ($unitSize) { |
|
| 312 | - return $unitSize; |
|
| 313 | - }); |
|
| 314 | - |
|
| 315 | - if (\count($units) > 1 && array_sum($units) === 0) { |
|
| 316 | - $dimension = $this->dimension; |
|
| 317 | - $units = []; |
|
| 318 | - |
|
| 319 | - $this->normalizeUnits($dimension, $units); |
|
| 320 | - |
|
| 321 | - $dimension = round($dimension, self::PRECISION); |
|
| 322 | - $units = array_filter($units, function ($unitSize) { |
|
| 323 | - return $unitSize; |
|
| 324 | - }); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - $unitSize = array_sum($units); |
|
| 328 | - |
|
| 329 | - if ($compiler && ($unitSize > 1 || $unitSize < 0 || \count($units) > 1)) { |
|
| 330 | - $this->units = $units; |
|
| 331 | - $unit = $this->unitStr(); |
|
| 332 | - } else { |
|
| 333 | - reset($units); |
|
| 334 | - $unit = key($units); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - $dimension = number_format($dimension, self::PRECISION, '.', ''); |
|
| 338 | - |
|
| 339 | - return (self::PRECISION ? rtrim(rtrim($dimension, '0'), '.') : $dimension) . $unit; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * {@inheritdoc} |
|
| 344 | - */ |
|
| 345 | - public function __toString() |
|
| 346 | - { |
|
| 347 | - return $this->output(); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Normalize units |
|
| 352 | - * |
|
| 353 | - * @param integer|float $dimension |
|
| 354 | - * @param array $units |
|
| 355 | - * @param string $baseUnit |
|
| 356 | - */ |
|
| 357 | - private function normalizeUnits(&$dimension, &$units, $baseUnit = null) |
|
| 358 | - { |
|
| 359 | - $dimension = $this->dimension; |
|
| 360 | - $units = []; |
|
| 361 | - |
|
| 362 | - foreach ($this->units as $unit => $exp) { |
|
| 363 | - if (! $baseUnit) { |
|
| 364 | - $baseUnit = $this->findBaseUnit($unit); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - if ($baseUnit && isset(static::$unitTable[$baseUnit][$unit])) { |
|
| 368 | - $factor = pow(static::$unitTable[$baseUnit][$unit], $exp); |
|
| 369 | - |
|
| 370 | - $unit = $baseUnit; |
|
| 371 | - $dimension /= $factor; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - $units[$unit] = $exp + (isset($units[$unit]) ? $units[$unit] : 0); |
|
| 375 | - } |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Find the base unit family for a given unit |
|
| 380 | - * |
|
| 381 | - * @param string $unit |
|
| 382 | - * |
|
| 383 | - * @return string|null |
|
| 384 | - */ |
|
| 385 | - private function findBaseUnit($unit) |
|
| 386 | - { |
|
| 387 | - foreach (static::$unitTable as $baseUnit => $unitVariants) { |
|
| 388 | - if (isset($unitVariants[$unit])) { |
|
| 389 | - return $baseUnit; |
|
| 390 | - } |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - return null; |
|
| 394 | - } |
|
| 32 | + const PRECISION = 10; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var integer |
|
| 36 | + * @deprecated use {Number::PRECISION} instead to read the precision. Configuring it is not supported anymore. |
|
| 37 | + */ |
|
| 38 | + public static $precision = self::PRECISION; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @see http://www.w3.org/TR/2012/WD-css3-values-20120308/ |
|
| 42 | + * |
|
| 43 | + * @var array |
|
| 44 | + */ |
|
| 45 | + protected static $unitTable = [ |
|
| 46 | + 'in' => [ |
|
| 47 | + 'in' => 1, |
|
| 48 | + 'pc' => 6, |
|
| 49 | + 'pt' => 72, |
|
| 50 | + 'px' => 96, |
|
| 51 | + 'cm' => 2.54, |
|
| 52 | + 'mm' => 25.4, |
|
| 53 | + 'q' => 101.6, |
|
| 54 | + ], |
|
| 55 | + 'turn' => [ |
|
| 56 | + 'deg' => 360, |
|
| 57 | + 'grad' => 400, |
|
| 58 | + 'rad' => 6.28318530717958647692528676, // 2 * M_PI |
|
| 59 | + 'turn' => 1, |
|
| 60 | + ], |
|
| 61 | + 's' => [ |
|
| 62 | + 's' => 1, |
|
| 63 | + 'ms' => 1000, |
|
| 64 | + ], |
|
| 65 | + 'Hz' => [ |
|
| 66 | + 'Hz' => 1, |
|
| 67 | + 'kHz' => 0.001, |
|
| 68 | + ], |
|
| 69 | + 'dpi' => [ |
|
| 70 | + 'dpi' => 1, |
|
| 71 | + 'dpcm' => 1 / 2.54, |
|
| 72 | + 'dppx' => 1 / 96, |
|
| 73 | + ], |
|
| 74 | + ]; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @var integer|float |
|
| 78 | + */ |
|
| 79 | + public $dimension; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @var array |
|
| 83 | + */ |
|
| 84 | + public $units; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Initialize number |
|
| 88 | + * |
|
| 89 | + * @param mixed $dimension |
|
| 90 | + * @param mixed $initialUnit |
|
| 91 | + */ |
|
| 92 | + public function __construct($dimension, $initialUnit) |
|
| 93 | + { |
|
| 94 | + $this->type = Type::T_NUMBER; |
|
| 95 | + $this->dimension = $dimension; |
|
| 96 | + $this->units = \is_array($initialUnit) |
|
| 97 | + ? $initialUnit |
|
| 98 | + : ($initialUnit ? [$initialUnit => 1] |
|
| 99 | + : []); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Coerce number to target units |
|
| 104 | + * |
|
| 105 | + * @param array $units |
|
| 106 | + * |
|
| 107 | + * @return \ScssPhp\ScssPhp\Node\Number |
|
| 108 | + */ |
|
| 109 | + public function coerce($units) |
|
| 110 | + { |
|
| 111 | + if ($this->unitless()) { |
|
| 112 | + return new Number($this->dimension, $units); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $dimension = $this->dimension; |
|
| 116 | + |
|
| 117 | + if (\count($units)) { |
|
| 118 | + $baseUnit = array_keys($units); |
|
| 119 | + $baseUnit = reset($baseUnit); |
|
| 120 | + $baseUnit = $this->findBaseUnit($baseUnit); |
|
| 121 | + if ($baseUnit && isset(static::$unitTable[$baseUnit])) { |
|
| 122 | + foreach (static::$unitTable[$baseUnit] as $unit => $conv) { |
|
| 123 | + $from = isset($this->units[$unit]) ? $this->units[$unit] : 0; |
|
| 124 | + $to = isset($units[$unit]) ? $units[$unit] : 0; |
|
| 125 | + $factor = pow($conv, $from - $to); |
|
| 126 | + $dimension /= $factor; |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + return new Number($dimension, $units); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Normalize number |
|
| 136 | + * |
|
| 137 | + * @return \ScssPhp\ScssPhp\Node\Number |
|
| 138 | + */ |
|
| 139 | + public function normalize() |
|
| 140 | + { |
|
| 141 | + $dimension = $this->dimension; |
|
| 142 | + $units = []; |
|
| 143 | + |
|
| 144 | + $this->normalizeUnits($dimension, $units); |
|
| 145 | + |
|
| 146 | + return new Number($dimension, $units); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * {@inheritdoc} |
|
| 151 | + */ |
|
| 152 | + public function offsetExists($offset) |
|
| 153 | + { |
|
| 154 | + if ($offset === -3) { |
|
| 155 | + return ! \is_null($this->sourceColumn); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + if ($offset === -2) { |
|
| 159 | + return ! \is_null($this->sourceLine); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + if ( |
|
| 163 | + $offset === -1 || |
|
| 164 | + $offset === 0 || |
|
| 165 | + $offset === 1 || |
|
| 166 | + $offset === 2 |
|
| 167 | + ) { |
|
| 168 | + return true; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + return false; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * {@inheritdoc} |
|
| 176 | + */ |
|
| 177 | + public function offsetGet($offset) |
|
| 178 | + { |
|
| 179 | + switch ($offset) { |
|
| 180 | + case -3: |
|
| 181 | + return $this->sourceColumn; |
|
| 182 | + |
|
| 183 | + case -2: |
|
| 184 | + return $this->sourceLine; |
|
| 185 | + |
|
| 186 | + case -1: |
|
| 187 | + return $this->sourceIndex; |
|
| 188 | + |
|
| 189 | + case 0: |
|
| 190 | + return $this->type; |
|
| 191 | + |
|
| 192 | + case 1: |
|
| 193 | + return $this->dimension; |
|
| 194 | + |
|
| 195 | + case 2: |
|
| 196 | + return $this->units; |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * {@inheritdoc} |
|
| 202 | + */ |
|
| 203 | + public function offsetSet($offset, $value) |
|
| 204 | + { |
|
| 205 | + if ($offset === 1) { |
|
| 206 | + $this->dimension = $value; |
|
| 207 | + } elseif ($offset === 2) { |
|
| 208 | + $this->units = $value; |
|
| 209 | + } elseif ($offset == -1) { |
|
| 210 | + $this->sourceIndex = $value; |
|
| 211 | + } elseif ($offset == -2) { |
|
| 212 | + $this->sourceLine = $value; |
|
| 213 | + } elseif ($offset == -3) { |
|
| 214 | + $this->sourceColumn = $value; |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * {@inheritdoc} |
|
| 220 | + */ |
|
| 221 | + public function offsetUnset($offset) |
|
| 222 | + { |
|
| 223 | + if ($offset === 1) { |
|
| 224 | + $this->dimension = null; |
|
| 225 | + } elseif ($offset === 2) { |
|
| 226 | + $this->units = null; |
|
| 227 | + } elseif ($offset === -1) { |
|
| 228 | + $this->sourceIndex = null; |
|
| 229 | + } elseif ($offset === -2) { |
|
| 230 | + $this->sourceLine = null; |
|
| 231 | + } elseif ($offset === -3) { |
|
| 232 | + $this->sourceColumn = null; |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Returns true if the number is unitless |
|
| 238 | + * |
|
| 239 | + * @return boolean |
|
| 240 | + */ |
|
| 241 | + public function unitless() |
|
| 242 | + { |
|
| 243 | + return ! array_sum($this->units); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Test if a number can be normalized in a base unit |
|
| 248 | + * ie if its units are homogeneous |
|
| 249 | + * |
|
| 250 | + * @return boolean |
|
| 251 | + */ |
|
| 252 | + public function isNormalizable() |
|
| 253 | + { |
|
| 254 | + if ($this->unitless()) { |
|
| 255 | + return false; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + $baseUnit = null; |
|
| 259 | + |
|
| 260 | + foreach ($this->units as $unit => $exp) { |
|
| 261 | + $b = $this->findBaseUnit($unit); |
|
| 262 | + |
|
| 263 | + if (\is_null($baseUnit)) { |
|
| 264 | + $baseUnit = $b; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + if (\is_null($b) or $b !== $baseUnit) { |
|
| 268 | + return false; |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + return $baseUnit; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Returns unit(s) as the product of numerator units divided by the product of denominator units |
|
| 277 | + * |
|
| 278 | + * @return string |
|
| 279 | + */ |
|
| 280 | + public function unitStr() |
|
| 281 | + { |
|
| 282 | + $numerators = []; |
|
| 283 | + $denominators = []; |
|
| 284 | + |
|
| 285 | + foreach ($this->units as $unit => $unitSize) { |
|
| 286 | + if ($unitSize > 0) { |
|
| 287 | + $numerators = array_pad($numerators, \count($numerators) + $unitSize, $unit); |
|
| 288 | + continue; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + if ($unitSize < 0) { |
|
| 292 | + $denominators = array_pad($denominators, \count($denominators) - $unitSize, $unit); |
|
| 293 | + continue; |
|
| 294 | + } |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + return implode('*', $numerators) . (\count($denominators) ? '/' . implode('*', $denominators) : ''); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Output number |
|
| 302 | + * |
|
| 303 | + * @param \ScssPhp\ScssPhp\Compiler $compiler |
|
| 304 | + * |
|
| 305 | + * @return string |
|
| 306 | + */ |
|
| 307 | + public function output(Compiler $compiler = null) |
|
| 308 | + { |
|
| 309 | + $dimension = round($this->dimension, self::PRECISION); |
|
| 310 | + |
|
| 311 | + $units = array_filter($this->units, function ($unitSize) { |
|
| 312 | + return $unitSize; |
|
| 313 | + }); |
|
| 314 | + |
|
| 315 | + if (\count($units) > 1 && array_sum($units) === 0) { |
|
| 316 | + $dimension = $this->dimension; |
|
| 317 | + $units = []; |
|
| 318 | + |
|
| 319 | + $this->normalizeUnits($dimension, $units); |
|
| 320 | + |
|
| 321 | + $dimension = round($dimension, self::PRECISION); |
|
| 322 | + $units = array_filter($units, function ($unitSize) { |
|
| 323 | + return $unitSize; |
|
| 324 | + }); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + $unitSize = array_sum($units); |
|
| 328 | + |
|
| 329 | + if ($compiler && ($unitSize > 1 || $unitSize < 0 || \count($units) > 1)) { |
|
| 330 | + $this->units = $units; |
|
| 331 | + $unit = $this->unitStr(); |
|
| 332 | + } else { |
|
| 333 | + reset($units); |
|
| 334 | + $unit = key($units); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + $dimension = number_format($dimension, self::PRECISION, '.', ''); |
|
| 338 | + |
|
| 339 | + return (self::PRECISION ? rtrim(rtrim($dimension, '0'), '.') : $dimension) . $unit; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * {@inheritdoc} |
|
| 344 | + */ |
|
| 345 | + public function __toString() |
|
| 346 | + { |
|
| 347 | + return $this->output(); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Normalize units |
|
| 352 | + * |
|
| 353 | + * @param integer|float $dimension |
|
| 354 | + * @param array $units |
|
| 355 | + * @param string $baseUnit |
|
| 356 | + */ |
|
| 357 | + private function normalizeUnits(&$dimension, &$units, $baseUnit = null) |
|
| 358 | + { |
|
| 359 | + $dimension = $this->dimension; |
|
| 360 | + $units = []; |
|
| 361 | + |
|
| 362 | + foreach ($this->units as $unit => $exp) { |
|
| 363 | + if (! $baseUnit) { |
|
| 364 | + $baseUnit = $this->findBaseUnit($unit); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + if ($baseUnit && isset(static::$unitTable[$baseUnit][$unit])) { |
|
| 368 | + $factor = pow(static::$unitTable[$baseUnit][$unit], $exp); |
|
| 369 | + |
|
| 370 | + $unit = $baseUnit; |
|
| 371 | + $dimension /= $factor; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + $units[$unit] = $exp + (isset($units[$unit]) ? $units[$unit] : 0); |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Find the base unit family for a given unit |
|
| 380 | + * |
|
| 381 | + * @param string $unit |
|
| 382 | + * |
|
| 383 | + * @return string|null |
|
| 384 | + */ |
|
| 385 | + private function findBaseUnit($unit) |
|
| 386 | + { |
|
| 387 | + foreach (static::$unitTable as $baseUnit => $unitVariants) { |
|
| 388 | + if (isset($unitVariants[$unit])) { |
|
| 389 | + return $baseUnit; |
|
| 390 | + } |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + return null; |
|
| 394 | + } |
|
| 395 | 395 | } |
@@ -152,11 +152,11 @@ discard block |
||
| 152 | 152 | public function offsetExists($offset) |
| 153 | 153 | { |
| 154 | 154 | if ($offset === -3) { |
| 155 | - return ! \is_null($this->sourceColumn); |
|
| 155 | + return !\is_null($this->sourceColumn); |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | if ($offset === -2) { |
| 159 | - return ! \is_null($this->sourceLine); |
|
| 159 | + return !\is_null($this->sourceLine); |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | if ( |
@@ -240,7 +240,7 @@ discard block |
||
| 240 | 240 | */ |
| 241 | 241 | public function unitless() |
| 242 | 242 | { |
| 243 | - return ! array_sum($this->units); |
|
| 243 | + return !array_sum($this->units); |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | /** |
@@ -308,7 +308,7 @@ discard block |
||
| 308 | 308 | { |
| 309 | 309 | $dimension = round($this->dimension, self::PRECISION); |
| 310 | 310 | |
| 311 | - $units = array_filter($this->units, function ($unitSize) { |
|
| 311 | + $units = array_filter($this->units, function($unitSize) { |
|
| 312 | 312 | return $unitSize; |
| 313 | 313 | }); |
| 314 | 314 | |
@@ -319,7 +319,7 @@ discard block |
||
| 319 | 319 | $this->normalizeUnits($dimension, $units); |
| 320 | 320 | |
| 321 | 321 | $dimension = round($dimension, self::PRECISION); |
| 322 | - $units = array_filter($units, function ($unitSize) { |
|
| 322 | + $units = array_filter($units, function($unitSize) { |
|
| 323 | 323 | return $unitSize; |
| 324 | 324 | }); |
| 325 | 325 | } |
@@ -360,7 +360,7 @@ discard block |
||
| 360 | 360 | $units = []; |
| 361 | 361 | |
| 362 | 362 | foreach ($this->units as $unit => $exp) { |
| 363 | - if (! $baseUnit) { |
|
| 363 | + if (!$baseUnit) { |
|
| 364 | 364 | $baseUnit = $this->findBaseUnit($unit); |
| 365 | 365 | } |
| 366 | 366 | |
@@ -6,7 +6,7 @@ |
||
| 6 | 6 | $baseDir = dirname($vendorDir); |
| 7 | 7 | |
| 8 | 8 | return array( |
| 9 | - 'ScssPhp\\ScssPhp\\' => array($vendorDir . '/scssphp/scssphp/src'), |
|
| 10 | - 'SCSS\\Tests\\' => array($vendorDir . '/mediawiki/scss/tests/phpunit'), |
|
| 11 | - 'SCSS\\' => array($vendorDir . '/mediawiki/scss/src'), |
|
| 9 | + 'ScssPhp\\ScssPhp\\' => array($vendorDir . '/scssphp/scssphp/src'), |
|
| 10 | + 'SCSS\\Tests\\' => array($vendorDir . '/mediawiki/scss/tests/phpunit'), |
|
| 11 | + 'SCSS\\' => array($vendorDir . '/mediawiki/scss/src'), |
|
| 12 | 12 | ); |
@@ -6,36 +6,36 @@ |
||
| 6 | 6 | |
| 7 | 7 | class ComposerStaticInitb8266408d82c92820c2131f83fb7eb6e |
| 8 | 8 | { |
| 9 | - public static $prefixLengthsPsr4 = array ( |
|
| 10 | - 'S' => |
|
| 11 | - array ( |
|
| 12 | - 'ScssPhp\\ScssPhp\\' => 16, |
|
| 13 | - 'SCSS\\Tests\\' => 11, |
|
| 14 | - 'SCSS\\' => 5, |
|
| 15 | - ), |
|
| 16 | - ); |
|
| 9 | + public static $prefixLengthsPsr4 = array ( |
|
| 10 | + 'S' => |
|
| 11 | + array ( |
|
| 12 | + 'ScssPhp\\ScssPhp\\' => 16, |
|
| 13 | + 'SCSS\\Tests\\' => 11, |
|
| 14 | + 'SCSS\\' => 5, |
|
| 15 | + ), |
|
| 16 | + ); |
|
| 17 | 17 | |
| 18 | - public static $prefixDirsPsr4 = array ( |
|
| 19 | - 'ScssPhp\\ScssPhp\\' => |
|
| 20 | - array ( |
|
| 21 | - 0 => __DIR__ . '/..' . '/scssphp/scssphp/src', |
|
| 22 | - ), |
|
| 23 | - 'SCSS\\Tests\\' => |
|
| 24 | - array ( |
|
| 25 | - 0 => __DIR__ . '/..' . '/mediawiki/scss/tests/phpunit', |
|
| 26 | - ), |
|
| 27 | - 'SCSS\\' => |
|
| 28 | - array ( |
|
| 29 | - 0 => __DIR__ . '/..' . '/mediawiki/scss/src', |
|
| 30 | - ), |
|
| 31 | - ); |
|
| 18 | + public static $prefixDirsPsr4 = array ( |
|
| 19 | + 'ScssPhp\\ScssPhp\\' => |
|
| 20 | + array ( |
|
| 21 | + 0 => __DIR__ . '/..' . '/scssphp/scssphp/src', |
|
| 22 | + ), |
|
| 23 | + 'SCSS\\Tests\\' => |
|
| 24 | + array ( |
|
| 25 | + 0 => __DIR__ . '/..' . '/mediawiki/scss/tests/phpunit', |
|
| 26 | + ), |
|
| 27 | + 'SCSS\\' => |
|
| 28 | + array ( |
|
| 29 | + 0 => __DIR__ . '/..' . '/mediawiki/scss/src', |
|
| 30 | + ), |
|
| 31 | + ); |
|
| 32 | 32 | |
| 33 | - public static function getInitializer(ClassLoader $loader) |
|
| 34 | - { |
|
| 35 | - return \Closure::bind(function () use ($loader) { |
|
| 36 | - $loader->prefixLengthsPsr4 = ComposerStaticInitb8266408d82c92820c2131f83fb7eb6e::$prefixLengthsPsr4; |
|
| 37 | - $loader->prefixDirsPsr4 = ComposerStaticInitb8266408d82c92820c2131f83fb7eb6e::$prefixDirsPsr4; |
|
| 33 | + public static function getInitializer(ClassLoader $loader) |
|
| 34 | + { |
|
| 35 | + return \Closure::bind(function () use ($loader) { |
|
| 36 | + $loader->prefixLengthsPsr4 = ComposerStaticInitb8266408d82c92820c2131f83fb7eb6e::$prefixLengthsPsr4; |
|
| 37 | + $loader->prefixDirsPsr4 = ComposerStaticInitb8266408d82c92820c2131f83fb7eb6e::$prefixDirsPsr4; |
|
| 38 | 38 | |
| 39 | - }, null, ClassLoader::class); |
|
| 40 | - } |
|
| 39 | + }, null, ClassLoader::class); |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -6,33 +6,33 @@ |
||
| 6 | 6 | |
| 7 | 7 | class ComposerStaticInitb8266408d82c92820c2131f83fb7eb6e |
| 8 | 8 | { |
| 9 | - public static $prefixLengthsPsr4 = array ( |
|
| 9 | + public static $prefixLengthsPsr4 = array( |
|
| 10 | 10 | 'S' => |
| 11 | - array ( |
|
| 11 | + array( |
|
| 12 | 12 | 'ScssPhp\\ScssPhp\\' => 16, |
| 13 | 13 | 'SCSS\\Tests\\' => 11, |
| 14 | 14 | 'SCSS\\' => 5, |
| 15 | 15 | ), |
| 16 | 16 | ); |
| 17 | 17 | |
| 18 | - public static $prefixDirsPsr4 = array ( |
|
| 18 | + public static $prefixDirsPsr4 = array( |
|
| 19 | 19 | 'ScssPhp\\ScssPhp\\' => |
| 20 | - array ( |
|
| 20 | + array( |
|
| 21 | 21 | 0 => __DIR__ . '/..' . '/scssphp/scssphp/src', |
| 22 | 22 | ), |
| 23 | 23 | 'SCSS\\Tests\\' => |
| 24 | - array ( |
|
| 24 | + array( |
|
| 25 | 25 | 0 => __DIR__ . '/..' . '/mediawiki/scss/tests/phpunit', |
| 26 | 26 | ), |
| 27 | 27 | 'SCSS\\' => |
| 28 | - array ( |
|
| 28 | + array( |
|
| 29 | 29 | 0 => __DIR__ . '/..' . '/mediawiki/scss/src', |
| 30 | 30 | ), |
| 31 | 31 | ); |
| 32 | 32 | |
| 33 | 33 | public static function getInitializer(ClassLoader $loader) |
| 34 | 34 | { |
| 35 | - return \Closure::bind(function () use ($loader) { |
|
| 35 | + return \Closure::bind(function() use ($loader) { |
|
| 36 | 36 | $loader->prefixLengthsPsr4 = ComposerStaticInitb8266408d82c92820c2131f83fb7eb6e::$prefixLengthsPsr4; |
| 37 | 37 | $loader->prefixDirsPsr4 = ComposerStaticInitb8266408d82c92820c2131f83fb7eb6e::$prefixDirsPsr4; |
| 38 | 38 | |
@@ -4,49 +4,49 @@ |
||
| 4 | 4 | |
| 5 | 5 | class ComposerAutoloaderInitb8266408d82c92820c2131f83fb7eb6e |
| 6 | 6 | { |
| 7 | - private static $loader; |
|
| 8 | - |
|
| 9 | - public static function loadClassLoader($class) |
|
| 10 | - { |
|
| 11 | - if ('Composer\Autoload\ClassLoader' === $class) { |
|
| 12 | - require __DIR__ . '/ClassLoader.php'; |
|
| 13 | - } |
|
| 14 | - } |
|
| 15 | - |
|
| 16 | - public static function getLoader() |
|
| 17 | - { |
|
| 18 | - if (null !== self::$loader) { |
|
| 19 | - return self::$loader; |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - spl_autoload_register(array('ComposerAutoloaderInitb8266408d82c92820c2131f83fb7eb6e', 'loadClassLoader'), true, true); |
|
| 23 | - self::$loader = $loader = new \Composer\Autoload\ClassLoader(); |
|
| 24 | - spl_autoload_unregister(array('ComposerAutoloaderInitb8266408d82c92820c2131f83fb7eb6e', 'loadClassLoader')); |
|
| 25 | - |
|
| 26 | - $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded()); |
|
| 27 | - if ($useStaticLoader) { |
|
| 28 | - require_once __DIR__ . '/autoload_static.php'; |
|
| 29 | - |
|
| 30 | - call_user_func(\Composer\Autoload\ComposerStaticInitb8266408d82c92820c2131f83fb7eb6e::getInitializer($loader)); |
|
| 31 | - } else { |
|
| 32 | - $map = require __DIR__ . '/autoload_namespaces.php'; |
|
| 33 | - foreach ($map as $namespace => $path) { |
|
| 34 | - $loader->set($namespace, $path); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - $map = require __DIR__ . '/autoload_psr4.php'; |
|
| 38 | - foreach ($map as $namespace => $path) { |
|
| 39 | - $loader->setPsr4($namespace, $path); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - $classMap = require __DIR__ . '/autoload_classmap.php'; |
|
| 43 | - if ($classMap) { |
|
| 44 | - $loader->addClassMap($classMap); |
|
| 45 | - } |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - $loader->register(true); |
|
| 49 | - |
|
| 50 | - return $loader; |
|
| 51 | - } |
|
| 7 | + private static $loader; |
|
| 8 | + |
|
| 9 | + public static function loadClassLoader($class) |
|
| 10 | + { |
|
| 11 | + if ('Composer\Autoload\ClassLoader' === $class) { |
|
| 12 | + require __DIR__ . '/ClassLoader.php'; |
|
| 13 | + } |
|
| 14 | + } |
|
| 15 | + |
|
| 16 | + public static function getLoader() |
|
| 17 | + { |
|
| 18 | + if (null !== self::$loader) { |
|
| 19 | + return self::$loader; |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + spl_autoload_register(array('ComposerAutoloaderInitb8266408d82c92820c2131f83fb7eb6e', 'loadClassLoader'), true, true); |
|
| 23 | + self::$loader = $loader = new \Composer\Autoload\ClassLoader(); |
|
| 24 | + spl_autoload_unregister(array('ComposerAutoloaderInitb8266408d82c92820c2131f83fb7eb6e', 'loadClassLoader')); |
|
| 25 | + |
|
| 26 | + $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded()); |
|
| 27 | + if ($useStaticLoader) { |
|
| 28 | + require_once __DIR__ . '/autoload_static.php'; |
|
| 29 | + |
|
| 30 | + call_user_func(\Composer\Autoload\ComposerStaticInitb8266408d82c92820c2131f83fb7eb6e::getInitializer($loader)); |
|
| 31 | + } else { |
|
| 32 | + $map = require __DIR__ . '/autoload_namespaces.php'; |
|
| 33 | + foreach ($map as $namespace => $path) { |
|
| 34 | + $loader->set($namespace, $path); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + $map = require __DIR__ . '/autoload_psr4.php'; |
|
| 38 | + foreach ($map as $namespace => $path) { |
|
| 39 | + $loader->setPsr4($namespace, $path); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + $classMap = require __DIR__ . '/autoload_classmap.php'; |
|
| 43 | + if ($classMap) { |
|
| 44 | + $loader->addClassMap($classMap); |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + $loader->register(true); |
|
| 49 | + |
|
| 50 | + return $loader; |
|
| 51 | + } |
|
| 52 | 52 | } |
@@ -42,396 +42,396 @@ discard block |
||
| 42 | 42 | */ |
| 43 | 43 | class ClassLoader |
| 44 | 44 | { |
| 45 | - // PSR-4 |
|
| 46 | - private $prefixLengthsPsr4 = array(); |
|
| 47 | - private $prefixDirsPsr4 = array(); |
|
| 48 | - private $fallbackDirsPsr4 = array(); |
|
| 49 | - |
|
| 50 | - // PSR-0 |
|
| 51 | - private $prefixesPsr0 = array(); |
|
| 52 | - private $fallbackDirsPsr0 = array(); |
|
| 53 | - |
|
| 54 | - private $useIncludePath = false; |
|
| 55 | - private $classMap = array(); |
|
| 56 | - private $classMapAuthoritative = false; |
|
| 57 | - private $missingClasses = array(); |
|
| 58 | - private $apcuPrefix; |
|
| 59 | - |
|
| 60 | - public function getPrefixes() |
|
| 61 | - { |
|
| 62 | - if (!empty($this->prefixesPsr0)) { |
|
| 63 | - return call_user_func_array('array_merge', $this->prefixesPsr0); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - return array(); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - public function getPrefixesPsr4() |
|
| 70 | - { |
|
| 71 | - return $this->prefixDirsPsr4; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - public function getFallbackDirs() |
|
| 75 | - { |
|
| 76 | - return $this->fallbackDirsPsr0; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - public function getFallbackDirsPsr4() |
|
| 80 | - { |
|
| 81 | - return $this->fallbackDirsPsr4; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public function getClassMap() |
|
| 85 | - { |
|
| 86 | - return $this->classMap; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @param array $classMap Class to filename map |
|
| 91 | - */ |
|
| 92 | - public function addClassMap(array $classMap) |
|
| 93 | - { |
|
| 94 | - if ($this->classMap) { |
|
| 95 | - $this->classMap = array_merge($this->classMap, $classMap); |
|
| 96 | - } else { |
|
| 97 | - $this->classMap = $classMap; |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Registers a set of PSR-0 directories for a given prefix, either |
|
| 103 | - * appending or prepending to the ones previously set for this prefix. |
|
| 104 | - * |
|
| 105 | - * @param string $prefix The prefix |
|
| 106 | - * @param array|string $paths The PSR-0 root directories |
|
| 107 | - * @param bool $prepend Whether to prepend the directories |
|
| 108 | - */ |
|
| 109 | - public function add($prefix, $paths, $prepend = false) |
|
| 110 | - { |
|
| 111 | - if (!$prefix) { |
|
| 112 | - if ($prepend) { |
|
| 113 | - $this->fallbackDirsPsr0 = array_merge( |
|
| 114 | - (array) $paths, |
|
| 115 | - $this->fallbackDirsPsr0 |
|
| 116 | - ); |
|
| 117 | - } else { |
|
| 118 | - $this->fallbackDirsPsr0 = array_merge( |
|
| 119 | - $this->fallbackDirsPsr0, |
|
| 120 | - (array) $paths |
|
| 121 | - ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $first = $prefix[0]; |
|
| 128 | - if (!isset($this->prefixesPsr0[$first][$prefix])) { |
|
| 129 | - $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
|
| 130 | - |
|
| 131 | - return; |
|
| 132 | - } |
|
| 133 | - if ($prepend) { |
|
| 134 | - $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
| 135 | - (array) $paths, |
|
| 136 | - $this->prefixesPsr0[$first][$prefix] |
|
| 137 | - ); |
|
| 138 | - } else { |
|
| 139 | - $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
| 140 | - $this->prefixesPsr0[$first][$prefix], |
|
| 141 | - (array) $paths |
|
| 142 | - ); |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Registers a set of PSR-4 directories for a given namespace, either |
|
| 148 | - * appending or prepending to the ones previously set for this namespace. |
|
| 149 | - * |
|
| 150 | - * @param string $prefix The prefix/namespace, with trailing '\\' |
|
| 151 | - * @param array|string $paths The PSR-4 base directories |
|
| 152 | - * @param bool $prepend Whether to prepend the directories |
|
| 153 | - * |
|
| 154 | - * @throws \InvalidArgumentException |
|
| 155 | - */ |
|
| 156 | - public function addPsr4($prefix, $paths, $prepend = false) |
|
| 157 | - { |
|
| 158 | - if (!$prefix) { |
|
| 159 | - // Register directories for the root namespace. |
|
| 160 | - if ($prepend) { |
|
| 161 | - $this->fallbackDirsPsr4 = array_merge( |
|
| 162 | - (array) $paths, |
|
| 163 | - $this->fallbackDirsPsr4 |
|
| 164 | - ); |
|
| 165 | - } else { |
|
| 166 | - $this->fallbackDirsPsr4 = array_merge( |
|
| 167 | - $this->fallbackDirsPsr4, |
|
| 168 | - (array) $paths |
|
| 169 | - ); |
|
| 170 | - } |
|
| 171 | - } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
|
| 172 | - // Register directories for a new namespace. |
|
| 173 | - $length = strlen($prefix); |
|
| 174 | - if ('\\' !== $prefix[$length - 1]) { |
|
| 175 | - throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
| 176 | - } |
|
| 177 | - $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
| 178 | - $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
| 179 | - } elseif ($prepend) { |
|
| 180 | - // Prepend directories for an already registered namespace. |
|
| 181 | - $this->prefixDirsPsr4[$prefix] = array_merge( |
|
| 182 | - (array) $paths, |
|
| 183 | - $this->prefixDirsPsr4[$prefix] |
|
| 184 | - ); |
|
| 185 | - } else { |
|
| 186 | - // Append directories for an already registered namespace. |
|
| 187 | - $this->prefixDirsPsr4[$prefix] = array_merge( |
|
| 188 | - $this->prefixDirsPsr4[$prefix], |
|
| 189 | - (array) $paths |
|
| 190 | - ); |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Registers a set of PSR-0 directories for a given prefix, |
|
| 196 | - * replacing any others previously set for this prefix. |
|
| 197 | - * |
|
| 198 | - * @param string $prefix The prefix |
|
| 199 | - * @param array|string $paths The PSR-0 base directories |
|
| 200 | - */ |
|
| 201 | - public function set($prefix, $paths) |
|
| 202 | - { |
|
| 203 | - if (!$prefix) { |
|
| 204 | - $this->fallbackDirsPsr0 = (array) $paths; |
|
| 205 | - } else { |
|
| 206 | - $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Registers a set of PSR-4 directories for a given namespace, |
|
| 212 | - * replacing any others previously set for this namespace. |
|
| 213 | - * |
|
| 214 | - * @param string $prefix The prefix/namespace, with trailing '\\' |
|
| 215 | - * @param array|string $paths The PSR-4 base directories |
|
| 216 | - * |
|
| 217 | - * @throws \InvalidArgumentException |
|
| 218 | - */ |
|
| 219 | - public function setPsr4($prefix, $paths) |
|
| 220 | - { |
|
| 221 | - if (!$prefix) { |
|
| 222 | - $this->fallbackDirsPsr4 = (array) $paths; |
|
| 223 | - } else { |
|
| 224 | - $length = strlen($prefix); |
|
| 225 | - if ('\\' !== $prefix[$length - 1]) { |
|
| 226 | - throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
| 227 | - } |
|
| 228 | - $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
| 229 | - $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Turns on searching the include path for class files. |
|
| 235 | - * |
|
| 236 | - * @param bool $useIncludePath |
|
| 237 | - */ |
|
| 238 | - public function setUseIncludePath($useIncludePath) |
|
| 239 | - { |
|
| 240 | - $this->useIncludePath = $useIncludePath; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Can be used to check if the autoloader uses the include path to check |
|
| 245 | - * for classes. |
|
| 246 | - * |
|
| 247 | - * @return bool |
|
| 248 | - */ |
|
| 249 | - public function getUseIncludePath() |
|
| 250 | - { |
|
| 251 | - return $this->useIncludePath; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Turns off searching the prefix and fallback directories for classes |
|
| 256 | - * that have not been registered with the class map. |
|
| 257 | - * |
|
| 258 | - * @param bool $classMapAuthoritative |
|
| 259 | - */ |
|
| 260 | - public function setClassMapAuthoritative($classMapAuthoritative) |
|
| 261 | - { |
|
| 262 | - $this->classMapAuthoritative = $classMapAuthoritative; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * Should class lookup fail if not found in the current class map? |
|
| 267 | - * |
|
| 268 | - * @return bool |
|
| 269 | - */ |
|
| 270 | - public function isClassMapAuthoritative() |
|
| 271 | - { |
|
| 272 | - return $this->classMapAuthoritative; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * APCu prefix to use to cache found/not-found classes, if the extension is enabled. |
|
| 277 | - * |
|
| 278 | - * @param string|null $apcuPrefix |
|
| 279 | - */ |
|
| 280 | - public function setApcuPrefix($apcuPrefix) |
|
| 281 | - { |
|
| 282 | - $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * The APCu prefix in use, or null if APCu caching is not enabled. |
|
| 287 | - * |
|
| 288 | - * @return string|null |
|
| 289 | - */ |
|
| 290 | - public function getApcuPrefix() |
|
| 291 | - { |
|
| 292 | - return $this->apcuPrefix; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * Registers this instance as an autoloader. |
|
| 297 | - * |
|
| 298 | - * @param bool $prepend Whether to prepend the autoloader or not |
|
| 299 | - */ |
|
| 300 | - public function register($prepend = false) |
|
| 301 | - { |
|
| 302 | - spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Unregisters this instance as an autoloader. |
|
| 307 | - */ |
|
| 308 | - public function unregister() |
|
| 309 | - { |
|
| 310 | - spl_autoload_unregister(array($this, 'loadClass')); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Loads the given class or interface. |
|
| 315 | - * |
|
| 316 | - * @param string $class The name of the class |
|
| 317 | - * @return bool|null True if loaded, null otherwise |
|
| 318 | - */ |
|
| 319 | - public function loadClass($class) |
|
| 320 | - { |
|
| 321 | - if ($file = $this->findFile($class)) { |
|
| 322 | - includeFile($file); |
|
| 323 | - |
|
| 324 | - return true; |
|
| 325 | - } |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Finds the path to the file where the class is defined. |
|
| 330 | - * |
|
| 331 | - * @param string $class The name of the class |
|
| 332 | - * |
|
| 333 | - * @return string|false The path if found, false otherwise |
|
| 334 | - */ |
|
| 335 | - public function findFile($class) |
|
| 336 | - { |
|
| 337 | - // class map lookup |
|
| 338 | - if (isset($this->classMap[$class])) { |
|
| 339 | - return $this->classMap[$class]; |
|
| 340 | - } |
|
| 341 | - if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { |
|
| 342 | - return false; |
|
| 343 | - } |
|
| 344 | - if (null !== $this->apcuPrefix) { |
|
| 345 | - $file = apcu_fetch($this->apcuPrefix.$class, $hit); |
|
| 346 | - if ($hit) { |
|
| 347 | - return $file; |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - $file = $this->findFileWithExtension($class, '.php'); |
|
| 352 | - |
|
| 353 | - // Search for Hack files if we are running on HHVM |
|
| 354 | - if (false === $file && defined('HHVM_VERSION')) { |
|
| 355 | - $file = $this->findFileWithExtension($class, '.hh'); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - if (null !== $this->apcuPrefix) { |
|
| 359 | - apcu_add($this->apcuPrefix.$class, $file); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - if (false === $file) { |
|
| 363 | - // Remember that this class does not exist. |
|
| 364 | - $this->missingClasses[$class] = true; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - return $file; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - private function findFileWithExtension($class, $ext) |
|
| 371 | - { |
|
| 372 | - // PSR-4 lookup |
|
| 373 | - $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
|
| 374 | - |
|
| 375 | - $first = $class[0]; |
|
| 376 | - if (isset($this->prefixLengthsPsr4[$first])) { |
|
| 377 | - $subPath = $class; |
|
| 378 | - while (false !== $lastPos = strrpos($subPath, '\\')) { |
|
| 379 | - $subPath = substr($subPath, 0, $lastPos); |
|
| 380 | - $search = $subPath . '\\'; |
|
| 381 | - if (isset($this->prefixDirsPsr4[$search])) { |
|
| 382 | - $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); |
|
| 383 | - foreach ($this->prefixDirsPsr4[$search] as $dir) { |
|
| 384 | - if (file_exists($file = $dir . $pathEnd)) { |
|
| 385 | - return $file; |
|
| 386 | - } |
|
| 387 | - } |
|
| 388 | - } |
|
| 389 | - } |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - // PSR-4 fallback dirs |
|
| 393 | - foreach ($this->fallbackDirsPsr4 as $dir) { |
|
| 394 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
|
| 395 | - return $file; |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - // PSR-0 lookup |
|
| 400 | - if (false !== $pos = strrpos($class, '\\')) { |
|
| 401 | - // namespaced class name |
|
| 402 | - $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
|
| 403 | - . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
|
| 404 | - } else { |
|
| 405 | - // PEAR-like class name |
|
| 406 | - $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - if (isset($this->prefixesPsr0[$first])) { |
|
| 410 | - foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
|
| 411 | - if (0 === strpos($class, $prefix)) { |
|
| 412 | - foreach ($dirs as $dir) { |
|
| 413 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
| 414 | - return $file; |
|
| 415 | - } |
|
| 416 | - } |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - // PSR-0 fallback dirs |
|
| 422 | - foreach ($this->fallbackDirsPsr0 as $dir) { |
|
| 423 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
| 424 | - return $file; |
|
| 425 | - } |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - // PSR-0 include paths. |
|
| 429 | - if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
|
| 430 | - return $file; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - return false; |
|
| 434 | - } |
|
| 45 | + // PSR-4 |
|
| 46 | + private $prefixLengthsPsr4 = array(); |
|
| 47 | + private $prefixDirsPsr4 = array(); |
|
| 48 | + private $fallbackDirsPsr4 = array(); |
|
| 49 | + |
|
| 50 | + // PSR-0 |
|
| 51 | + private $prefixesPsr0 = array(); |
|
| 52 | + private $fallbackDirsPsr0 = array(); |
|
| 53 | + |
|
| 54 | + private $useIncludePath = false; |
|
| 55 | + private $classMap = array(); |
|
| 56 | + private $classMapAuthoritative = false; |
|
| 57 | + private $missingClasses = array(); |
|
| 58 | + private $apcuPrefix; |
|
| 59 | + |
|
| 60 | + public function getPrefixes() |
|
| 61 | + { |
|
| 62 | + if (!empty($this->prefixesPsr0)) { |
|
| 63 | + return call_user_func_array('array_merge', $this->prefixesPsr0); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + return array(); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + public function getPrefixesPsr4() |
|
| 70 | + { |
|
| 71 | + return $this->prefixDirsPsr4; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + public function getFallbackDirs() |
|
| 75 | + { |
|
| 76 | + return $this->fallbackDirsPsr0; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + public function getFallbackDirsPsr4() |
|
| 80 | + { |
|
| 81 | + return $this->fallbackDirsPsr4; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public function getClassMap() |
|
| 85 | + { |
|
| 86 | + return $this->classMap; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @param array $classMap Class to filename map |
|
| 91 | + */ |
|
| 92 | + public function addClassMap(array $classMap) |
|
| 93 | + { |
|
| 94 | + if ($this->classMap) { |
|
| 95 | + $this->classMap = array_merge($this->classMap, $classMap); |
|
| 96 | + } else { |
|
| 97 | + $this->classMap = $classMap; |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Registers a set of PSR-0 directories for a given prefix, either |
|
| 103 | + * appending or prepending to the ones previously set for this prefix. |
|
| 104 | + * |
|
| 105 | + * @param string $prefix The prefix |
|
| 106 | + * @param array|string $paths The PSR-0 root directories |
|
| 107 | + * @param bool $prepend Whether to prepend the directories |
|
| 108 | + */ |
|
| 109 | + public function add($prefix, $paths, $prepend = false) |
|
| 110 | + { |
|
| 111 | + if (!$prefix) { |
|
| 112 | + if ($prepend) { |
|
| 113 | + $this->fallbackDirsPsr0 = array_merge( |
|
| 114 | + (array) $paths, |
|
| 115 | + $this->fallbackDirsPsr0 |
|
| 116 | + ); |
|
| 117 | + } else { |
|
| 118 | + $this->fallbackDirsPsr0 = array_merge( |
|
| 119 | + $this->fallbackDirsPsr0, |
|
| 120 | + (array) $paths |
|
| 121 | + ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $first = $prefix[0]; |
|
| 128 | + if (!isset($this->prefixesPsr0[$first][$prefix])) { |
|
| 129 | + $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
|
| 130 | + |
|
| 131 | + return; |
|
| 132 | + } |
|
| 133 | + if ($prepend) { |
|
| 134 | + $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
| 135 | + (array) $paths, |
|
| 136 | + $this->prefixesPsr0[$first][$prefix] |
|
| 137 | + ); |
|
| 138 | + } else { |
|
| 139 | + $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
| 140 | + $this->prefixesPsr0[$first][$prefix], |
|
| 141 | + (array) $paths |
|
| 142 | + ); |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Registers a set of PSR-4 directories for a given namespace, either |
|
| 148 | + * appending or prepending to the ones previously set for this namespace. |
|
| 149 | + * |
|
| 150 | + * @param string $prefix The prefix/namespace, with trailing '\\' |
|
| 151 | + * @param array|string $paths The PSR-4 base directories |
|
| 152 | + * @param bool $prepend Whether to prepend the directories |
|
| 153 | + * |
|
| 154 | + * @throws \InvalidArgumentException |
|
| 155 | + */ |
|
| 156 | + public function addPsr4($prefix, $paths, $prepend = false) |
|
| 157 | + { |
|
| 158 | + if (!$prefix) { |
|
| 159 | + // Register directories for the root namespace. |
|
| 160 | + if ($prepend) { |
|
| 161 | + $this->fallbackDirsPsr4 = array_merge( |
|
| 162 | + (array) $paths, |
|
| 163 | + $this->fallbackDirsPsr4 |
|
| 164 | + ); |
|
| 165 | + } else { |
|
| 166 | + $this->fallbackDirsPsr4 = array_merge( |
|
| 167 | + $this->fallbackDirsPsr4, |
|
| 168 | + (array) $paths |
|
| 169 | + ); |
|
| 170 | + } |
|
| 171 | + } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
|
| 172 | + // Register directories for a new namespace. |
|
| 173 | + $length = strlen($prefix); |
|
| 174 | + if ('\\' !== $prefix[$length - 1]) { |
|
| 175 | + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
| 176 | + } |
|
| 177 | + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
| 178 | + $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
| 179 | + } elseif ($prepend) { |
|
| 180 | + // Prepend directories for an already registered namespace. |
|
| 181 | + $this->prefixDirsPsr4[$prefix] = array_merge( |
|
| 182 | + (array) $paths, |
|
| 183 | + $this->prefixDirsPsr4[$prefix] |
|
| 184 | + ); |
|
| 185 | + } else { |
|
| 186 | + // Append directories for an already registered namespace. |
|
| 187 | + $this->prefixDirsPsr4[$prefix] = array_merge( |
|
| 188 | + $this->prefixDirsPsr4[$prefix], |
|
| 189 | + (array) $paths |
|
| 190 | + ); |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Registers a set of PSR-0 directories for a given prefix, |
|
| 196 | + * replacing any others previously set for this prefix. |
|
| 197 | + * |
|
| 198 | + * @param string $prefix The prefix |
|
| 199 | + * @param array|string $paths The PSR-0 base directories |
|
| 200 | + */ |
|
| 201 | + public function set($prefix, $paths) |
|
| 202 | + { |
|
| 203 | + if (!$prefix) { |
|
| 204 | + $this->fallbackDirsPsr0 = (array) $paths; |
|
| 205 | + } else { |
|
| 206 | + $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Registers a set of PSR-4 directories for a given namespace, |
|
| 212 | + * replacing any others previously set for this namespace. |
|
| 213 | + * |
|
| 214 | + * @param string $prefix The prefix/namespace, with trailing '\\' |
|
| 215 | + * @param array|string $paths The PSR-4 base directories |
|
| 216 | + * |
|
| 217 | + * @throws \InvalidArgumentException |
|
| 218 | + */ |
|
| 219 | + public function setPsr4($prefix, $paths) |
|
| 220 | + { |
|
| 221 | + if (!$prefix) { |
|
| 222 | + $this->fallbackDirsPsr4 = (array) $paths; |
|
| 223 | + } else { |
|
| 224 | + $length = strlen($prefix); |
|
| 225 | + if ('\\' !== $prefix[$length - 1]) { |
|
| 226 | + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
| 227 | + } |
|
| 228 | + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
| 229 | + $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Turns on searching the include path for class files. |
|
| 235 | + * |
|
| 236 | + * @param bool $useIncludePath |
|
| 237 | + */ |
|
| 238 | + public function setUseIncludePath($useIncludePath) |
|
| 239 | + { |
|
| 240 | + $this->useIncludePath = $useIncludePath; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Can be used to check if the autoloader uses the include path to check |
|
| 245 | + * for classes. |
|
| 246 | + * |
|
| 247 | + * @return bool |
|
| 248 | + */ |
|
| 249 | + public function getUseIncludePath() |
|
| 250 | + { |
|
| 251 | + return $this->useIncludePath; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Turns off searching the prefix and fallback directories for classes |
|
| 256 | + * that have not been registered with the class map. |
|
| 257 | + * |
|
| 258 | + * @param bool $classMapAuthoritative |
|
| 259 | + */ |
|
| 260 | + public function setClassMapAuthoritative($classMapAuthoritative) |
|
| 261 | + { |
|
| 262 | + $this->classMapAuthoritative = $classMapAuthoritative; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * Should class lookup fail if not found in the current class map? |
|
| 267 | + * |
|
| 268 | + * @return bool |
|
| 269 | + */ |
|
| 270 | + public function isClassMapAuthoritative() |
|
| 271 | + { |
|
| 272 | + return $this->classMapAuthoritative; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * APCu prefix to use to cache found/not-found classes, if the extension is enabled. |
|
| 277 | + * |
|
| 278 | + * @param string|null $apcuPrefix |
|
| 279 | + */ |
|
| 280 | + public function setApcuPrefix($apcuPrefix) |
|
| 281 | + { |
|
| 282 | + $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * The APCu prefix in use, or null if APCu caching is not enabled. |
|
| 287 | + * |
|
| 288 | + * @return string|null |
|
| 289 | + */ |
|
| 290 | + public function getApcuPrefix() |
|
| 291 | + { |
|
| 292 | + return $this->apcuPrefix; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * Registers this instance as an autoloader. |
|
| 297 | + * |
|
| 298 | + * @param bool $prepend Whether to prepend the autoloader or not |
|
| 299 | + */ |
|
| 300 | + public function register($prepend = false) |
|
| 301 | + { |
|
| 302 | + spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Unregisters this instance as an autoloader. |
|
| 307 | + */ |
|
| 308 | + public function unregister() |
|
| 309 | + { |
|
| 310 | + spl_autoload_unregister(array($this, 'loadClass')); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Loads the given class or interface. |
|
| 315 | + * |
|
| 316 | + * @param string $class The name of the class |
|
| 317 | + * @return bool|null True if loaded, null otherwise |
|
| 318 | + */ |
|
| 319 | + public function loadClass($class) |
|
| 320 | + { |
|
| 321 | + if ($file = $this->findFile($class)) { |
|
| 322 | + includeFile($file); |
|
| 323 | + |
|
| 324 | + return true; |
|
| 325 | + } |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Finds the path to the file where the class is defined. |
|
| 330 | + * |
|
| 331 | + * @param string $class The name of the class |
|
| 332 | + * |
|
| 333 | + * @return string|false The path if found, false otherwise |
|
| 334 | + */ |
|
| 335 | + public function findFile($class) |
|
| 336 | + { |
|
| 337 | + // class map lookup |
|
| 338 | + if (isset($this->classMap[$class])) { |
|
| 339 | + return $this->classMap[$class]; |
|
| 340 | + } |
|
| 341 | + if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { |
|
| 342 | + return false; |
|
| 343 | + } |
|
| 344 | + if (null !== $this->apcuPrefix) { |
|
| 345 | + $file = apcu_fetch($this->apcuPrefix.$class, $hit); |
|
| 346 | + if ($hit) { |
|
| 347 | + return $file; |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + $file = $this->findFileWithExtension($class, '.php'); |
|
| 352 | + |
|
| 353 | + // Search for Hack files if we are running on HHVM |
|
| 354 | + if (false === $file && defined('HHVM_VERSION')) { |
|
| 355 | + $file = $this->findFileWithExtension($class, '.hh'); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + if (null !== $this->apcuPrefix) { |
|
| 359 | + apcu_add($this->apcuPrefix.$class, $file); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + if (false === $file) { |
|
| 363 | + // Remember that this class does not exist. |
|
| 364 | + $this->missingClasses[$class] = true; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + return $file; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + private function findFileWithExtension($class, $ext) |
|
| 371 | + { |
|
| 372 | + // PSR-4 lookup |
|
| 373 | + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
|
| 374 | + |
|
| 375 | + $first = $class[0]; |
|
| 376 | + if (isset($this->prefixLengthsPsr4[$first])) { |
|
| 377 | + $subPath = $class; |
|
| 378 | + while (false !== $lastPos = strrpos($subPath, '\\')) { |
|
| 379 | + $subPath = substr($subPath, 0, $lastPos); |
|
| 380 | + $search = $subPath . '\\'; |
|
| 381 | + if (isset($this->prefixDirsPsr4[$search])) { |
|
| 382 | + $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); |
|
| 383 | + foreach ($this->prefixDirsPsr4[$search] as $dir) { |
|
| 384 | + if (file_exists($file = $dir . $pathEnd)) { |
|
| 385 | + return $file; |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | + } |
|
| 389 | + } |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + // PSR-4 fallback dirs |
|
| 393 | + foreach ($this->fallbackDirsPsr4 as $dir) { |
|
| 394 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
|
| 395 | + return $file; |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + // PSR-0 lookup |
|
| 400 | + if (false !== $pos = strrpos($class, '\\')) { |
|
| 401 | + // namespaced class name |
|
| 402 | + $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
|
| 403 | + . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
|
| 404 | + } else { |
|
| 405 | + // PEAR-like class name |
|
| 406 | + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + if (isset($this->prefixesPsr0[$first])) { |
|
| 410 | + foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
|
| 411 | + if (0 === strpos($class, $prefix)) { |
|
| 412 | + foreach ($dirs as $dir) { |
|
| 413 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
| 414 | + return $file; |
|
| 415 | + } |
|
| 416 | + } |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + // PSR-0 fallback dirs |
|
| 422 | + foreach ($this->fallbackDirsPsr0 as $dir) { |
|
| 423 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
| 424 | + return $file; |
|
| 425 | + } |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + // PSR-0 include paths. |
|
| 429 | + if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
|
| 430 | + return $file; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + return false; |
|
| 434 | + } |
|
| 435 | 435 | } |
| 436 | 436 | |
| 437 | 437 | /** |
@@ -441,5 +441,5 @@ discard block |
||
| 441 | 441 | */ |
| 442 | 442 | function includeFile($file) |
| 443 | 443 | { |
| 444 | - include $file; |
|
| 444 | + include $file; |
|
| 445 | 445 | } |
@@ -342,7 +342,7 @@ discard block |
||
| 342 | 342 | return false; |
| 343 | 343 | } |
| 344 | 344 | if (null !== $this->apcuPrefix) { |
| 345 | - $file = apcu_fetch($this->apcuPrefix.$class, $hit); |
|
| 345 | + $file = apcu_fetch($this->apcuPrefix . $class, $hit); |
|
| 346 | 346 | if ($hit) { |
| 347 | 347 | return $file; |
| 348 | 348 | } |
@@ -356,7 +356,7 @@ discard block |
||
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | if (null !== $this->apcuPrefix) { |
| 359 | - apcu_add($this->apcuPrefix.$class, $file); |
|
| 359 | + apcu_add($this->apcuPrefix . $class, $file); |
|
| 360 | 360 | } |
| 361 | 361 | |
| 362 | 362 | if (false === $file) { |