aboutsummaryrefslogtreecommitdiffstats
path: root/include/classes/class.bigpipe.php
blob: 1edefc877dcf2e84b1e6ae02ef777969eae0e090 (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
<?php
class BigPipe {
	public  static $enabled  = TRUE;
	private static $pagelets = [];
	private static $count    = 0;

	#====================================================================================================
	# Gibt TRUE zurück wenn BigPipe eingeschaltet ist
	#====================================================================================================
	public static function isEnabled() {
		return self::$enabled ? TRUE : FALSE;
	}

	#====================================================================================================
	# Neues Pagelet zur Pipeline hinzufügen
	#====================================================================================================
	public static function addPagelet(Pagelet $Pagelet, $priority) {
		self::$pagelets[$priority][] = $Pagelet;
		self::$count++;
	}

	#====================================================================================================
	# Gibt einen einzelnen Pagelet-Response aus
	#====================================================================================================
	private static function pageletResponse(Pagelet $Pagelet, $async = FALSE, $last = FALSE) {
		$data = [
			'ID' => $Pagelet->getID(),
			'RESOURCES' => ['CSS' => $Pagelet->getCSSFiles(), 'JS' => $Pagelet->getJSFiles(), 'JS_CODE' => removeLineBreaksAndTabs($Pagelet->getJSCode())]
		];

		if($last) {
			$data['IS_LAST'] = true;
		}

		echo '<code class="hidden" id="_'.$data['ID'].'"><!-- '.str_replace('--', '&#45;&#45;', removeLineBreaksAndTabs($Pagelet->getHTML())).' --></code>'."\n";
		echo '<script>BigPipe.onPageletArrive('.json_encode($data).($async ? ', document.getElementById("_'.$Pagelet->getID().'").innerHTML' : NULL).');</script>'."\n\n";
	}

	#====================================================================================================
	# Sendet den Output-Buffer so weit wie möglich in Richtung User
	#====================================================================================================
	public static function flushOutputBuffer() {
		ob_flush(); flush();
	}

	#====================================================================================================
	# Alle Pagelets an Client schicken
	#====================================================================================================
	public static function render($async = FALSE) {
		self::flushOutputBuffer();

		$i = 0;

		ksort(self::$pagelets);

		foreach(array_reverse(self::$pagelets) as $priority => $pagelets) {
			foreach($pagelets as $Pagelet) {
				if(!self::isEnabled()) {
					if($Pagelet->getJSCode()) {
						echo '<script>'.$Pagelet->getJSCode().'</script>'."\n";
					}

					foreach($Pagelet->getCSSFiles() as $CSSFile) {
						echo '<link href="'.$CSSFile.'" rel="stylesheet" />'."\n";
					}

					foreach($Pagelet->getJSFiles() as $JSFile) {
						echo '<script src="'.$JSFile.'"></script>'."\n";
					}
				}

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