From 19c0af6809b8db0b763565fa9b273c90da3d1894 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Sat, 22 Oct 2016 01:08:22 +0200 Subject: The Pagelet representation class in PHP now has a separate method to add dependencies (either with the instance of the dependency Pagelet or the unique ID as string as argument). --- include/classes/BigPipe/BigPipe.php | 2 +- include/classes/BigPipe/Pagelet.php | 16 +++++++++++++--- include/pagelets.php | 9 +++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/classes/BigPipe/BigPipe.php b/include/classes/BigPipe/BigPipe.php index 16df618..098e071 100755 --- a/include/classes/BigPipe/BigPipe.php +++ b/include/classes/BigPipe/BigPipe.php @@ -5,7 +5,7 @@ # # # The BigPipe main class is responsible for sorting and rendering the pagelets # # and their associated resources. This class also provides methods to turn off # -# the pipelining mode or turn on the debugging mode. # +# the pipeline mode or turn on the debugging mode. # # # #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# namespace BigPipe; diff --git a/include/classes/BigPipe/Pagelet.php b/include/classes/BigPipe/Pagelet.php index 0df117c..df74876 100755 --- a/include/classes/BigPipe/Pagelet.php +++ b/include/classes/BigPipe/Pagelet.php @@ -34,9 +34,8 @@ class Pagelet extends Item { const PHASE_LOADJS = 3; # After all the JS resources have been loaded const PHASE_DONE = 4; # After the static JS code has been executed - public function __construct($customID = NULL, $priority = self::PRIORITY_NORMAL, array $dependencies = []) { + public function __construct($customID = NULL, $priority = self::PRIORITY_NORMAL) { $this->ID = $customID ?? 'P'.++self::$count; - $this->dependencies = $dependencies; $this->resources = array_pad($this->resources, 2, []); $this->phaseDoneJS = array_pad($this->phaseDoneJS, 5, []); @@ -69,7 +68,7 @@ class Pagelet extends Item { # Return all display dependencies #=============================================================================== public function getDependencies(): array { - return $this->dependencies; + return array_unique($this->dependencies); } #=============================================================================== @@ -107,6 +106,17 @@ class Pagelet extends Item { return $this->JSCode[] = $code; } + #=============================================================================== + # Attach a display dependency + #=============================================================================== + public function addDependency($Pagelet) { + if($Pagelet instanceof Pagelet) { + return $this->dependencies[] = $Pagelet->getID(); + } + + return $this->dependencies[] = $Pagelet; + } + #=============================================================================== # Set custom placeholder tagname #=============================================================================== diff --git a/include/pagelets.php b/include/pagelets.php index 9725879..4a86d49 100644 --- a/include/pagelets.php +++ b/include/pagelets.php @@ -33,7 +33,7 @@ $PageletGreen = new BigPipe\Pagelet('greenPL'); #=============================================================================== # Pagelet within $PageletGreen #=============================================================================== - // The third parameter is required to ensure that the $InnerPagelet will only be + // The addDependency call is required to ensure that $InnerPagelet will only be // executed if the HTML from the $PageletGreen has ALREADY DISPLAYED. Otherwise, // $InnerPagelet would not find his placeholder tag which is defined WITHIN the // HTML on $PageletGreen. Of course, you can still add other pagelets as @@ -44,7 +44,12 @@ $PageletGreen = new BigPipe\Pagelet('greenPL'); // the first which arrives, but it will first be displayed if his dependency // pagelets are already displayed. - $InnerPagelet = new BigPipe\Pagelet('innerPL', BigPipe\Pagelet::PRIORITY_HIGHEST, [$PageletGreen->getID()]); + $InnerPagelet = new BigPipe\Pagelet('innerPL', BigPipe\Pagelet::PRIORITY_HIGHEST); + + // NOTICE: You can also use the Pagelet ID (as string) as argument. May be helpful + // if a dependency Pagelet object is not accessible within the current scope. + $InnerPagelet->addDependency($PageletGreen); + $InnerPagelet->addHTML('
Inner Pagelet \(o_o)/
'); } -- cgit v1.2.3