|
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; |
|
8
|
|
|
/** Parses "string" and function(args) e.g. data(foo) or iteration(bar) */ |
|
9
|
|
|
class ValueParser { |
|
10
|
|
|
private $dataFunction; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct(Hook\DataFunction $dataFunction) { |
|
13
|
|
|
$this->dataFunction = $dataFunction; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
private function extractQuotedString($marker, $str) { |
|
17
|
|
|
$finalPos = $this->findMatchingPos($str, $marker); |
|
18
|
|
|
$string = substr($str, 1, $finalPos-1); |
|
19
|
|
|
//Now remove escape characters |
|
20
|
|
|
return str_replace('\\' . $marker, $marker, $string); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
private function parseFunction($function) { |
|
24
|
|
|
$open = strpos($function, '('); |
|
25
|
|
|
if ($open) { |
|
26
|
|
|
$close = strpos($function, ')', $open); |
|
27
|
|
|
|
|
28
|
|
|
//Count the number of fresh opening ( before $close |
|
29
|
|
|
$cPos = $open+1; |
|
30
|
|
|
while (($cPos = strpos($function, '(', $cPos+1)) !== false && $cPos < $close) $close = strpos($function, ')', $close+1); |
|
31
|
|
|
|
|
32
|
|
|
$name = substr($function, 0, $open); |
|
33
|
|
|
|
|
34
|
|
|
$params = substr($function, $open+1, $close-$open-1); |
|
35
|
|
|
return ['name' => $name, 'params' => $params, 'endPoint' => $close]; |
|
36
|
|
|
} |
|
37
|
|
|
else return ['name' => null, 'params' => $function, 'endPoint' => strlen($function)]; |
|
38
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function parse($function, \DomElement $element = null) { |
|
42
|
|
|
$result = []; |
|
43
|
|
|
if ($function && in_array($function[0], ['\'', '"'])) { |
|
44
|
|
|
$finalPos = $this->findMatchingPos($function, $function[0]); |
|
45
|
|
|
$result[] = $this->extractQuotedString($function[0], $function); |
|
46
|
|
|
} |
|
47
|
|
|
else { |
|
48
|
|
|
$func = $this->parseFunction($function); |
|
49
|
|
|
$finalPos = $func['endPoint']; |
|
50
|
|
|
|
|
51
|
|
|
if (($data = $this->callFunc($func['name'], $func['params'], $element)) !== false) { |
|
52
|
|
|
$result = $this->appendToArray($result, $data); |
|
53
|
|
|
} |
|
54
|
|
|
else $result[] = trim($function); |
|
55
|
|
|
} |
|
56
|
|
|
$remaining = trim(substr($function, $finalPos+1)); |
|
57
|
|
|
return $this->parseNextValue($remaining, $result, $element); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function appendToArray($array, $value) { |
|
61
|
|
|
if (is_array($value)) $array += $value; |
|
62
|
|
|
else $array[] = $value; |
|
63
|
|
|
return $array; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function callFunc($name, $params, $element) { |
|
67
|
|
|
if ($name && is_callable([$this->dataFunction, $name])) { |
|
68
|
|
|
return $this->dataFunction->$name($this->parse($params, $element), $element); |
|
69
|
|
|
} |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function parseNextValue($remaining, $result, $element) { |
|
74
|
|
|
if (strlen($remaining) > 0 && $remaining[0] == ',') $result = array_merge($result, $this->parse(trim(substr($remaining, 1)), $element)); |
|
75
|
|
|
return $result; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function findMatchingPos($string, $char, $start = 0, $escape = '\\') { |
|
79
|
|
|
$pos = $start+1; |
|
80
|
|
|
$end = 0; |
|
|
|
|
|
|
81
|
|
|
while ($end = strpos($string, $char, $pos)) { |
|
82
|
|
|
if ($string[$end-1] === $escape) $pos = $end+1; |
|
83
|
|
|
else { |
|
84
|
|
|
break; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
return $end; |
|
88
|
|
|
} |
|
89
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.