@@ -16,6 +16,9 @@ discard block |
||
| 16 | 16 | private $filePath; |
| 17 | 17 | private $import = []; |
| 18 | 18 | |
| 19 | + /** |
|
| 20 | + * @param string $tss |
|
| 21 | + */ |
|
| 19 | 22 | public function __construct($tss, CssToXpath $xPath, Value $valueParser, \Transphporm\TSSCache $cache, \Transphporm\FilePath $filePath) { |
| 20 | 23 | $this->cache = $cache; |
| 21 | 24 | $this->xPath = $xPath; |
@@ -42,6 +45,9 @@ discard block |
||
| 42 | 45 | return $this->cache->write($this->file, $rules, $this->import); |
| 43 | 46 | } |
| 44 | 47 | |
| 48 | + /** |
|
| 49 | + * @param integer $indexStart |
|
| 50 | + */ |
|
| 45 | 51 | private function parseTokens($indexStart) { |
| 46 | 52 | $this->rules = []; |
| 47 | 53 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
@@ -66,6 +72,9 @@ discard block |
||
| 66 | 72 | if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
| 67 | 73 | } |
| 68 | 74 | |
| 75 | + /** |
|
| 76 | + * @param integer $index |
|
| 77 | + */ |
|
| 69 | 78 | private function CssToRules($selector, $index, $properties, $line) { |
| 70 | 79 | $parts = $selector->trim()->splitOnToken(Tokenizer::ARG); |
| 71 | 80 | $rules = []; |
@@ -89,6 +98,9 @@ discard block |
||
| 89 | 98 | return $rules; |
| 90 | 99 | } |
| 91 | 100 | |
| 101 | + /** |
|
| 102 | + * @param integer $indexStart |
|
| 103 | + */ |
|
| 92 | 104 | private function processingInstructions($token, $indexStart) { |
| 93 | 105 | if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
| 94 | 106 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
@@ -117,6 +129,10 @@ discard block |
||
| 117 | 129 | return ($a->depth < $b->depth) ? -1 : 1; |
| 118 | 130 | } |
| 119 | 131 | |
| 132 | + /** |
|
| 133 | + * @param string $open |
|
| 134 | + * @param string $close |
|
| 135 | + */ |
|
| 120 | 136 | private function stripComments($str, $open, $close) { |
| 121 | 137 | $pos = 0; |
| 122 | 138 | while (($pos = strpos($str, $open, $pos)) !== false) { |
@@ -25,8 +25,11 @@ discard block |
||
| 25 | 25 | $this->file = $tss; |
| 26 | 26 | $this->rules = $this->cache->load($tss); |
| 27 | 27 | $this->filePath->setBaseDir(dirname(realpath($tss)) . DIRECTORY_SEPARATOR); |
| 28 | - if (empty($this->rules)) $tss = file_get_contents($tss); |
|
| 29 | - else return; |
|
| 28 | + if (empty($this->rules)) { |
|
| 29 | + $tss = file_get_contents($tss); |
|
| 30 | + } else { |
|
| 31 | + return; |
|
| 32 | + } |
|
| 30 | 33 | } |
| 31 | 34 | $this->tss = $this->stripComments($tss, '//', "\n"); |
| 32 | 35 | $this->tss = $this->stripComments($this->tss, '/*', '*/'); |
@@ -35,7 +38,9 @@ discard block |
||
| 35 | 38 | } |
| 36 | 39 | |
| 37 | 40 | public function parse($indexStart = 0) { |
| 38 | - if (!empty($this->rules)) return $this->rules['rules']; |
|
| 41 | + if (!empty($this->rules)) { |
|
| 42 | + return $this->rules['rules']; |
|
| 43 | + } |
|
| 39 | 44 | $rules = $this->parseTokens($indexStart); |
| 40 | 45 | usort($rules, [$this, 'sortRules']); |
| 41 | 46 | $this->checkError($rules); |
@@ -47,8 +52,9 @@ discard block |
||
| 47 | 52 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
| 48 | 53 | if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
| 49 | 54 | $this->rules = array_merge($this->rules, $processing); |
| 55 | + } else if ($token['type'] !== Tokenizer::NEW_LINE) { |
|
| 56 | + $this->addRules($token, $indexStart); |
|
| 50 | 57 | } |
| 51 | - else if ($token['type'] !== Tokenizer::NEW_LINE) $this->addRules($token, $indexStart); |
|
| 52 | 58 | } |
| 53 | 59 | return $this->rules; |
| 54 | 60 | } |
@@ -56,14 +62,18 @@ discard block |
||
| 56 | 62 | private function addRules($token, $indexStart) { |
| 57 | 63 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
| 58 | 64 | $this->tss->skip(count($selector)); |
| 59 | - if (count($selector) === 0) return; |
|
| 65 | + if (count($selector) === 0) { |
|
| 66 | + return; |
|
| 67 | + } |
|
| 60 | 68 | |
| 61 | 69 | $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $token['line']); |
| 62 | 70 | $this->rules = $this->writeRule($this->rules, $newRules); |
| 63 | 71 | } |
| 64 | 72 | |
| 65 | 73 | private function checkError($rules) { |
| 66 | - if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
|
| 74 | + if (empty($rules) && count($this->tss) > 0) { |
|
| 75 | + throw new \Exception('No TSS rules parsed'); |
|
| 76 | + } |
|
| 67 | 77 | } |
| 68 | 78 | |
| 69 | 79 | private function CssToRules($selector, $index, $properties, $line) { |
@@ -90,7 +100,9 @@ discard block |
||
| 90 | 100 | } |
| 91 | 101 | |
| 92 | 102 | private function processingInstructions($token, $indexStart) { |
| 93 | - if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
|
| 103 | + if ($token['type'] !== Tokenizer::AT_SIGN) { |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 94 | 106 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
| 95 | 107 | $funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
| 96 | 108 | $args = $this->valueParser->parseTokens($tokens->from(Tokenizer::NAME)); |
@@ -112,7 +124,9 @@ discard block |
||
| 112 | 124 | |
| 113 | 125 | private function sortRules($a, $b) { |
| 114 | 126 | //If they have the same depth, compare on index |
| 115 | - if ($a->depth === $b->depth) return $a->index < $b->index ? -1 : 1; |
|
| 127 | + if ($a->depth === $b->depth) { |
|
| 128 | + return $a->index < $b->index ? -1 : 1; |
|
| 129 | + } |
|
| 116 | 130 | |
| 117 | 131 | return ($a->depth < $b->depth) ? -1 : 1; |
| 118 | 132 | } |
@@ -121,7 +135,9 @@ discard block |
||
| 121 | 135 | $pos = 0; |
| 122 | 136 | while (($pos = strpos($str, $open, $pos)) !== false) { |
| 123 | 137 | $end = strpos($str, $close, $pos); |
| 124 | - if ($end === false) break; |
|
| 138 | + if ($end === false) { |
|
| 139 | + break; |
|
| 140 | + } |
|
| 125 | 141 | $str = substr_replace($str, '', $pos, $end-$pos+strlen($close)); |
| 126 | 142 | } |
| 127 | 143 | |
@@ -2,20 +2,20 @@ |
||
| 2 | 2 | namespace Transphporm; |
| 3 | 3 | |
| 4 | 4 | class FilePath { |
| 5 | - private $baseDir; |
|
| 6 | - private $customBase; |
|
| 5 | + private $baseDir; |
|
| 6 | + private $customBase; |
|
| 7 | 7 | |
| 8 | - public function __construct($customBase = null) { |
|
| 9 | - $this->baseDir = ''; |
|
| 10 | - if ($customBase === null) $this->customBase = getcwd(); |
|
| 11 | - else $this->customBase = rtrim($customBase, '/'); |
|
| 12 | - } |
|
| 8 | + public function __construct($customBase = null) { |
|
| 9 | + $this->baseDir = ''; |
|
| 10 | + if ($customBase === null) $this->customBase = getcwd(); |
|
| 11 | + else $this->customBase = rtrim($customBase, '/'); |
|
| 12 | + } |
|
| 13 | 13 | |
| 14 | - public function setBaseDir($baseDir) { |
|
| 15 | - $this->baseDir = $baseDir; |
|
| 16 | - } |
|
| 14 | + public function setBaseDir($baseDir) { |
|
| 15 | + $this->baseDir = $baseDir; |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - public function getFilePath($filePath = '') { |
|
| 18 | + public function getFilePath($filePath = '') { |
|
| 19 | 19 | if (isset($filePath[0]) && $filePath[0] == '/') return $this->customBase . $filePath; |
| 20 | 20 | else return $this->baseDir . $filePath; |
| 21 | 21 | } |
@@ -16,7 +16,7 @@ |
||
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | public function getFilePath($filePath = '') { |
| 19 | - if (isset($filePath[0]) && $filePath[0] == '/') return $this->customBase . $filePath; |
|
| 20 | - else return $this->baseDir . $filePath; |
|
| 19 | + if (isset($filePath[0]) && $filePath[0] == '/') return $this->customBase.$filePath; |
|
| 20 | + else return $this->baseDir.$filePath; |
|
| 21 | 21 | } |
| 22 | 22 | } |
@@ -7,8 +7,11 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | public function __construct($customBase = null) { |
| 9 | 9 | $this->baseDir = ''; |
| 10 | - if ($customBase === null) $this->customBase = getcwd(); |
|
| 11 | - else $this->customBase = rtrim($customBase, '/'); |
|
| 10 | + if ($customBase === null) { |
|
| 11 | + $this->customBase = getcwd(); |
|
| 12 | + } else { |
|
| 13 | + $this->customBase = rtrim($customBase, '/'); |
|
| 14 | + } |
|
| 12 | 15 | } |
| 13 | 16 | |
| 14 | 17 | public function setBaseDir($baseDir) { |
@@ -16,7 +19,10 @@ discard block |
||
| 16 | 19 | } |
| 17 | 20 | |
| 18 | 21 | public function getFilePath($filePath = '') { |
| 19 | - if (isset($filePath[0]) && $filePath[0] == '/') return $this->customBase . $filePath; |
|
| 20 | - else return $this->baseDir . $filePath; |
|
| 22 | + if (isset($filePath[0]) && $filePath[0] == '/') { |
|
| 23 | + return $this->customBase . $filePath; |
|
| 24 | + } else { |
|
| 25 | + return $this->baseDir . $filePath; |
|
| 26 | + } |
|
| 21 | 27 | } |
| 22 | 28 | } |