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

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

	#===============================================================================
	# Return TRUE if the pipeline is enabled
	#===============================================================================
	public static function isEnabled() {
		return self::$enabled;
	}

	#===============================================================================
	# Enable or disable the pipeline mode
	#===============================================================================
	public static function enablePipeline($enabled = TRUE) {
		return self::$enabled = (bool) $enabled;
	}

	#===============================================================================
	# 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) {
		$pageletJSON = [
			'ID' => $Pagelet->getID(), 'NEED' => $Pagelet->getDependencies(),
			'RESOURCES' => ['CSS' => $Pagelet->getCSSFiles(), 'JS' => $Pagelet->getJSFiles(), 'JS_CODE' => removeLineBreaksAndTabs($Pagelet->getJSCode())],
			'PHASES' => (object) $Pagelet->getPhaseDoneJS()
		];

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

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

		$pageletJSON = json_encode($pageletJSON, (self::$debug ? 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::isEnabled()) {
					foreach($Pagelet->getCSSFiles() as $CSSFile) {
						echo "<link href=\"{$CSSFile}\" rel=\"stylesheet\" />\n";
					}

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

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

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

					self::$debug AND usleep((rand(250, 1000) * 1000));
				}
			}
		}
	}
}