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