forked from phptal/PHPTAL
/
FillSlot.php
182 lines (163 loc) · 5.74 KB
/
FillSlot.php
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
declare(strict_types=1);
/**
* PHPTAL templating engine
*
* @category HTML
* @package PHPTAL
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
* @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @link http://phptal.org/
*/
namespace PhpTal\Php\Attribute\METAL;
use PhpTal\Dom\Attr;
use PhpTal\Dom\Element;
use PhpTal\Dom\Node;
use PhpTal\Php\Attribute;
use PhpTal\Php\CodeWriter;
use PhpTal\TalNamespace\Builtin;
/**
* METAL Specification 1.0
*
* argument ::= Name
*
* Example:
*
* <table metal:use-macro="here/doc1/macros/sidebar">
* <tr><th>Links</th></tr>
* <tr><td metal:fill-slot="links">
* <a href="http://www.goodplace.com">Good Place</a><br>
* <a href="http://www.badplace.com">Bad Place</a><br>
* <a href="http://www.otherplace.com">Other Place</a>
* </td></tr>
* </table>
*
* PHPTAL:
*
* 1. evaluate slots
*
* <?php ob_start(); ? >
* <td>
* <a href="http://www.goodplace.com">Good Place</a><br>
* <a href="http://www.badplace.com">Bad Place</a><br>
* <a href="http://www.otherplace.com">Other Place</a>
* </td>
* <?php $tpl->slots->links = ob_get_contents(); ob_end_clean(); ? >
*
* 2. call the macro (here not supported)
*
* <?php echo phptal_macro($tpl, 'master_page.html/macros/sidebar'); ? >
*
*
* @package PHPTAL
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
*/
class FillSlot extends Attribute
{
public const CALLBACK_THRESHOLD = 10000;
/**
* @var int
*/
private static $uid = 0;
/**
* @var string
*/
private $function_name;
/**
* Called before element printing.
*
* @param CodeWriter $codewriter
*/
public function before(CodeWriter $codewriter): void
{
if ($this->shouldUseCallback()) {
$function_base_name = 'slot_' . preg_replace('/[^a-z0-9]/', '_', $this->expression) . '_' . (self::$uid++);
$codewriter->doFunction($function_base_name, '\PhpTal\PHPTAL $_thistpl, \PhpTal\PHPTAL $tpl');
$this->function_name = $codewriter->getFunctionPrefix() . $function_base_name;
$codewriter->doSetVar('$ctx', '$tpl->getContext()');
$codewriter->doInitTranslator();
} else {
$codewriter->pushCode('ob_start()');
$this->function_name = null;
}
}
/**
* Called after element printing.
*
* @param CodeWriter $codewriter
*
* @throws \PhpTal\Exception\PhpTalException
*/
public function after(CodeWriter $codewriter): void
{
if ($this->function_name !== null) {
$codewriter->doEnd();
$codewriter->pushCode(
'$ctx->fillSlotCallback(' . $codewriter->str($this->expression) . ', '
. $codewriter->str($this->function_name) . ', $_thistpl, clone $tpl)'
);
} else {
$codewriter->pushCode('$ctx->fillSlot(' . $codewriter->str($this->expression) . ', ob_get_clean())');
}
}
/**
* inspects contents of the element to decide whether callback makes sense
*/
private function shouldUseCallback(): bool
{
// since callback is slightly slower than buffering,
// use callback only for content that is large to offset speed loss by memory savings
return $this->estimateNumberOfBytesOutput($this->phpelement, false) > self::CALLBACK_THRESHOLD;
}
/**
* @param Element $element
* @param bool $is_nested_in_repeat true if any parent element has tal:repeat
*
* @return int
*/
private function estimateNumberOfBytesOutput(Element $element, bool $is_nested_in_repeat): int
{
// macros don't output anything on their own
if ($element->hasAttributeNS(Builtin::NS_METAL, 'define-macro')) {
return 0;
}
$estimated_bytes = 2 * (3 + strlen($element->getQualifiedName()));
foreach ($element->getAttributeNodes() as $attr) {
$estimated_bytes += 4 + strlen($attr->getQualifiedName());
if ($attr->getReplacedState() === Attr::NOT_REPLACED) {
$estimated_bytes += strlen($attr->getValueEscaped()); // this is shoddy for replaced attributes
}
}
$has_repeat_attr = $element->hasAttributeNS(Builtin::NS_TAL, 'repeat');
$isRepeating = $has_repeat_attr || $is_nested_in_repeat;
if ($element->hasAttributeNS(Builtin::NS_TAL, 'content') ||
$element->hasAttributeNS(Builtin::NS_TAL, 'replace')) {
// assume that output in loops is shorter (e.g. table rows) than outside (main content)
$estimated_bytes += $isRepeating ? 500 : 2000;
} else {
foreach ($element->childNodes as $node) {
if ($node instanceof Element) {
$estimated_bytes += $this->estimateNumberOfBytesOutput(
$node,
$isRepeating
);
} else {
/** @var Node $node */
$estimated_bytes += strlen($node->getValueEscaped());
}
}
}
if ($element->hasAttributeNS(Builtin::NS_METAL, 'use-macro')) {
$estimated_bytes += $isRepeating ? 500 : 2000;
}
if ($element->hasAttributeNS(Builtin::NS_TAL, 'condition')) {
$estimated_bytes /= 2; // naively assuming 50% chance, that works well with if/else pattern
}
if ($element->hasAttributeNS(Builtin::NS_TAL, 'repeat')) {
// assume people don't write big nested loops
$estimated_bytes *= $is_nested_in_repeat ? 5 : 10;
}
return (int) $estimated_bytes;
}
}