diff options
author | Thomas Lange <code@nerdmind.de> | 2015-12-26 16:37:25 +0100 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2015-12-26 16:37:25 +0100 |
commit | 6658708bdd775a6ab1352e0c36b4c7deafc52e84 (patch) | |
tree | e95ef6b6fb67e0d1ca42221e94631fa6123b6f22 /include/classes | |
parent | 903100ecb99ab926d4ac3be4eba11ea1c9800e12 (diff) | |
download | bigpipe-6658708bdd775a6ab1352e0c36b4c7deafc52e84.tar.gz bigpipe-6658708bdd775a6ab1352e0c36b4c7deafc52e84.tar.xz bigpipe-6658708bdd775a6ab1352e0c36b4c7deafc52e84.zip |
Multiple updates; new feature PhaseDoneJS
Diffstat (limited to 'include/classes')
-rwxr-xr-x | include/classes/BigPipe/BigPipe.php | 42 | ||||
-rwxr-xr-x | include/classes/BigPipe/DemoPagelet.php | 17 | ||||
-rwxr-xr-x | include/classes/BigPipe/Pagelet.php | 69 |
3 files changed, 97 insertions, 31 deletions
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 '<code class="hidden" id="_'.$data['ID'].'"><!-- '.str_replace('--', '--', removeLineBreaksAndTabs($Pagelet->getHTML())).' --></code>'."\n"; - echo '<script>BigPipe.onPageletArrive('.json_encode($data).($async ? ', document.getElementById("_'.$Pagelet->getID().'").innerHTML' : NULL).');</script>'."\n\n"; + $pageletHTML = str_replace('--', '--', removeLineBreaksAndTabs($Pagelet->getHTML())); + $pageletJSON = json_encode($data, (self::$debug ? JSON_PRETTY_PRINT : FALSE)); + + echo "<code class=\"hidden\" id=\"_{$Pagelet->getID()}\"><!-- {$pageletHTML} --></code>\n"; + echo "<script>BigPipe.onPageletArrive({$pageletJSON});</script>\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 @@ +<?php +namespace BigPipe; + +class DemoPagelet extends Pagelet { + + public function __construct($priority = Pagelet::PRIORITY_NORMAL) { + parent::__construct($priority); + + $message = '%s: PhaseDoneJS for phase %s'; + + $this->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 '<div id="'.$this->getID().'">'.((!BigPipe::isEnabled()) ? $this->getHTML() : NULL).'</div>'; |