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