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\Property; |
8
|
|
|
class Content implements \Transphporm\Property { |
9
|
|
|
private $headers; |
10
|
|
|
private $formatter; |
11
|
|
|
|
12
|
|
|
public function __construct(&$headers, \Transphporm\Hook\Formatter $formatter) { |
13
|
|
|
$this->headers = &$headers; |
14
|
|
|
$this->formatter = $formatter; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
18
|
|
|
if (!$this->shouldRun($element)) return false; |
19
|
|
|
|
20
|
|
|
$values = $this->formatter->format($values, $rules); |
21
|
|
|
|
22
|
|
|
if (!$this->processPseudo($values, $element, $pseudoMatcher)) { |
23
|
|
|
//Remove the current contents |
24
|
|
|
$this->removeAllChildren($element); |
25
|
|
|
//Now make a text node |
26
|
|
|
if ($this->getContentMode($rules) === 'replace') $this->replaceContent($element, $values); |
27
|
|
|
else $this->appendContent($element, $values); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private function shouldRun($element) { |
32
|
|
|
do { |
33
|
|
|
if ($element->getAttribute('transphporm') == 'includedtemplate') return false; |
34
|
|
|
} |
35
|
|
|
while (($element = $element->parentNode) instanceof \DomElement); |
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function getContentMode($rules) { |
40
|
|
|
return (isset($rules['content-mode'])) ? $rules['content-mode']->read() : 'append'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function processPseudo($value, $element, $pseudoMatcher) { |
44
|
|
|
$pseudoContent = ['attr', 'header', 'before', 'after']; |
45
|
|
|
foreach ($pseudoContent as $pseudo) { |
46
|
|
|
if ($pseudoMatcher->hasFunction($pseudo)) { |
47
|
|
|
$this->$pseudo($value, $pseudoMatcher->getFuncArgs($pseudo, $element)[0], $element); |
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function getNode($node, $document) { |
55
|
|
|
foreach ($node as $n) { |
56
|
|
|
if (is_array($n)) { |
57
|
|
|
foreach ($this->getNode($n, $document) as $new) yield $new; |
58
|
|
|
} |
59
|
|
|
else { |
60
|
|
|
yield $this->convertNode($n, $document); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function convertNode($node, $document) { |
66
|
|
|
if ($node instanceof \DomElement) { |
67
|
|
|
$new = $document->importNode($node, true); |
68
|
|
|
//Removing this might cause problems with caching... |
69
|
|
|
//$new->setAttribute('transphporm', 'added'); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
else { |
72
|
|
|
if ($node instanceof \DomText) $node = $node->nodeValue; |
73
|
|
|
$new = $document->createElement('text'); |
74
|
|
|
|
75
|
|
|
$new->appendChild($document->createTextNode($node)); |
76
|
|
|
$new->setAttribute('transphporm', 'text'); |
77
|
|
|
} |
78
|
|
|
return $new; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** Functions for writing to pseudo elements, attr, before, after, header */ |
82
|
|
|
private function attr($value, $pseudoArgs, $element) { |
83
|
|
|
|
84
|
|
|
$element->setAttribute($pseudoArgs, implode('', $value)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function header($value, $pseudoArgs, $element) { |
|
|
|
|
88
|
|
|
$this->headers[] = [$pseudoArgs, implode('', $value)]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function before($value, $pseudoArgs, $element) { |
|
|
|
|
92
|
|
|
foreach ($this->getNode($value, $element->ownerDocument) as $node) { |
93
|
|
|
$element->insertBefore($node, $element->firstChild); |
94
|
|
|
} |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function after($value, $pseudoArgs, $element) { |
|
|
|
|
99
|
|
|
foreach ($this->getNode($value, $element->ownerDocument) as $node) { |
100
|
|
|
$element->appendChild($node); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function replaceContent($element, $content) { |
105
|
|
|
//If this rule was cached, the elements that were added last time need to be removed prior to running the rule again. |
106
|
|
|
foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
107
|
|
|
$element->parentNode->insertBefore($node, $element); |
108
|
|
|
} |
109
|
|
|
$element->setAttribute('transphporm', 'remove'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function appendContent($element, $content) { |
113
|
|
|
foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
114
|
|
|
$element->appendChild($node); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function removeAllChildren($element) { |
119
|
|
|
while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.