1
|
|
|
<?php |
2
|
|
|
/* @description Transformation Style Sheets - Revolutionising PHP templating * |
3
|
|
|
* @author Tom Butler [email protected] * |
4
|
|
|
* @copyright 2015 Tom Butler <[email protected]> | https://r.je/ * |
5
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License * |
6
|
|
|
* @version 1.0 */ |
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 $baseDir; |
12
|
|
|
private $configLine; |
13
|
|
|
private $file; |
14
|
|
|
private $line; |
15
|
|
|
private $valueParser; |
16
|
|
|
private $pseudoMatcher; |
17
|
|
|
private $properties = []; |
18
|
|
|
private $functionSet; |
19
|
|
|
|
20
|
|
|
public function __construct(array $rules, &$baseDir, &$configLine, $file, $line, PseudoMatcher $pseudoMatcher, \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet) { |
21
|
|
|
$this->rules = $rules; |
22
|
|
|
$this->baseDir = &$baseDir; |
23
|
|
|
$this->configLine = &$configLine; |
24
|
|
|
$this->file = $file; |
25
|
|
|
$this->line = $line; |
26
|
|
|
$this->valueParser = $valueParser; |
27
|
|
|
$this->pseudoMatcher = $pseudoMatcher; |
28
|
|
|
$this->functionSet = $functionSet; |
29
|
|
|
if ($this->file !== null) $this->baseDir = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function run(\DomElement $element) { |
33
|
|
|
$this->functionSet->setElement($element); |
34
|
|
|
$this->configLine = $this->line; |
35
|
|
|
try { |
36
|
|
|
//Don't run if there's a pseudo element like nth-child() and this element doesn't match it |
37
|
|
|
if (!$this->pseudoMatcher->matches($element)) return; |
38
|
|
|
$this->callProperties($element); |
39
|
|
|
} |
40
|
|
|
catch (\Transphporm\RunException $e) { |
41
|
|
|
throw new \Transphporm\Exception($e, $this->file, $this->line); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// TODO: Have all rule values parsed before running them so that things like `content-append` are not expecting tokens |
46
|
|
|
// problem with this is that anything in data changed by run properties is not shown |
47
|
|
|
// TODO: Allow `update-frequency` to be parsed before it is accessed in rule (might need to switch location of rule check) |
48
|
|
|
private function callProperties($element) { |
49
|
|
|
foreach ($this->rules as $name => $value) { |
50
|
|
|
$result = $this->callProperty($name, $element, $this->getArgs($value)); |
51
|
|
|
if ($result === false) break; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
private function getArgs($value) { |
55
|
|
|
return $this->valueParser->parseTokens($value); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function registerProperty($name, \Transphporm\Property $property) { |
59
|
|
|
$this->properties[$name] = $property; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function callProperty($name, $element, $value) { |
63
|
|
|
if (isset($this->properties[$name])) { |
64
|
|
|
try { |
65
|
|
|
return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties); |
66
|
|
|
} |
67
|
|
|
catch (\Exception $e) { |
68
|
|
|
if ($e instanceof \Transphporm\RunException) throw $e; |
69
|
|
|
throw new \Transphporm\RunException(\Transphporm\Exception::PROPERTY, $name, $e); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|