From 6658708bdd775a6ab1352e0c36b4c7deafc52e84 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Sat, 26 Dec 2015 16:37:25 +0100 Subject: Multiple updates; new feature PhaseDoneJS --- include/classes/BigPipe/BigPipe.php | 42 +++++++++++++------- include/classes/BigPipe/DemoPagelet.php | 17 ++++++++ include/classes/BigPipe/Pagelet.php | 69 +++++++++++++++++++++++++-------- include/functions.php | 2 +- 4 files changed, 98 insertions(+), 32 deletions(-) create mode 100755 include/classes/BigPipe/DemoPagelet.php (limited to 'include') diff --git a/include/classes/BigPipe/BigPipe.php b/include/classes/BigPipe/BigPipe.php index b69f1d8..740d047 100755 --- a/include/classes/BigPipe/BigPipe.php +++ b/include/classes/BigPipe/BigPipe.php @@ -2,53 +2,65 @@ namespace BigPipe; class BigPipe { - public static $enabled = TRUE; + private static $enabled = TRUE; + private static $debug = TRUE; private static $pagelets = []; private static $count = 0; #==================================================================================================== - # Gibt TRUE zurück wenn BigPipe eingeschaltet ist + # Return TRUE if the pipeline is enabled #==================================================================================================== public static function isEnabled() { - return self::$enabled ? TRUE : FALSE; + return self::$enabled; } #==================================================================================================== - # Neues Pagelet zur Pipeline hinzufügen + # 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; - self::$count++; + return ++self::$count; } #==================================================================================================== - # Gibt einen einzelnen Pagelet-Response aus + # Prints a single pagelet response #==================================================================================================== - private static function pageletResponse(Pagelet $Pagelet, $async = FALSE, $last = FALSE) { + private static function singleResponse(Pagelet $Pagelet, $last = FALSE) { $data = [ 'ID' => $Pagelet->getID(), - 'RESOURCES' => ['CSS' => $Pagelet->getCSSFiles(), 'JS' => $Pagelet->getJSFiles(), 'JS_CODE' => removeLineBreaksAndTabs($Pagelet->getJSCode())] + 'RESOURCES' => ['CSS' => $Pagelet->getCSSFiles(), 'JS' => $Pagelet->getJSFiles(), 'JS_CODE' => removeLineBreaksAndTabs($Pagelet->getJSCode())], + 'PHASES' => (object) $Pagelet->getPhaseDoneJS(), ]; if($last) { $data['IS_LAST'] = true; } - echo ''."\n"; - echo ''."\n\n"; + $pageletHTML = str_replace('--', '--', removeLineBreaksAndTabs($Pagelet->getHTML())); + $pageletJSON = json_encode($data, (self::$debug ? JSON_PRETTY_PRINT : FALSE)); + + echo "getID()}\">\n"; + echo "\n\n"; } #==================================================================================================== - # Sendet den Output-Buffer so weit wie möglich in Richtung User + # Sends output buffer so far as possible towards user #==================================================================================================== public static function flushOutputBuffer() { ob_flush(); flush(); } #==================================================================================================== - # Alle Pagelets an Client schicken + # Render the pagelets #==================================================================================================== - public static function render($async = FALSE) { + public static function render() { self::flushOutputBuffer(); $i = 0; @@ -72,8 +84,10 @@ class BigPipe { } else { - self::pageletResponse($Pagelet, $async, (self::$count === ++$i)); + self::singleResponse($Pagelet, (self::$count === ++$i)); self::flushOutputBuffer(); + + self::$debug AND usleep((rand(250, 1000) * 1000)); } } } diff --git a/include/classes/BigPipe/DemoPagelet.php b/include/classes/BigPipe/DemoPagelet.php new file mode 100755 index 0000000..a6c61e1 --- /dev/null +++ b/include/classes/BigPipe/DemoPagelet.php @@ -0,0 +1,17 @@ +addPhaseDoneJS(self::PHASE_ARRIVE, 'console.log("'.sprintf($message, $this->getID(), 'ARRIVE').'")'); + $this->addPhaseDoneJS(self::PHASE_LOADCSS, 'console.log("'.sprintf($message, $this->getID(), 'LOADCSS').'")'); + $this->addPhaseDoneJS(self::PHASE_PUTHTML, 'console.log("'.sprintf($message, $this->getID(), 'PUTHTML').'")'); + $this->addPhaseDoneJS(self::PHASE_LOADJS, 'console.log("'.sprintf($message, $this->getID(), 'LOADJS').'")'); + $this->addPhaseDoneJS(self::PHASE_EXECJS, 'console.log("'.sprintf($message, $this->getID(), 'EXECJS').'")'); + } +} \ No newline at end of file diff --git a/include/classes/BigPipe/Pagelet.php b/include/classes/BigPipe/Pagelet.php index 6fe0032..d1c335f 100755 --- a/include/classes/BigPipe/Pagelet.php +++ b/include/classes/BigPipe/Pagelet.php @@ -3,82 +3,117 @@ namespace BigPipe; class Pagelet { private $ID = NULL; - private $HTML = NULL; + private $HTML = ""; private $JSCode = ""; - private $CSSFiles = []; private $JSFiles = []; + private $CSSFiles = []; + private $phaseDoneJS = []; private static $count = 0; - public function __construct($priority = 50) { + #==================================================================================================== + # Priorities for sorting the pagelets + #==================================================================================================== + const PRIORITY_HIGHEST = 100; + const PRIORITY_HIGH = 75; + const PRIORITY_NORMAL = 50; + const PRIORITY_LOW = 25; + const PRIORITY_LOWEST = 0; + + #==================================================================================================== + # Callback phase numbers for PhaseDoneJS + #==================================================================================================== + const PHASE_ARRIVE = 0; # After the pagelet reached BigPipe + const PHASE_LOADCSS = 1; # After all the CSS resources have been loaded + const PHASE_PUTHTML = 2; # After the HTML content has been injected into the placeholders + const PHASE_LOADJS = 3; # After all the JS resources have been loaded + const PHASE_EXECJS = 4; # After the static JS code has been executed + + public function __construct($priority = self::PRIORITY_NORMAL) { + $this->phaseDoneJS = array_pad([], 5, []); $this->ID = 'P'.++self::$count; + BigPipe::addPagelet($this, $priority); } #==================================================================================================== - # ID zurückgeben + # Return the unique ID #==================================================================================================== public function getID() { return $this->ID; } #==================================================================================================== - # HTML-Code zurückgeben + # Return the HTML content #==================================================================================================== public function getHTML() { return $this->HTML; } #==================================================================================================== - # CSS-Ressourcen zurückgeben + # Return the CSS resources #==================================================================================================== public function getCSSFiles() { return $this->CSSFiles; } #==================================================================================================== - # JS-Ressourcen zurückgeben + # Return the JS resources #==================================================================================================== public function getJSFiles() { return $this->JSFiles; } #==================================================================================================== - # JS-Code zurückgeben + # Return the main JS code #==================================================================================================== public function getJSCode() { return $this->JSCode; } #==================================================================================================== - # HTML-Code hinzufügen + # Add HTML or attach more #==================================================================================================== public function addHTML($HTML) { - $this->HTML .= $HTML; + return $this->HTML .= $HTML; } #==================================================================================================== - # CSS-Ressource hinzufügen + # Attach a CSS resource #==================================================================================================== public function addCSS($file) { - $this->CSSFiles[] = $file; + return $this->CSSFiles[] = $file; } #==================================================================================================== - # JS-Ressource hinzufügen + # Attach a JS resource #==================================================================================================== public function addJS($file) { - $this->JSFiles[] = $file; + return $this->JSFiles[] = $file; } #==================================================================================================== - # JS-Code hinzufügen + # Add JS code or attach more #==================================================================================================== public function addJSCode($code) { - $this->JSCode .= $code; + return $this->JSCode .= $code; + } + + #==================================================================================================== + # Attach a PhaseDoneJS callback + #==================================================================================================== + public function addPhaseDoneJS($phase, $callback) { + return $this->phaseDoneJS[$phase][] = removeLineBreaksAndTabs($callback); + } + + #==================================================================================================== + # Return all registered PhaseDoneJS callbacks + #==================================================================================================== + public function getPhaseDoneJS() { + return $this->phaseDoneJS; } #==================================================================================================== - # Magische Methode: __toString() + # Magic method: __toString() #==================================================================================================== public function __toString() { return '
'.((!BigPipe::isEnabled()) ? $this->getHTML() : NULL).'
'; diff --git a/include/functions.php b/include/functions.php index 443d594..21eb3f0 100755 --- a/include/functions.php +++ b/include/functions.php @@ -1,6 +1,6 @@