aboutsummaryrefslogtreecommitdiffstats
path: root/include/classes/BigPipe/BigPipe.php
blob: 082ca14b9f952c44cc70b018d7447df71a826596 (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
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
<?php
namespace BigPipe;

class BigPipe {
	private static $debugging = FALSE;
	private static $enabled   = TRUE;
	private static $pagelets  = [];
	private static $count     = 0;

	#===============================================================================
	# Enable or disable the pipeline mode
	#===============================================================================
	public static function enabled($change = NULL) {
		if($change !== NULL) {
			self::$enabled = (bool) $change;
		}

		return self::$enabled;
	}

	#===============================================================================
	# Return if debugging is enabled or change
	#===============================================================================
	public static function debugging($change = NULL): bool {
		if($change !== NULL) {
			self::$debugging = (bool) $change;
		}

		return self::$debugging;
	}

	#===============================================================================
	# Add a new pagelet to pipeline
	#===============================================================================
	public static function addPagelet(Pagelet $Pagelet, $priority) {
		self::$pagelets[$priority][] = $Pagelet;
		return ++self::$count;
	}

	#===============================================================================
	# Prints a single pagelet response
	#===============================================================================
	private static function singleResponse(Pagelet $Pagelet, $last = FALSE) {
		if(self::debugging()) {
			self::addDebugPhaseDoneJS($Pagelet);

			array_map('self::addDebugPhaseDoneJS', $Pagelet->getCSSResources());
			array_map('self::addDebugPhaseDoneJS', $Pagelet->getJSResources());

			usleep(rand(125, 175) * 2000);
		}

		$stylesheets = [];
		$javascripts = [];

		foreach($Pagelet->getCSSResources() as $Resource) {
			$stylesheets[$Resource->getURL()] = $Resource->getPhaseDoneJS();
		}

		foreach($Pagelet->getJSResources() as $Resource) {
			$javascripts[$Resource->getURL()] = $Resource->getPhaseDoneJS();
		}

		$pageletJSON = [
			'ID' => $Pagelet->getID(),
			'NEED' => $Pagelet->getDependencies(),
			'RSRC' => (object) [
				Resource::TYPE_STYLESHEET => (object) $stylesheets,
				Resource::TYPE_JAVASCRIPT => (object) $javascripts,
			],
			'CODE' => removeLineBreaksAndTabs($Pagelet->getJSCode()),
			'PHASE' => $Pagelet->getPhaseDoneJS()
		];

		if($last) {
			$pageletJSON['IS_LAST'] = TRUE;
		}

		$pageletHTML = removeLineBreaksAndTabs($Pagelet->getHTML());
		$pageletHTML = str_replace('--', '&#45;&#45;', $pageletHTML);

		$pageletJSON = json_encode($pageletJSON, (self::debugging() ? JSON_PRETTY_PRINT : NULL));

		echo "<code class=\"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();
	}

	#===============================================================================
	# Render the pagelets
	#===============================================================================
	public static function render() {
		self::flushOutputBuffer();

		$i = 0;

		ksort(self::$pagelets);

		foreach(array_reverse(self::$pagelets) as $priority => $pagelets) {
			foreach($pagelets as $Pagelet) {
				if(!self::enabled()) {
					foreach($Pagelet->getCSSResources() as $Resource) {
						echo $Resource->renderHTML()."\n";
					}

					foreach($Pagelet->getJSResources() as $Resource) {
						echo $Resource->renderHTML()."\n";
					}

					foreach($Pagelet->getJSCode() as $JSCode) {
						echo "<script>{$JSCode}</script>\n";
					}
				}

				else {
					self::singleResponse($Pagelet, (self::$count === ++$i));
					self::flushOutputBuffer();
				}
			}
		}
	}

	#===============================================================================
	# Add PhaseDoneJS for debugging Pagelet and Resource
	#===============================================================================
	private static function addDebugPhaseDoneJS($Instance) {
		$objpath = str_replace('\\', '|', get_class($Instance));

		if($Instance instanceof Pagelet) {
			$message = "console.log(\"%%c[{$objpath}]%%c#(%%c%s%%c): PhaseDoneJS for phase: %s\", \"font-weight:bold\", \"color:#666\", \"color:#008B45\", \"color:#666\")";

			$Instance->addPhaseDoneJS($Instance::PHASE_INIT,    sprintf($message, $Instance->getID(), 'INIT'));
			$Instance->addPhaseDoneJS($Instance::PHASE_LOADCSS, sprintf($message, $Instance->getID(), 'LOADCSS'));
			$Instance->addPhaseDoneJS($Instance::PHASE_HTML,    sprintf($message, $Instance->getID(), 'HTML'));
			$Instance->addPhaseDoneJS($Instance::PHASE_LOADJS,  sprintf($message, $Instance->getID(), 'LOADJS'));
			$Instance->addPhaseDoneJS($Instance::PHASE_DONE,    sprintf($message, $Instance->getID(), 'DONE'));

			return $Instance;
		}

		if($Instance instanceof Resource) {
			$message = "console.log(\"[{$objpath}]%%c#(%%c%s%%c): PhaseDoneJS for phase: %s\", \"color:#666\", \"color:#008B45\", \"color:#666\")";

			$Instance->addPhaseDoneJS($Instance::PHASE_INIT, sprintf($message, $Instance->getID(), 'INIT'));
			$Instance->addPhaseDoneJS($Instance::PHASE_LOAD, sprintf($message, $Instance->getID(), 'LOAD'));
			$Instance->addPhaseDoneJS($Instance::PHASE_DONE, sprintf($message, $Instance->getID(), 'DONE'));

			return $Instance;
		}
	}
}
?>