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

src/Hook/PropertyHook.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\Hook;
8
/** Hooks into the template system, gets assigned as `ul li` or similar and `run()` is called with any elements that match */
9
class PropertyHook implements \Transphporm\Hook {
10
	private $rules;
11
	private $configLine;
12
	private $file;
13
    private $filePath;
14
	private $line;
15
	private $valueParser;
16
	private $pseudoMatcher;
17
	private $properties = [];
18
	private $functionSet;
19
20 View Code Duplication
	public function __construct(array $rules, &$configLine, $file, $line, PseudoMatcher $pseudoMatcher,
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
21
            \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet, \Transphporm\FilePath $filePath) {
22
		$this->rules = $rules;
23
		$this->configLine = &$configLine;
24
		$this->file = $file;
25
        $this->filePath = $filePath;
26
		$this->line = $line;
27
		$this->valueParser = $valueParser;
28
		$this->pseudoMatcher = $pseudoMatcher;
29
		$this->functionSet = $functionSet;
30
	}
31
32
	public function run(\DomElement $element) {
33
		//Set the baseDir so that all files for this rule are relative to the file it came from
34
        if ($this->file !== null) $this->filePath->setBaseDir(dirname(realpath($this->file)));
35
		$this->functionSet->setElement($element);
36
		$this->configLine = $this->line;
37
		try {
38
			//Don't run if there's a pseudo element like nth-child() and this element doesn't match it
39
			if (!$this->pseudoMatcher->matches($element)) return;
40
			$this->callProperties($element);
41
		}
42
		catch (\Transphporm\RunException $e) {
43
			throw new \Transphporm\Exception($e, $this->file, $this->line);
44
		}
45
	}
46
47
	// TODO: Have all rule values parsed before running them so that things like `content-append` are not expecting tokens
48
	// problem with this is that anything in data changed by run properties is not shown
49
	// TODO: Allow `update-frequency` to be parsed before it is accessed in rule (might need to switch location of rule check)
50
	private function callProperties($element) {
51
		foreach ($this->rules as $name => $value) {
52
			$result = $this->callProperty($name, $element, $this->getArgs($value));
53
			if ($result === false) break;
54
		}
55
	}
56
	private function getArgs($value) {
57
		return $this->valueParser->parseTokens($value);
58
	}
59
60
	public function registerProperty($name, \Transphporm\Property $property) {
61
		$this->properties[$name] = $property;
62
	}
63
64
	private function callProperty($name, $element, $value) {
65
		if (isset($this->properties[$name])) {
66
			try {
67
				return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties);
68
			}
69
			catch (\Exception $e) {
70
				if ($e instanceof \Transphporm\RunException) throw $e;
71
				throw new \Transphporm\RunException(\Transphporm\Exception::PROPERTY, $name, $e);
72
			}
73
		}
74
	}
75
}
76