1
|
|
|
<?php |
2
|
|
|
/* @description Transformation Style Sheets - Revolutionising PHP templating * |
3
|
|
|
* @author Tom Butler [email protected] * |
4
|
|
|
* @copyright 2015 Tom Butler <[email protected]> | https://r.je/ * |
5
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License * |
6
|
|
|
* @version 1.0 */ |
7
|
|
|
namespace Transphporm\Parser; |
8
|
|
|
/** Parses a .tss file into individual rules, each rule has a query e,g, `ul li` and a set of rules e.g. `display: none; bind: iteration(id);` */ |
9
|
|
|
class Sheet { |
10
|
|
|
private $tss; |
11
|
|
|
private $baseDir; |
12
|
|
|
private $valueParser; |
13
|
|
|
private $xPath; |
14
|
|
|
|
15
|
|
|
public function __construct($tss, $baseDir, CssToXpath $xPath, Value $valueParser) { |
16
|
|
|
$this->tss = $this->stripComments($tss, '//', "\n"); |
17
|
|
|
$this->tss = $this->stripComments($this->tss, '/*', '*/'); |
18
|
|
|
$tokenizer = new Tokenizer($this->tss); |
19
|
|
|
$this->tss = $tokenizer->getTokens(); |
20
|
|
|
$this->baseDir = $baseDir; |
21
|
|
|
$this->xPath = $xPath; |
22
|
|
|
$this->valueParser = $valueParser; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function parse($indexStart = 0) { |
26
|
|
|
$rules = []; |
27
|
|
|
$numOfTokens = count($this->tss); |
28
|
|
|
for ($i = 0; isset($this->tss[$i]) && $i <= $numOfTokens; $i++) { |
29
|
|
|
if ($this->tss[$i]['type'] === Tokenizer::WHITESPACE) continue; |
30
|
|
|
if ($processing = $this->processingInstructions($i, count($rules)+$indexStart)) { |
31
|
|
|
$i = $processing['endPos']+1; |
32
|
|
|
$rules = array_merge($rules, $processing['rules']); |
33
|
|
|
continue; |
34
|
|
|
} |
35
|
|
|
$tokens = array_slice($this->tss, $i); |
36
|
|
|
$selector = $this->splitOnToken($tokens, Tokenizer::OPEN_BRACE)[0]; |
37
|
|
|
$i += count($selector); |
38
|
|
|
if ($selector[count($selector)-1]['type'] === Tokenizer::WHITESPACE) array_pop($selector); |
39
|
|
|
if (!isset($this->tss[$i])) break; |
40
|
|
|
|
41
|
|
|
$newRules = $this->cssToRules($selector, count($rules)+$indexStart, $this->getProperties($this->tss[$i]['value'])); |
42
|
|
|
$rules = $this->writeRule($rules, $newRules); |
43
|
|
|
} |
44
|
|
|
usort($rules, [$this, 'sortRules']); |
45
|
|
|
if (empty($rules) && !empty($this->tss)) throw new \Exception("No TSS rules parsed"); |
46
|
|
|
return $rules; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function CssToRules($selector, $index, $properties) { |
50
|
|
|
$parts = $this->splitOnToken($selector, Tokenizer::ARG); |
51
|
|
|
$rules = []; |
52
|
|
|
foreach ($parts as $part) { |
53
|
|
|
$tokenCss = $part; |
54
|
|
|
$rules[json_encode($part)] = new \Transphporm\Rule($this->xPath->getXpath($tokenCss), $this->xPath->getPseudo($tokenCss), $this->xPath->getDepth($tokenCss), $this->baseDir, $index++); |
55
|
|
|
$rules[json_encode($part)]->properties = $properties; |
56
|
|
|
} |
57
|
|
|
return $rules; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function writeRule($rules, $newRules) { |
61
|
|
|
foreach ($newRules as $selector => $newRule) { |
62
|
|
|
if (isset($rules[$selector])) { |
63
|
|
|
$newRule->properties = array_merge($rules[$selector]->properties, $newRule->properties); |
64
|
|
|
} |
65
|
|
|
$rules[$selector] = $newRule; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $rules; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function processingInstructions($key, $indexStart) { |
72
|
|
|
if (isset($this->tss[$key]) && $this->tss[$key]['type'] !== Tokenizer::AT_SIGN) return false; |
73
|
|
|
$rules = []; |
74
|
|
|
$tokens = array_slice($this->tss, $key+1); |
75
|
|
|
$tokens = $this->splitOnToken($tokens, Tokenizer::SEMI_COLON)[0]; |
76
|
|
|
$pos = $key+count($tokens)+1; |
77
|
|
|
$funcName = array_shift($tokens)['value']; |
78
|
|
|
$args = $this->valueParser->parseTokens($tokens); |
79
|
|
|
$rules = array_merge($rules, $this->$funcName($args, $indexStart)); |
80
|
|
|
return ['endPos' => $pos, 'rules' => $rules]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function import($args, $indexStart) { |
84
|
|
|
$fileName = $args[0]; |
85
|
|
|
$sheet = new Sheet(file_get_contents($this->baseDir . $fileName), dirname(realpath($this->baseDir . $fileName)) . DIRECTORY_SEPARATOR, $this->xPath, $this->valueParser); |
86
|
|
|
return $sheet->parse($indexStart); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function sortRules($a, $b) { |
90
|
|
|
//If they have the same depth, compare on index |
91
|
|
|
if ($a->depth === $b->depth) return $a->index < $b->index ? -1 : 1; |
92
|
|
|
|
93
|
|
|
return ($a->depth < $b->depth) ? -1 : 1; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function stripComments($str, $open, $close) { |
97
|
|
|
$pos = 0; |
98
|
|
|
while (($pos = strpos($str, $open, $pos)) !== false) { |
99
|
|
|
$end = strpos($str, $close, $pos); |
100
|
|
|
if ($end === false) break; |
101
|
|
|
$str = substr_replace($str, '', $pos, $end-$pos+strlen($close)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $str; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
private function splitOnToken($tokens, $splitOn) { |
|
|
|
|
108
|
|
|
$splitTokens = []; |
109
|
|
|
$i = 0; |
110
|
|
|
foreach ($tokens as $token) { |
111
|
|
|
if ($token['type'] === $splitOn) $i++; |
112
|
|
|
else $splitTokens[$i][] = $token; |
113
|
|
|
} |
114
|
|
|
return $splitTokens; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function removeWhitespace($tokens) { |
118
|
|
|
$newTokens = []; |
119
|
|
|
foreach ($tokens as $token) { |
120
|
|
|
if ($token['type'] !== Tokenizer::WHITESPACE) $newTokens[] = $token; |
121
|
|
|
} |
122
|
|
|
return $newTokens; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function getProperties($tokens) { |
126
|
|
|
$rules = $this->splitOnToken($tokens, Tokenizer::SEMI_COLON); |
127
|
|
|
$return = []; |
128
|
|
|
foreach ($rules as $rule) { |
129
|
|
|
$rule = $this->removeWhitespace($rule); |
130
|
|
|
if (isset($rule[1]) && $rule[1]['type'] === Tokenizer::COLON) $return[$rule[0]['value']] = array_slice($rule, 2); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $return; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.