aboutsummaryrefslogtreecommitdiffstats
path: root/include/classes/BigPipe/BigPipe.php
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2015-07-25 21:55:05 +0200
committerThomas Lange <code@nerdmind.de>2015-07-25 21:55:05 +0200
commit903100ecb99ab926d4ac3be4eba11ea1c9800e12 (patch)
tree44d025c884bffe7f90c6055a6476b25e4cf0271c /include/classes/BigPipe/BigPipe.php
parent6abf184af5be6b8932373fc5f92afe46e0a0a2c9 (diff)
downloadbigpipe-903100ecb99ab926d4ac3be4eba11ea1c9800e12.tar.gz
bigpipe-903100ecb99ab926d4ac3be4eba11ea1c9800e12.tar.xz
bigpipe-903100ecb99ab926d4ac3be4eba11ea1c9800e12.zip
Namespaces added
Diffstat (limited to 'include/classes/BigPipe/BigPipe.php')
-rwxr-xr-xinclude/classes/BigPipe/BigPipe.php81
1 files changed, 81 insertions, 0 deletions
diff --git a/include/classes/BigPipe/BigPipe.php b/include/classes/BigPipe/BigPipe.php
new file mode 100755
index 0000000..b69f1d8
--- /dev/null
+++ b/include/classes/BigPipe/BigPipe.php
@@ -0,0 +1,81 @@
+<?php
+namespace BigPipe;
+
+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();
+ }
+ }
+ }
+ }
+} \ No newline at end of file