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\TSSFunction; |
8
|
|
|
/* Handles template() function calls from the stylesheet */ |
9
|
|
|
class Template implements \Transphporm\TSSFunction { |
10
|
|
|
private $elementData; |
11
|
|
|
private $baseDir; |
12
|
|
|
|
13
|
|
|
public function __construct(\Transphporm\Hook\ElementData $elementData, &$baseDir) { |
14
|
|
|
$this->baseDir = &$baseDir; |
15
|
|
|
$this->elementData = $elementData; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
private function readArray($array, $index) { |
19
|
|
|
return isset($array[$index]) ? $array[$index] : null; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function run(array $args, \DomElement $element) { |
23
|
|
|
$selector = $this->readArray($args, 1); |
24
|
|
|
$tss = $this->readArray($args, 2); |
25
|
|
|
$newTemplate = new \Transphporm\Builder($this->baseDir . $args[0], $tss ? $this->baseDir . $tss : null); |
26
|
|
|
|
27
|
|
|
$doc = $newTemplate->output($this->elementData->getData($element), true)->body; |
28
|
|
|
if ($selector != '') return $this->templateSubsection($doc, $selector); |
29
|
|
|
|
30
|
|
|
$newNode = $doc->documentElement; |
31
|
|
|
$result = []; |
32
|
|
|
if ($newNode->tagName === 'template') { |
33
|
|
|
foreach ($newNode->childNodes as $node) { |
34
|
|
|
$result[] = $this->getClonedElement($node, $tss); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
//else $result[] = $newNode; |
|
|
|
|
38
|
|
|
return $result; |
39
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function templateSubsection($doc, $selector) { |
43
|
|
|
$xpathStr = (new \Transphporm\Parser\CssToXpath($selector, new \Transphporm\Parser\Value($this)))->getXpath(); |
44
|
|
|
$xpath = new \DomXpath($doc); |
45
|
|
|
$nodes = $xpath->query($xpathStr); |
46
|
|
|
$result = []; |
47
|
|
|
foreach ($nodes as $node) { |
48
|
|
|
$result[] = $node; |
49
|
|
|
} |
50
|
|
|
return $result; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function getClonedElement($node, $tss) { |
54
|
|
|
$clone = $node->cloneNode(true); |
55
|
|
|
if ($tss != null && $clone instanceof \DomElement) $clone->setAttribute('transphporm', 'includedtemplate'); |
56
|
|
|
return $clone; |
57
|
|
|
} |
58
|
|
|
} |
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.