1 | <?php |
||
9 | class Sheet { |
||
10 | private $cache; |
||
11 | private $tss; |
||
12 | private $rules; |
||
13 | private $file; |
||
14 | private $valueParser; |
||
15 | private $xPath; |
||
16 | private $filePath; |
||
17 | private $import = []; |
||
18 | |||
19 | public function __construct($tss, CssToXpath $xPath, Value $valueParser, \Transphporm\TSSCache $cache, \Transphporm\FilePath $filePath) { |
||
20 | $this->cache = $cache; |
||
21 | $this->xPath = $xPath; |
||
22 | $this->valueParser = $valueParser; |
||
23 | $this->filePath = $filePath; |
||
24 | if (is_file($tss)) { |
||
25 | $this->file = $tss; |
||
26 | $this->rules = $this->cache->load($tss); |
||
27 | $this->filePath->addPath(dirname(realpath($tss))); |
||
28 | if (empty($this->rules)) $tss = file_get_contents($tss); |
||
29 | else return; |
||
30 | } |
||
31 | $this->tss = (new Tokenizer($tss))->getTokens(); |
||
32 | } |
||
33 | |||
34 | public function parse($indexStart = 0) { |
||
35 | if (!empty($this->rules)) return $this->rules['rules']; |
||
36 | $rules = $this->parseTokens($indexStart); |
||
37 | usort($rules, [$this, 'sortRules']); |
||
38 | $this->checkError($rules); |
||
39 | //var_dump($rules); |
||
40 | return $this->cache->write($this->file, $rules, $this->import); |
||
41 | } |
||
42 | |||
43 | private function parseTokens($indexStart) { |
||
44 | $this->rules = []; |
||
45 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
||
46 | if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
||
47 | $this->rules = array_merge($this->rules, $processing); |
||
48 | } |
||
49 | else if ($token['type'] !== Tokenizer::NEW_LINE) $this->addRules($token, $indexStart++); |
||
50 | } |
||
51 | |||
52 | return $this->rules; |
||
53 | } |
||
54 | |||
55 | private function addRules($token, $indexStart) { |
||
56 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
||
57 | |||
58 | $this->tss->skip(count($selector)); |
||
59 | if (count($selector) === 0) return; |
||
60 | |||
61 | $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $token['line']); |
||
62 | $this->rules = $this->writeRule($this->rules, $newRules); |
||
63 | } |
||
64 | |||
65 | private function checkError($rules) { |
||
68 | |||
69 | private function CssToRules($selector, $index, $properties, $line) { |
||
79 | |||
80 | private function writeRule($rules, $newRules) { |
||
81 | foreach ($newRules as $selector => $newRule) { |
||
82 | |||
83 | if (isset($rules[$selector])) { |
||
92 | |||
93 | private function processingInstructions($token, $indexStart) { |
||
104 | |||
105 | private function import($args, $indexStart) { |
||
111 | |||
112 | private function sortRules($a, $b) { |
||
121 | |||
122 | |||
123 | private function sortPseudo($a, $b) { |
||
126 | |||
127 | private function getProperties($tokens) { |
||
138 | } |
||
139 |