Completed
Push — master ( 609c6a...23e35e )
by Tom
02:19
created

SheetLoader::getRules()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 7
nc 3
nop 3
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;
8
//Separates out TSS file loading/caching from parsing
9
class SheetLoader {
10
    private $cache;
11
    private $sheet;
0 ignored issues
show
Unused Code introduced by
The property $sheet is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
    private $time;
13
    private $import = [];
14
15
    public function __construct(Cache $cache, FilePath $filePath, $tss, $time) {
16
        $this->cache = $cache;
17
        $this->filePath = $filePath;
0 ignored issues
show
Bug introduced by
The property filePath does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
        $this->tss = $tss;
0 ignored issues
show
Bug introduced by
The property tss does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
        $this->time = $time;
20
    }
21
22
	private function getRulesFromCache($file) {
23
		//The cache for the key: the filename and template prefix
24
		//Each template may have a different prefix which changes the parsed TSS,
25
		//Because of this the cache needs to be generated for each template prefix.
26
		$key = $this->getCacheKey($file);
27
		//Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet
28
		$rules = $this->cache->load($key, filemtime($file));
29
		if ($rules) {
30
			foreach ($rules['import'] as $file) {
31
				if (!$this->cache->load($this->getCacheKey($file), filemtime($file))) return false;
32
			}
33
		}
34
		return $rules;
35
	}
36
	//Allows controlling whether any updates are required to the template
37
	//e.g. return false
38
	//	 1. If all update-frequencies  haven't expired
39
	//   2. If the data hasn't changed since the last run
40
	public function updateRequired($data) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
		return true;
42
	}
43
44
	public function addImport($import) {
45
		$this->import[] = $import;
46
	}
47
48
	private function getCacheKey($file) {
49
		return $file . dirname(realpath($file)) . DIRECTORY_SEPARATOR;
50
	}
51
	//write the sheet to cache
52
    public function write($file, $rules, $imports = []) {
53
		if (is_file($file)) {
54
			$key = $this->getCacheKey($file);
55
			$existing = $this->cache->load($key, filemtime($file));
56
			if (isset($existing['import']) && empty($imports)) $imports = $existing['import'];
57
			$this->cache->write($key, ['rules' => $rules, 'import' => $imports]);
58
		}
59
		return $rules;
60
    }
61
62
	public function processRules($template, \Transphporm\Config $config) {
63
		$rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser());
64
65
		usort($rules, [$this, 'sortRules']);
66
67
		foreach ($rules as $rule) {
68
			if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config);
69
		}
70
71
		if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import);
72
	}
73
74
	//Load the TSS
75
	public function getRules($tss, $cssToXpath, $valueParser) {
76
		if (is_file($tss)) {
77
    		//$rules = $this->cache->load($tss);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
78
    		$rules = $this->getRulesFromCache($tss)['rules'];
79
			$this->filePath->addPath(dirname(realpath($tss)));
80
			if (empty($rules)) $tss = file_get_contents($tss);
81
			else return $rules;
82
    	}
83
		return (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse();
84
	}
85
86
	//Process a TSS rule e.g. `ul li {content: "foo"; format: bar}
87
	private function executeTssRule($rule, $template, $config) {
88
		$rule->touch();
89
90
		$pseudoMatcher = $config->createPseudoMatcher($rule->pseudo);
91
		$hook = new Hook\PropertyHook($rule->properties, $config->getLine(), $rule->file, $rule->line, $pseudoMatcher, $config->getValueParser(), $config->getFunctionSet(), $config->getFilePath());
0 ignored issues
show
Bug introduced by
$config->getLine() cannot be passed to __construct() as the parameter $configLine expects a reference.
Loading history...
92
		$config->loadProperties($hook);
93
		$template->addHook($rule->query, $hook);
94
	}
95
96
97
	private function sortRules($a, $b) {
98
		//If they have the same depth, compare on index
99
		if ($a->query === $b->query) return $this->sortPseudo($a, $b);
100
101
		if ($a->depth === $b->depth) $property = 'index';
102
		else $property = 'depth';
103
104
		return ($a->$property < $b->$property) ? -1 : 1;
105
	}
106
107
108
	private function sortPseudo($a, $b) {
109
		return count($a->pseudo) < count($b->pseudo)  ? -1  :1;
110
	}
111
}
112