Completed
Push — master ( 02c8a1...58faf5 )
by Tom
02:14
created

src/SheetLoader/SheetLoader.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\SheetLoader;
8
//Separates out TSS file loading/caching from parsing
9
class SheetLoader {
10
    private $tss;
11
    private $filePath;
12
    private $time;
13
    private $import = [];
14
15
    public function __construct(\Transphporm\Cache $cache, \Transphporm\FilePath $filePath, TSSRules $tss, $time) {
16
    	$this->cache = $cache;
17
        $this->filePath = $filePath;
18
        $this->tss = $tss;
19
        $this->time = $time ?? time();
20
    }
21
22
	//Allows controlling whether any updates are required to the template
23
	//e.g. return false
24
	//	 1. If all update-frequencies  haven't expired
25
	//   2. If the data hasn't changed since the last run
26
	//If this function returns false, the rendered template is sent straight from the cache skipping 99% of transphporm's code
27
	public function updateRequired($data) {
28
		return $this->tss->updateRequired($data);
29
	}
30
31
	public function addImport($import) {
32
		$this->filePath->addPath(dirname(realpath($this->filePath->getFilePath($import))));
33
		$this->import[] = $import;
34
	}
35
36
	public function setCacheKey($tokens) {
37
		$newTokens = [];
38
		foreach ($tokens as $token) {
39
			if ($token['type'] == \Transphporm\Parser\Tokenizer::NAME && $token['value'] == 'data') {
40
				$tokens->next();
41
				$newTokens = array_merge($newTokens, iterator_to_array($tokens->current()['value']));
42
			}
43
			else $newTokens[] = $token;
44
		}
45
46
		$this->tss->setCacheKey(new \Transphporm\Parser\Tokens($newTokens));
47
	}
48
49
	public function getCacheKey($data) {
50
		return $this->tss->getCacheKey($data);
51
	}
52
53
54
	public function processRules($template, \Transphporm\Config $config) {
55
		$rules = $this->getRules($config->getCssToXpath(), $config->getValueParser());
56
57
58
		usort($rules, [$this, 'sortRules']);
59
60
		foreach ($rules as $rule) {
61
			if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config);
62
		}
63
64
		//if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import);
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
		$this->tss->write($rules, $this->import);
66
	}
67
68
	//Load the TSS
69
	public function getRules($cssToXpath, $valueParser, $indexStart = 0) {
70
		return $this->tss->getRules($cssToXpath, $valueParser, $this, $indexStart);
71
	}
72
73
	//Process a TSS rule e.g. `ul li {content: "foo"; format: bar}
74
	private function executeTssRule($rule, $template, $config) {
75
		$rule->touch();
76
77
		$pseudoMatcher = $config->createPseudoMatcher($rule->pseudo);
78
		$hook = new \Transphporm\Hook\PropertyHook($rule->properties, $config->getLine(), $rule->file, $rule->line, $pseudoMatcher, $config->getValueParser(), $config->getFunctionSet(), $config->getFilePath());
79
		$config->loadProperties($hook);
80
		$template->addHook($rule->query, $hook);
81
	}
82
83
84
	private function sortRules($a, $b) {
85
		//If they have the same depth, compare on index
86
		if ($a->query === $b->query) return $this->sortPseudo($a, $b);
87
88
		if ($a->depth === $b->depth) $property = 'index';
89
		else $property = 'depth';
90
91
		return ($a->$property < $b->$property) ? -1 : 1;
92
	}
93
94
95
	private function sortPseudo($a, $b) {
96
		return count($a->pseudo) > count($b->pseudo)  ? 1 : -1;
97
	}
98
}
99