blob: 2d308f167f167857cd2d4c28487cf4219d6f93e3 (
plain)
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
|
<?php
namespace ipl\Html;
use Exception;
/**
* Text node where content creation is deferred until rendering
*
* The content is created by a passed in callback which is only called when the node is going to be rendered and
* automatically escaped to HTML.
* If the created content is already escaped, see {@link setEscaped()} to indicate this.
*
* # Example Usage
* ```
* $benchmark = new Benchmark();
*
* $performance = new DeferredText(function () use ($benchmark) {
* return $benchmark->summary();
* });
*
* execute_query();
*
* $benchmark->tick('Fetched results');
*
* generate_report();
*
* $benchmark->tick('Report generated');
*
* echo $performance;
* ```
*/
class DeferredText implements ValidHtml
{
/** @var callable will return the text that should be rendered */
protected $callback;
/** @var bool */
protected $escaped = false;
/**
* Create a new text node where content creation is deferred until rendering
*
* @param callable $callback Must return the content that should be rendered
*/
public function __construct(callable $callback)
{
$this->callback = $callback;
}
/**
* Create a new text node where content creation is deferred until rendering
*
* @param callable $callback Must return the content that should be rendered
*
* @return static
*/
public static function create(callable $callback)
{
return new static($callback);
}
/**
* Get whether the callback promises that its content is already escaped
*
* @return bool
*/
public function isEscaped()
{
return $this->escaped;
}
/**
* Set whether the callback's content is already escaped
*
* @param bool $escaped
*
* @return $this
*/
public function setEscaped($escaped = true)
{
$this->escaped = $escaped;
return $this;
}
/**
* Render text to HTML when treated like a string
*
* Calls {@link render()} internally in order to render the text to HTML.
* Exceptions will be automatically caught and returned as HTML string as well using {@link Error::render()}.
*
* @return string
*/
public function __toString()
{
try {
return $this->render();
} catch (Exception $e) {
return Error::render($e);
}
}
public function render()
{
$callback = $this->callback;
if ($this->escaped) {
return $callback();
} else {
return Html::escape($callback());
}
}
}
|