From 58dbefb69068e074586be4ade67871a81c853233 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Wed, 4 Oct 2017 18:00:12 +0200 Subject: The method "addPagelet" from the BigPipe main class was replaced by the new implemented methods "enqueue" and "dequeue" for the new queueing mechanism. The "dequeue" method is currently unused but will be used in the future. The changes in this commit are partially preparations for further commits. --- include/classes/BigPipe/BigPipe.php | 35 ++++++++++++++++++++++++----------- include/classes/BigPipe/Pagelet.php | 2 +- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/include/classes/BigPipe/BigPipe.php b/include/classes/BigPipe/BigPipe.php index 9499f2e..2989a75 100755 --- a/include/classes/BigPipe/BigPipe.php +++ b/include/classes/BigPipe/BigPipe.php @@ -14,7 +14,6 @@ class BigPipe { private static $debugging = FALSE; private static $enabled = TRUE; private static $pagelets = []; - private static $count = 0; #=============================================================================== # Enable or disable the pipeline mode @@ -39,11 +38,17 @@ class BigPipe { } #=============================================================================== - # Add a new pagelet to pipeline + # Insert pagelet into queue #=============================================================================== - public static function addPagelet(Pagelet $Pagelet, $priority) { - self::$pagelets[$priority][] = $Pagelet; - return ++self::$count; + public static function enqueue(Pagelet $Pagelet) { + self::$pagelets[spl_object_hash($Pagelet)] = $Pagelet; + } + + #=============================================================================== + # Remove pagelet from queue + #=============================================================================== + public static function dequeue(Pagelet $Pagelet) { + unset(self::$pagelets[spl_object_hash($Pagelet)]); } #=============================================================================== @@ -102,24 +107,32 @@ class BigPipe { } #=============================================================================== - # Render the pagelets + # Renders all remaining pagelets from the queue in the appropriate order #=============================================================================== public static function render() { self::flushOutputBuffer(); $i = 0; - ksort(self::$pagelets); + $pagelets_ordered = []; + + foreach(self::$pagelets as $Pagelet) { + $pagelets_ordered[$Pagelet->getPriority()][] = $Pagelet; + } + + krsort($pagelets_ordered); + + if(!empty($pagelets_ordered)) { + $pagelets = call_user_func_array('array_merge', $pagelets_ordered); - foreach(array_reverse(self::$pagelets) as $priority => $pagelets) { foreach($pagelets as $Pagelet) { if(!self::enabled()) { foreach($Pagelet->getResources()[Resource::TYPE_STYLESHEET] as $Resource) { - echo $Resource->renderHTML()."\n"; + echo "{$Resource->renderHTML()}\n"; } foreach($Pagelet->getResources()[Resource::TYPE_JAVASCRIPT] as $Resource) { - echo $Resource->renderHTML()."\n"; + echo "{$Resource->renderHTML()}\n"; } foreach($Pagelet->getJSCode() as $JSCode) { @@ -128,7 +141,7 @@ class BigPipe { } else { - self::singleResponse($Pagelet, (self::$count === ++$i)); + self::singleResponse($Pagelet, (count(self::$pagelets) === ++$i)); self::flushOutputBuffer(); } } diff --git a/include/classes/BigPipe/Pagelet.php b/include/classes/BigPipe/Pagelet.php index 484a6e2..bdab651 100755 --- a/include/classes/BigPipe/Pagelet.php +++ b/include/classes/BigPipe/Pagelet.php @@ -43,7 +43,7 @@ class Pagelet extends Item { $this->resources = array_pad($this->resources, 2, []); $this->phaseDoneJS = array_pad($this->phaseDoneJS, 5, []); - BigPipe::addPagelet($this, $priority); + BigPipe::enqueue($this); } #=============================================================================== -- cgit v1.2.3