blob: c5f33e386397bd1730e143be6f4dcdf8977b53e6 (
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
|
<?php
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
# BigPipe main class [Thomas Lange <code@nerdmind.de>] #
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
# #
# The BigPipe main class is responsible for sorting and rendering the pagelets #
# and their associated resources. This class also provides methods to turn off #
# the pipeline mode or turn on the debugging mode. #
# #
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
namespace BigPipe;
class BigPipe {
private static $enabled = TRUE;
private static $pagelets = [];
#===============================================================================
# Enable or disable the pipeline mode
#===============================================================================
public static function enabled($change = NULL) {
if($change !== NULL) {
self::$enabled = (bool) $change;
}
return self::$enabled;
}
#===============================================================================
# Insert pagelet into queue
#===============================================================================
public static function enqueue(Pagelet $Pagelet) {
self::$pagelets[spl_object_hash($Pagelet)] = $Pagelet;
}
#===============================================================================
# Remove pagelet from queue
#===============================================================================
public static function dequeue(Pagelet $Pagelet) {
unset(self::$pagelets[spl_object_hash($Pagelet)]);
}
#===============================================================================
# Prints a single pagelet response
#===============================================================================
private static function singleResponse(Pagelet $Pagelet, $last = FALSE) {
$pageletJSON = $Pagelet->getStructure();
if($last) {
$pageletJSON['IS_LAST'] = TRUE;
}
$pageletHTML = removeLineBreaksAndTabs($Pagelet->getHTML());
$pageletHTML = str_replace('--', '--', $pageletHTML);
$pageletJSON = json_encode($pageletJSON);
echo "<code hidden id=\"_{$Pagelet->getID()}\"><!-- {$pageletHTML} --></code>\n";
echo "<script>BigPipe.onPageletArrive({$pageletJSON}, document.getElementById(\"_{$Pagelet->getID()}\"));</script>\n\n";
}
#===============================================================================
# Sends output buffer so far as possible towards user
#===============================================================================
public static function flushOutputBuffer() {
ob_flush(); flush();
}
#===============================================================================
# Renders all remaining pagelets from the queue in the appropriate order
#===============================================================================
public static function render() {
self::flushOutputBuffer();
$i = 0;
$pagelets_ordered = [];
foreach(self::$pagelets as $Pagelet) {
$pagelets_ordered[$Pagelet->getPriority()][] = $Pagelet;
}
krsort($pagelets_ordered);
if(!empty($pagelets_ordered)) {
$pagelets = call_user_func_array('array_merge', $pagelets_ordered);
foreach($pagelets as $Pagelet) {
if(!self::enabled()) {
foreach($Pagelet->getResources()[Resource::TYPE_STYLESHEET] as $Resource) {
echo "{$Resource->renderHTML()}\n";
}
foreach($Pagelet->getResources()[Resource::TYPE_JAVASCRIPT] as $Resource) {
echo "{$Resource->renderHTML()}\n";
}
foreach($Pagelet->getJSCode() as $JSCode) {
echo "<script>{$JSCode}</script>\n";
}
}
else {
self::singleResponse($Pagelet, (count(self::$pagelets) === ++$i));
self::flushOutputBuffer();
}
}
}
}
}
?>
|