@@ -43,6 +43,9 @@ discard block |
||
| 43 | 43 | return $this->cache->write($this->file, $rules, $this->import); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | + /** |
|
| 47 | + * @param integer $indexStart |
|
| 48 | + */ |
|
| 46 | 49 | private function parseTokens($indexStart) { |
| 47 | 50 | $this->rules = []; |
| 48 | 51 | $line = 1; |
@@ -60,6 +63,9 @@ discard block |
||
| 60 | 63 | return $this->rules; |
| 61 | 64 | } |
| 62 | 65 | |
| 66 | + /** |
|
| 67 | + * @param integer $line |
|
| 68 | + */ |
|
| 63 | 69 | private function addRules($token, $indexStart, $line) { |
| 64 | 70 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
| 65 | 71 | $this->tss->skip(count($selector)); |
@@ -73,6 +79,9 @@ discard block |
||
| 73 | 79 | if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
| 74 | 80 | } |
| 75 | 81 | |
| 82 | + /** |
|
| 83 | + * @param integer $index |
|
| 84 | + */ |
|
| 76 | 85 | private function CssToRules($selector, $index, $properties, $line) { |
| 77 | 86 | $parts = $selector->trim()->splitOnToken(Tokenizer::ARG); |
| 78 | 87 | $rules = []; |
@@ -96,6 +105,9 @@ discard block |
||
| 96 | 105 | return $rules; |
| 97 | 106 | } |
| 98 | 107 | |
| 108 | + /** |
|
| 109 | + * @param integer $indexStart |
|
| 110 | + */ |
|
| 99 | 111 | private function processingInstructions($token, $indexStart) { |
| 100 | 112 | if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
| 101 | 113 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
@@ -123,6 +135,10 @@ discard block |
||
| 123 | 135 | return ($a->depth < $b->depth) ? -1 : 1; |
| 124 | 136 | } |
| 125 | 137 | |
| 138 | + /** |
|
| 139 | + * @param string $open |
|
| 140 | + * @param string $close |
|
| 141 | + */ |
|
| 126 | 142 | private function stripComments($str, $open, $close) { |
| 127 | 143 | $pos = 0; |
| 128 | 144 | while (($pos = strpos($str, $open, $pos)) !== false) { |
@@ -24,8 +24,11 @@ discard block |
||
| 24 | 24 | $this->file = $tss; |
| 25 | 25 | $this->rules = $this->cache->load($tss); |
| 26 | 26 | $baseDir = dirname(realpath($tss)) . DIRECTORY_SEPARATOR; |
| 27 | - if (empty($this->rules)) $tss = file_get_contents($tss); |
|
| 28 | - else return; |
|
| 27 | + if (empty($this->rules)) { |
|
| 28 | + $tss = file_get_contents($tss); |
|
| 29 | + } else { |
|
| 30 | + return; |
|
| 31 | + } |
|
| 29 | 32 | } |
| 30 | 33 | $this->tss = $this->stripComments($tss, '//', "\n"); |
| 31 | 34 | $this->tss = $this->stripComments($this->tss, '/*', '*/'); |
@@ -36,7 +39,9 @@ discard block |
||
| 36 | 39 | } |
| 37 | 40 | |
| 38 | 41 | public function parse($indexStart = 0) { |
| 39 | - if (!empty($this->rules)) return $this->rules['rules']; |
|
| 42 | + if (!empty($this->rules)) { |
|
| 43 | + return $this->rules['rules']; |
|
| 44 | + } |
|
| 40 | 45 | $rules = $this->parseTokens($indexStart); |
| 41 | 46 | usort($rules, [$this, 'sortRules']); |
| 42 | 47 | $this->checkError($rules); |
@@ -50,12 +55,12 @@ discard block |
||
| 50 | 55 | if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
| 51 | 56 | $this->rules = array_merge($this->rules, $processing); |
| 52 | 57 | continue; |
| 53 | - } |
|
| 54 | - else if ($token['type'] === Tokenizer::NEW_LINE) { |
|
| 58 | + } else if ($token['type'] === Tokenizer::NEW_LINE) { |
|
| 55 | 59 | $line++; |
| 56 | 60 | continue; |
| 61 | + } else { |
|
| 62 | + $this->addRules($token, $indexStart, $line); |
|
| 57 | 63 | } |
| 58 | - else $this->addRules($token, $indexStart, $line); |
|
| 59 | 64 | } |
| 60 | 65 | return $this->rules; |
| 61 | 66 | } |
@@ -63,14 +68,18 @@ discard block |
||
| 63 | 68 | private function addRules($token, $indexStart, $line) { |
| 64 | 69 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
| 65 | 70 | $this->tss->skip(count($selector)); |
| 66 | - if (count($selector) === 0) return; |
|
| 71 | + if (count($selector) === 0) { |
|
| 72 | + return; |
|
| 73 | + } |
|
| 67 | 74 | |
| 68 | 75 | $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $line); |
| 69 | 76 | $this->rules = $this->writeRule($this->rules, $newRules); |
| 70 | 77 | } |
| 71 | 78 | |
| 72 | 79 | private function checkError($rules) { |
| 73 | - if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
|
| 80 | + if (empty($rules) && count($this->tss) > 0) { |
|
| 81 | + throw new \Exception('No TSS rules parsed'); |
|
| 82 | + } |
|
| 74 | 83 | } |
| 75 | 84 | |
| 76 | 85 | private function CssToRules($selector, $index, $properties, $line) { |
@@ -97,7 +106,9 @@ discard block |
||
| 97 | 106 | } |
| 98 | 107 | |
| 99 | 108 | private function processingInstructions($token, $indexStart) { |
| 100 | - if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
|
| 109 | + if ($token['type'] !== Tokenizer::AT_SIGN) { |
|
| 110 | + return false; |
|
| 111 | + } |
|
| 101 | 112 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
| 102 | 113 | $funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
| 103 | 114 | $args = $this->valueParser->parseTokens($tokens->from(Tokenizer::NAME)); |
@@ -109,8 +120,11 @@ discard block |
||
| 109 | 120 | } |
| 110 | 121 | |
| 111 | 122 | private function import($args, $indexStart) { |
| 112 | - if ($this->file !== null) $fileName = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR . $args[0]; |
|
| 113 | - else $fileName = $args[0]; |
|
| 123 | + if ($this->file !== null) { |
|
| 124 | + $fileName = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR . $args[0]; |
|
| 125 | + } else { |
|
| 126 | + $fileName = $args[0]; |
|
| 127 | + } |
|
| 114 | 128 | $this->import[] = $fileName; |
| 115 | 129 | $sheet = new Sheet($fileName, $this->baseDir, $this->xPath, $this->valueParser, $this->cache); |
| 116 | 130 | return $sheet->parse($indexStart); |
@@ -118,7 +132,9 @@ discard block |
||
| 118 | 132 | |
| 119 | 133 | private function sortRules($a, $b) { |
| 120 | 134 | //If they have the same depth, compare on index |
| 121 | - if ($a->depth === $b->depth) return $a->index < $b->index ? -1 : 1; |
|
| 135 | + if ($a->depth === $b->depth) { |
|
| 136 | + return $a->index < $b->index ? -1 : 1; |
|
| 137 | + } |
|
| 122 | 138 | |
| 123 | 139 | return ($a->depth < $b->depth) ? -1 : 1; |
| 124 | 140 | } |
@@ -127,7 +143,9 @@ discard block |
||
| 127 | 143 | $pos = 0; |
| 128 | 144 | while (($pos = strpos($str, $open, $pos)) !== false) { |
| 129 | 145 | $end = strpos($str, $close, $pos); |
| 130 | - if ($end === false) break; |
|
| 146 | + if ($end === false) { |
|
| 147 | + break; |
|
| 148 | + } |
|
| 131 | 149 | $str = substr_replace($str, '', $pos, $end-$pos+strlen($close)); |
| 132 | 150 | } |
| 133 | 151 | |
@@ -6,28 +6,28 @@ discard block |
||
| 6 | 6 | * @version 1.0 */ |
| 7 | 7 | namespace Transphporm\Parser; |
| 8 | 8 | class Tokens implements \Iterator, \Countable { |
| 9 | - private $tokens; |
|
| 10 | - private $iterator = 0; |
|
| 9 | + private $tokens; |
|
| 10 | + private $iterator = 0; |
|
| 11 | 11 | |
| 12 | - public function __construct(array $tokens) { |
|
| 13 | - $this->tokens = $tokens; |
|
| 14 | - } |
|
| 12 | + public function __construct(array $tokens) { |
|
| 13 | + $this->tokens = $tokens; |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | - public function count() { |
|
| 17 | - return count($this->tokens); |
|
| 18 | - } |
|
| 16 | + public function count() { |
|
| 17 | + return count($this->tokens); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - // Iterator Functions |
|
| 21 | - public function current() { |
|
| 22 | - return $this->tokens[$this->iterator]; |
|
| 23 | - } |
|
| 20 | + // Iterator Functions |
|
| 21 | + public function current() { |
|
| 22 | + return $this->tokens[$this->iterator]; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - public function key() { |
|
| 26 | - return $this->iterator; |
|
| 27 | - } |
|
| 25 | + public function key() { |
|
| 26 | + return $this->iterator; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - public function next() { |
|
| 30 | - ++$this->iterator; |
|
| 29 | + public function next() { |
|
| 30 | + ++$this->iterator; |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | public function valid() { |
@@ -38,67 +38,67 @@ discard block |
||
| 38 | 38 | $this->iterator = 0; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - private function getKeysOfTokenType($tokenType) { |
|
| 42 | - return array_keys(array_column($this->tokens, 'type'), $tokenType); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - private function getKeyToSlice($tokenType) { |
|
| 46 | - $keys = $this->getKeysOfTokenType($tokenType); |
|
| 47 | - if (empty($keys)) return false; |
|
| 48 | - $key = $keys[0]; |
|
| 49 | - for ($i = 0; $key < $this->iterator; $i++) $key = $keys[$i]; |
|
| 50 | - return $key; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - public function from($tokenType, $inclusive = false) { |
|
| 54 | - $key = $this->getKeyToSlice($tokenType); |
|
| 55 | - if ($key === false) return new Tokens([]); |
|
| 56 | - if (!$inclusive) $key++; |
|
| 57 | - return new Tokens(array_slice($this->tokens, $key)); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - public function to($tokenType, $inclusive = false) { |
|
| 61 | - $key = $this->getKeyToSlice($tokenType); |
|
| 62 | - if ($key === false) return new Tokens([]); |
|
| 63 | - if ($inclusive) $key++; |
|
| 64 | - return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - public function skip($count) { |
|
| 68 | - $this->iterator += $count; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - public function splitOnToken($tokenType) { |
|
| 72 | - $splitTokens = []; |
|
| 41 | + private function getKeysOfTokenType($tokenType) { |
|
| 42 | + return array_keys(array_column($this->tokens, 'type'), $tokenType); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + private function getKeyToSlice($tokenType) { |
|
| 46 | + $keys = $this->getKeysOfTokenType($tokenType); |
|
| 47 | + if (empty($keys)) return false; |
|
| 48 | + $key = $keys[0]; |
|
| 49 | + for ($i = 0; $key < $this->iterator; $i++) $key = $keys[$i]; |
|
| 50 | + return $key; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + public function from($tokenType, $inclusive = false) { |
|
| 54 | + $key = $this->getKeyToSlice($tokenType); |
|
| 55 | + if ($key === false) return new Tokens([]); |
|
| 56 | + if (!$inclusive) $key++; |
|
| 57 | + return new Tokens(array_slice($this->tokens, $key)); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + public function to($tokenType, $inclusive = false) { |
|
| 61 | + $key = $this->getKeyToSlice($tokenType); |
|
| 62 | + if ($key === false) return new Tokens([]); |
|
| 63 | + if ($inclusive) $key++; |
|
| 64 | + return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + public function skip($count) { |
|
| 68 | + $this->iterator += $count; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + public function splitOnToken($tokenType) { |
|
| 72 | + $splitTokens = []; |
|
| 73 | 73 | $i = 0; |
| 74 | 74 | foreach ($this->tokens as $token) { |
| 75 | 75 | if ($token['type'] === $tokenType) $i++; |
| 76 | 76 | else $splitTokens[$i][] = $token; |
| 77 | 77 | } |
| 78 | - return array_map(function ($tokens) { |
|
| 79 | - return new Tokens($tokens); |
|
| 80 | - }, $splitTokens); |
|
| 78 | + return array_map(function ($tokens) { |
|
| 79 | + return new Tokens($tokens); |
|
| 80 | + }, $splitTokens); |
|
| 81 | 81 | //return $splitTokens; |
| 82 | - } |
|
| 83 | - |
|
| 84 | - public function trim() { |
|
| 85 | - $tokens = $this->tokens; |
|
| 86 | - // Remove end whitespace |
|
| 87 | - while (end($tokens)['type'] === Tokenizer::WHITESPACE) { |
|
| 88 | - array_pop($tokens); |
|
| 89 | - } |
|
| 90 | - // Remove begining whitespace |
|
| 91 | - while (isset($tokens[0]) && $tokens[0]['type'] === Tokenizer::WHITESPACE) { |
|
| 92 | - array_shift($tokens); |
|
| 93 | - } |
|
| 94 | - return new Tokens($tokens); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - public function read($offset = 0) { |
|
| 98 | - return isset($this->tokens[$offset]) ? $this->tokens[$offset]['value'] : false; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - public function type($offset = 0) { |
|
| 102 | - return isset($this->tokens[$offset]) ? $this->tokens[$offset]['type'] : false; |
|
| 103 | - } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public function trim() { |
|
| 85 | + $tokens = $this->tokens; |
|
| 86 | + // Remove end whitespace |
|
| 87 | + while (end($tokens)['type'] === Tokenizer::WHITESPACE) { |
|
| 88 | + array_pop($tokens); |
|
| 89 | + } |
|
| 90 | + // Remove begining whitespace |
|
| 91 | + while (isset($tokens[0]) && $tokens[0]['type'] === Tokenizer::WHITESPACE) { |
|
| 92 | + array_shift($tokens); |
|
| 93 | + } |
|
| 94 | + return new Tokens($tokens); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + public function read($offset = 0) { |
|
| 98 | + return isset($this->tokens[$offset]) ? $this->tokens[$offset]['value'] : false; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + public function type($offset = 0) { |
|
| 102 | + return isset($this->tokens[$offset]) ? $this->tokens[$offset]['type'] : false; |
|
| 103 | + } |
|
| 104 | 104 | } |