1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\TextFormatter |
5
|
|
|
* @copyright Copyright (c) 2010-2017 The s9e Authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\TextFormatter\Configurator\TemplateNormalizations; |
9
|
|
|
|
10
|
|
|
use DOMAttr; |
11
|
|
|
use DOMElement; |
12
|
|
|
use s9e\TextFormatter\Configurator\Helpers\AVTHelper; |
13
|
|
|
|
14
|
|
|
abstract class AbstractConstantFolding extends AbstractNormalization |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
|
|
protected $queries = [ |
20
|
|
|
'//*[namespace-uri() != $XSL]/@*[contains(.,"{")]', |
21
|
|
|
'//xsl:value-of' |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Return the optimization passes supported by the concrete implementation |
26
|
|
|
* |
27
|
|
|
* @return array Regexps as keys, method names as values |
28
|
|
|
*/ |
29
|
|
|
abstract protected function getOptimizationPasses(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Evaluate given expression and return the result |
33
|
|
|
* |
34
|
|
|
* @param string $expr |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
protected function evaluateExpression($expr) |
38
|
|
|
{ |
39
|
|
|
$original = $expr; |
40
|
|
|
foreach ($this->getOptimizationPasses() as $regexp => $methodName) |
41
|
|
|
{ |
42
|
|
|
$regexp = str_replace(' ', '\\s*', $regexp); |
43
|
|
|
$expr = preg_replace_callback($regexp, [$this, $methodName], $expr); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return ($expr === $original) ? $expr : $this->evaluateExpression(trim($expr)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Replace constant expressions in given AVT |
51
|
|
|
* |
52
|
|
|
* @param DOMAttr $attribute |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
protected function normalizeAttribute(DOMAttr $attribute) |
56
|
|
|
{ |
57
|
|
|
AVTHelper::replace( |
58
|
|
|
$attribute, |
59
|
|
|
function ($token) |
60
|
|
|
{ |
61
|
|
|
if ($token[0] === 'expression') |
62
|
|
|
{ |
63
|
|
|
$token[1] = $this->evaluateExpression($token[1]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $token; |
67
|
|
|
} |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Replace constant expressions in given xsl:value-of element |
73
|
|
|
* |
74
|
|
|
* @param DOMElement $valueOf |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
protected function normalizeElement(DOMElement $valueOf) |
78
|
|
|
{ |
79
|
|
|
$valueOf->setAttribute('select', $this->evaluateExpression($valueOf->getAttribute('select'))); |
80
|
|
|
} |
81
|
|
|
} |