-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathTemplate.php
More file actions
149 lines (112 loc) · 3.7 KB
/
Template.php
File metadata and controls
149 lines (112 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
*
* Generalized 'in CSS' templating.
*
*/
namespace CssCrush;
class Template
{
// Positional argument default values.
public $defaults = [];
// The number of expected arguments.
public $argCount = 0;
public $substitutions;
// The string passed in with arg calls replaced by tokens.
public $string;
public function __construct($str)
{
static $templateFunctions;
if (! $templateFunctions) {
$templateFunctions = new Functions();
}
$str = Template::unTokenize($str);
// Parse all arg function calls in the passed string,
// callback creates default values.
$self = $this;
$captureCallback = function ($str) use (&$self)
{
$args = Functions::parseArgsSimple($str);
$position = array_shift($args);
// Match the argument index integer.
if (! isset($position) || ! ctype_digit($position)) {
return '';
}
// Store the default value.
$defaultValue = isset($args[0]) ? $args[0] : null;
if (isset($defaultValue)) {
$self->defaults[$position] = $defaultValue;
}
// Update argument count.
$argNumber = ((int) $position) + 1;
$self->argCount = max($self->argCount, $argNumber);
return "?a$position?";
};
$templateFunctions->register['#'] = $captureCallback;
$this->string = $templateFunctions->apply($str);
}
public function __invoke(?array $args = null, $str = null)
{
$str = isset($str) ? $str : $this->string;
// Apply passed arguments as priority.
if (isset($args)) {
list($find, $replace) = $this->prepare($args, false);
}
// Secondly use prepared substitutions if available.
elseif ($this->substitutions) {
list($find, $replace) = $this->substitutions;
}
// Apply substitutions.
if (isset($find) && isset($replace)) {
$str = str_replace($find, $replace, $str);
}
return Template::tokenize($str);
}
public function getArgValue($index, &$args)
{
// First lookup a passed value.
if (isset($args[$index]) && $args[$index] !== 'default') {
return $args[$index];
}
// Get a default value.
$default = isset($this->defaults[$index]) ? $this->defaults[$index] : '';
// Recurse for nested arg() calls.
while (preg_match(Regex::$patt->a_token, $default, $m)) {
$default = str_replace(
$m[0],
$this->getArgValue((int) $m[1], $args),
$default);
}
return $default;
}
public function prepare(array $args, $persist = true)
{
// Create table of substitutions.
$find = [];
$replace = [];
if ($this->argCount) {
$argIndexes = range(0, $this->argCount-1);
foreach ($argIndexes as $index) {
$find[] = "?a$index?";
$replace[] = $this->getArgValue($index, $args);
}
}
$substitutions = [$find, $replace];
// Persist substitutions by default.
if ($persist) {
$this->substitutions = $substitutions;
}
return $substitutions;
}
public static function tokenize($str)
{
$str = Crush::$process->tokens->capture($str, 's');
$str = Crush::$process->tokens->capture($str, 'u');
return $str;
}
public static function unTokenize($str)
{
$str = Crush::$process->tokens->restore($str, ['u', 's']);
return $str;
}
}