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 $origBaseDir; |
12
|
|
|
private $newBaseDir; |
13
|
|
|
private $valueParser; |
14
|
|
|
private $pseudoMatcher; |
15
|
|
|
private $properties = []; |
16
|
|
|
private $functionSet; |
17
|
|
|
|
18
|
|
|
public function __construct(array $rules, &$origBaseDir, $newBaseDir, PseudoMatcher $pseudoMatcher, \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet) { |
19
|
|
|
$this->rules = $rules; |
20
|
|
|
$this->origBaseDir = $origBaseDir; |
21
|
|
|
$this->newBaseDir = $newBaseDir; |
22
|
|
|
$this->valueParser = $valueParser; |
23
|
|
|
$this->pseudoMatcher = $pseudoMatcher; |
24
|
|
|
$this->functionSet = $functionSet; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function run(\DomElement $element) { |
28
|
|
|
$this->functionSet->setElement($element); |
29
|
|
|
$this->origBaseDir = $this->newBaseDir; |
30
|
|
|
//Don't run if there's a pseudo element like nth-child() and this element doesn't match it |
31
|
|
|
if (!$this->pseudoMatcher->matches($element)) return; |
32
|
|
|
|
33
|
|
|
// TODO: Have all rule values parsed before running them so that things like `content-append` are not expecting tokens |
34
|
|
|
// problem with this is that anything in data changed by run properties is not shown |
35
|
|
|
// TODO: Allow `update-frequency` to be parsed before it is accessed in rule (might need to switch location of rule check) |
36
|
|
|
|
37
|
|
|
foreach ($this->rules as $name => $value) { |
38
|
|
|
$result = $this->callProperty($name, $element, $this->getArgs($value, $element)); |
39
|
|
|
if ($result === false) break; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function getArgs($value, $element) { |
|
|
|
|
44
|
|
|
return $this->valueParser->parseTokens($value); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function registerProperty($name, \Transphporm\Property $property) { |
48
|
|
|
$this->properties[$name] = $property; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function callProperty($name, $element, $value) { |
52
|
|
|
if (isset($this->properties[$name])) return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.