1
|
|
|
<?php |
2
|
|
|
/* @description Transformation Style Sheets - Revolutionising PHP templating * |
3
|
|
|
* @author Tom Butler [email protected] * |
4
|
|
|
* @copyright 2017 Tom Butler <[email protected]> | https://r.je/ * |
5
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License * |
6
|
|
|
* @version 1.2 */ |
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 $xPath; |
12
|
|
|
private $valueParser; |
13
|
|
|
private $filePath; |
14
|
|
|
private $sheetLoader; |
15
|
|
|
private $file; |
16
|
|
|
private $rules; |
17
|
|
|
|
18
|
|
View Code Duplication |
public function __construct($tss, CssToXpath $xPath, Value $valueParser, \Transphporm\FilePath $filePath, \Transphporm\SheetLoader\SheetLoader $sheetLoader, $file = null) { |
|
|
|
|
19
|
|
|
$this->xPath = $xPath; |
20
|
|
|
$this->valueParser = $valueParser; |
21
|
|
|
$this->filePath = $filePath; |
22
|
|
|
$this->sheetLoader = $sheetLoader; |
23
|
|
|
$this->file = $file; |
24
|
|
|
$this->tss = (new Tokenizer($tss))->getTokens(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function parse($indexStart = 0) { |
28
|
|
|
if (!empty($this->rules)) return $this->rules['rules']; |
29
|
|
|
$rules = $this->parseTokens($indexStart); |
30
|
|
|
$this->checkError($rules); |
31
|
|
|
return $rules; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function parseTokens($indexStart) { |
35
|
|
|
$this->rules = []; |
36
|
|
|
foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
37
|
|
|
if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
38
|
|
|
$this->rules = array_merge($this->rules, $processing); |
39
|
|
|
} |
40
|
|
|
else if (!in_array($token['type'], [Tokenizer::NEW_LINE, Tokenizer::AT_SIGN])) $this->addRules($token, $indexStart++); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this->rules; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function addRules($token, $indexStart) { |
47
|
|
|
$selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
48
|
|
|
|
49
|
|
|
$this->tss->skip(count($selector)); |
50
|
|
|
if (count($selector) === 0) return; |
51
|
|
|
$newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $token['line']); |
52
|
|
|
$this->rules = $this->writeRule($this->rules, $newRules); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function checkError($rules) { |
56
|
|
|
if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function CssToRules($selector, $index, $properties, $line) { |
60
|
|
|
$parts = $selector->trim()->splitOnToken(Tokenizer::ARG); |
61
|
|
|
$rules = []; |
62
|
|
|
foreach ($parts as $part) { |
63
|
|
|
$serialized = serialize($part->removeLine()); |
64
|
|
|
$rules[$serialized] = new \Transphporm\Rule($this->xPath->getXpath($part), $this->xPath->getPseudo($part), $this->xPath->getDepth($part), $index, $this->file, $line); |
65
|
|
|
$rules[$serialized]->properties = $properties; |
66
|
|
|
} |
67
|
|
|
return $rules; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function writeRule($rules, $newRules) { |
71
|
|
|
foreach ($newRules as $selector => $newRule) { |
72
|
|
|
if (isset($rules[$selector])) { |
73
|
|
|
$newRule->properties = array_merge($rules[$selector]->properties, $newRule->properties); |
74
|
|
|
$newRule->index = $rules[$selector]->index; |
75
|
|
|
} |
76
|
|
|
$rules[$selector] = $newRule; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $rules; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function processingInstructions($token, $indexStart) { |
83
|
|
|
if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
84
|
|
|
$tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
85
|
|
|
$funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
86
|
|
|
$funcToks = $tokens->from(Tokenizer::NAME); |
87
|
|
|
$args = $this->valueParser->parseTokens($funcToks); |
88
|
|
|
$rules = $this->$funcName($args, $indexStart, $funcToks); |
89
|
|
|
$this->tss->skip(count($tokens)+2); |
90
|
|
|
|
91
|
|
|
return $rules; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function import($args, $indexStart, $tokens) { |
|
|
|
|
95
|
|
|
$fileName = $this->filePath->getFilePath($args[0]); |
96
|
|
|
$this->sheetLoader->addImport($fileName); |
97
|
|
|
|
98
|
|
|
$tssFile = new \Transphporm\SheetLoader\TSSString(file_get_contents($fileName), $this->filePath); |
99
|
|
|
return $tssFile->getRules($this->xPath, $this->valueParser, $this->sheetLoader, $indexStart); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function cacheKey($args, $indexStart, $tokens) { |
|
|
|
|
103
|
|
|
$this->sheetLoader->setCacheKey($tokens); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function getProperties($tokens) { |
107
|
|
|
$rules = $tokens->splitOnToken(Tokenizer::SEMI_COLON); |
108
|
|
|
|
109
|
|
|
$return = []; |
110
|
|
|
foreach ($rules as $rule) { |
111
|
|
|
$name = $rule->from(Tokenizer::NAME, true)->to(Tokenizer::COLON)->read(); |
112
|
|
|
$tokens = $rule->from(Tokenizer::COLON)->trim(); |
113
|
|
|
if (count($tokens) > 0) $return[$name] = $rule->from(Tokenizer::COLON)->trim(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $return; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.