From dc0b1a382b5f980cffe87e9965e659d07107026e Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Sun, 10 Jul 2016 11:24:33 +0200 Subject: Each resource has now an ID; Code improvements and comment blocks added. --- include/classes/BigPipe/BigPipe.php | 33 +++++++---- include/classes/BigPipe/Item.php | 38 +++++++++++++ include/classes/BigPipe/Pagelet.php | 98 +++++++++++--------------------- include/classes/BigPipe/Resource.php | 37 ++++-------- include/classes/BigPipe/Resource/CSS.php | 11 +++- include/classes/BigPipe/Resource/JS.php | 11 +++- static/bigpipe.js | 50 ++++++++-------- 7 files changed, 144 insertions(+), 134 deletions(-) create mode 100644 include/classes/BigPipe/Item.php diff --git a/include/classes/BigPipe/BigPipe.php b/include/classes/BigPipe/BigPipe.php index 082ca14..8d95779 100755 --- a/include/classes/BigPipe/BigPipe.php +++ b/include/classes/BigPipe/BigPipe.php @@ -1,4 +1,13 @@ ] # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# +# # +# 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. # +# # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# namespace BigPipe; class BigPipe { @@ -44,8 +53,8 @@ class BigPipe { if(self::debugging()) { self::addDebugPhaseDoneJS($Pagelet); - array_map('self::addDebugPhaseDoneJS', $Pagelet->getCSSResources()); - array_map('self::addDebugPhaseDoneJS', $Pagelet->getJSResources()); + array_map('self::addDebugPhaseDoneJS', $Pagelet->getResources()[Resource::TYPE_STYLESHEET]); + array_map('self::addDebugPhaseDoneJS', $Pagelet->getResources()[Resource::TYPE_JAVASCRIPT]); usleep(rand(125, 175) * 2000); } @@ -53,20 +62,20 @@ class BigPipe { $stylesheets = []; $javascripts = []; - foreach($Pagelet->getCSSResources() as $Resource) { - $stylesheets[$Resource->getURL()] = $Resource->getPhaseDoneJS(); + foreach($Pagelet->getResources()[Resource::TYPE_STYLESHEET] as $Resource) { + $stylesheets[] = ['ID' => $Resource->getID(), 'HREF' => $Resource->getURL(), 'PHASE' => $Resource->getPhaseDoneJS()]; } - foreach($Pagelet->getJSResources() as $Resource) { - $javascripts[$Resource->getURL()] = $Resource->getPhaseDoneJS(); + foreach($Pagelet->getResources()[Resource::TYPE_JAVASCRIPT] as $Resource) { + $javascripts[] = ['ID' => $Resource->getID(), 'HREF' => $Resource->getURL(), 'PHASE' => $Resource->getPhaseDoneJS()]; } $pageletJSON = [ - 'ID' => $Pagelet->getID(), + 'ID' => $Pagelet->getID(), 'NEED' => $Pagelet->getDependencies(), - 'RSRC' => (object) [ - Resource::TYPE_STYLESHEET => (object) $stylesheets, - Resource::TYPE_JAVASCRIPT => (object) $javascripts, + 'RSRC' => [ + Resource::TYPE_STYLESHEET => $stylesheets, + Resource::TYPE_JAVASCRIPT => $javascripts, ], 'CODE' => removeLineBreaksAndTabs($Pagelet->getJSCode()), 'PHASE' => $Pagelet->getPhaseDoneJS() @@ -105,11 +114,11 @@ class BigPipe { foreach(array_reverse(self::$pagelets) as $priority => $pagelets) { foreach($pagelets as $Pagelet) { if(!self::enabled()) { - foreach($Pagelet->getCSSResources() as $Resource) { + foreach($Pagelet->getResources()[Resource::TYPE_STYLESHEET] as $Resource) { echo $Resource->renderHTML()."\n"; } - foreach($Pagelet->getJSResources() as $Resource) { + foreach($Pagelet->getResources()[Resource::TYPE_JAVASCRIPT] as $Resource) { echo $Resource->renderHTML()."\n"; } diff --git a/include/classes/BigPipe/Item.php b/include/classes/BigPipe/Item.php new file mode 100644 index 0000000..a4c739c --- /dev/null +++ b/include/classes/BigPipe/Item.php @@ -0,0 +1,38 @@ +] # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# +# # +# The item class abstracts the properties and methods that are required by the # +# Pagelet and Resource class both. Each one can have PhaseDoneJS callbacks for # +# several phases numbers which are defined as constants of the specific class. # +# # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# +namespace BigPipe; + +abstract class Item { + protected $ID = ''; + protected $phaseDoneJS = []; + + #=============================================================================== + # Return the unique ID + #=============================================================================== + public function getID() { + return $this->ID; + } + + #=============================================================================== + # Return all registered PhaseDoneJS callbacks + #=============================================================================== + public function getPhaseDoneJS(): array { + return $this->phaseDoneJS; + } + + #=============================================================================== + # Attach a PhaseDoneJS callback + #=============================================================================== + public function addPhaseDoneJS($phase, $callback) { + return $this->phaseDoneJS[$phase][] = removeLineBreaksAndTabs($callback); + } +} +?> \ No newline at end of file diff --git a/include/classes/BigPipe/Pagelet.php b/include/classes/BigPipe/Pagelet.php index 7502a02..c81460f 100755 --- a/include/classes/BigPipe/Pagelet.php +++ b/include/classes/BigPipe/Pagelet.php @@ -1,13 +1,17 @@ ] # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# +# # +# [More information coming soon] # +# # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# namespace BigPipe; -class Pagelet { - private $ID = ''; +class Pagelet extends Item { private $HTML = ''; private $JSCode = []; - private $JSResources = []; - private $CSSResources = []; - private $phaseDoneJS = []; + private $resources = []; private $dependencies = []; private $tagname = 'div'; private static $count = 0; @@ -31,18 +35,13 @@ class Pagelet { const PHASE_DONE = 4; # After the static JS code has been executed public function __construct($customID = NULL, $priority = self::PRIORITY_NORMAL, array $dependencies = []) { - $this->phaseDoneJS = array_pad($this->phaseDoneJS, 5, []); + $this->ID = $customID ?? 'P'.++self::$count; $this->dependencies = $dependencies; - $this->ID = is_string($customID) ? $customID : 'P'.++self::$count; - BigPipe::addPagelet($this, $priority); - } + $this->resources = array_pad($this->resources, 2, []); + $this->phaseDoneJS = array_pad($this->phaseDoneJS, 5, []); - #=============================================================================== - # Return the unique ID - #=============================================================================== - public function getID() { - return $this->ID; + BigPipe::addPagelet($this, $priority); } #=============================================================================== @@ -55,10 +54,24 @@ class Pagelet { #=============================================================================== # Return the main JS code #=============================================================================== - public function getJSCode() { + public function getJSCode(): array { return $this->JSCode; } + #=============================================================================== + # Return attached resources + #=============================================================================== + public function getResources(): array { + return $this->resources; + } + + #=============================================================================== + # Return all display dependencies + #=============================================================================== + public function getDependencies(): array { + return $this->dependencies; + } + #=============================================================================== # Add HTML or attach more #=============================================================================== @@ -70,32 +83,21 @@ class Pagelet { # Add resource #=============================================================================== public function addResource(Resource $Resource): Resource { - switch($Resource->getType()) { - case Resource::TYPE_STYLESHEET: - return $this->CSSResources[] = $Resource; - break; - - case Resource::TYPE_JAVASCRIPT: - return $this->JSResources[] = $Resource; - break; - - default: - return $Resource; - } + return $this->resources[$Resource->getType()][] = $Resource; } #=============================================================================== # Short: Add CSS resource by URL #=============================================================================== public function addCSS($resourceURL): Resource { - return $this->addResource(new Resource\CSS($resourceURL)); + return $this->addResource(new Resource\CSS(NULL, $resourceURL)); } #=============================================================================== # Short: Add JS resource by URL #=============================================================================== public function addJS($resourceURL): Resource { - return $this->addResource(new Resource\JS($resourceURL)); + return $this->addResource(new Resource\JS(NULL, $resourceURL)); } #=============================================================================== @@ -105,41 +107,6 @@ class Pagelet { 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(): array { - return $this->phaseDoneJS; - } - - #=============================================================================== - # Return the attached CSS resources - #=============================================================================== - public function getCSSResources(): array { - return $this->CSSResources; - } - - #=============================================================================== - # Return the attached JS resources - #=============================================================================== - public function getJSResources(): array { - return $this->JSResources; - } - - #=============================================================================== - # Return all display dependencies - #=============================================================================== - public function getDependencies(): array { - return $this->dependencies; - } - #=============================================================================== # Set custom placeholder tagname #=============================================================================== @@ -157,4 +124,5 @@ class Pagelet { return $pageletHTML; } -} \ No newline at end of file +} +?> \ No newline at end of file diff --git a/include/classes/BigPipe/Resource.php b/include/classes/BigPipe/Resource.php index d93ad16..1aaeb35 100755 --- a/include/classes/BigPipe/Resource.php +++ b/include/classes/BigPipe/Resource.php @@ -1,11 +1,16 @@ ] # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# +# # +# [More information coming soon] # +# # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# namespace BigPipe; -abstract class Resource { - private $ID = ''; +abstract class Resource extends Item { private $type = ''; private $resourceURL = ''; - private $phaseDoneJS = []; private static $count = 0; #=============================================================================== @@ -29,18 +34,12 @@ abstract class Resource { #=============================================================================== # Build resource #=============================================================================== - public function __construct($type, $resourceURL) { - $this->phaseDoneJS = array_pad($this->phaseDoneJS, 3, []); - $this->ID = 'R'.++self::$count; + public function __construct($customID = NULL, $type, $resourceURL) { + $this->ID = $customID ?? 'R'.++self::$count; $this->type = $type; $this->resourceURL = $resourceURL; - } - #=============================================================================== - # Return the unique ID - #=============================================================================== - public function getID() { - return $this->ID; + $this->phaseDoneJS = array_pad($this->phaseDoneJS, 3, []); } #=============================================================================== @@ -56,19 +55,5 @@ abstract class Resource { public function getURL() { return $this->resourceURL; } - - #=============================================================================== - # 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; - } } ?> \ No newline at end of file diff --git a/include/classes/BigPipe/Resource/CSS.php b/include/classes/BigPipe/Resource/CSS.php index 71d2244..b02f1cb 100644 --- a/include/classes/BigPipe/Resource/CSS.php +++ b/include/classes/BigPipe/Resource/CSS.php @@ -1,4 +1,11 @@ ] # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# +# # +# [More information coming soon] # +# # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# namespace BigPipe\Resource; class CSS extends \BigPipe\Resource { @@ -6,8 +13,8 @@ class CSS extends \BigPipe\Resource { #=============================================================================== # Build resource #=============================================================================== - public function __construct($resourceURL) { - parent::__construct(parent::TYPE_STYLESHEET, $resourceURL); + public function __construct($customID = NULL, $resourceURL) { + parent::__construct($customID, parent::TYPE_STYLESHEET, $resourceURL); } #=============================================================================== diff --git a/include/classes/BigPipe/Resource/JS.php b/include/classes/BigPipe/Resource/JS.php index f9e109b..e79bd8b 100644 --- a/include/classes/BigPipe/Resource/JS.php +++ b/include/classes/BigPipe/Resource/JS.php @@ -1,4 +1,11 @@ ] # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# +# # +# [More information coming soon] # +# # +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# namespace BigPipe\Resource; class JS extends \BigPipe\Resource { @@ -6,8 +13,8 @@ class JS extends \BigPipe\Resource { #=============================================================================== # Build resource #=============================================================================== - public function __construct($resourceURL) { - parent::__construct(parent::TYPE_JAVASCRIPT, $resourceURL); + public function __construct($customID = NULL, $resourceURL) { + parent::__construct($customID, parent::TYPE_JAVASCRIPT, $resourceURL); } #=============================================================================== diff --git a/static/bigpipe.js b/static/bigpipe.js index 3bf5809..7664d61 100755 --- a/static/bigpipe.js +++ b/static/bigpipe.js @@ -35,15 +35,16 @@ var BigPipe = (function() { //============================================================================== // Resource: Represents a single CSS or JS resource //============================================================================== - function Resource(resourceURL, type, pageletID, phaseDoneJS) { + function Resource(dataJSON, type, pageletID) { + this.ID = dataJSON.ID; + this.HREF = dataJSON.HREF; this.pageletID = pageletID; - this.resourceURL = resourceURL; this.callbacks = []; this.node = false; this.done = false; this.type = type; - this.phaseDoneJS = phaseDoneJS; + this.phaseDoneJS = dataJSON.PHASE; this.phase = 0; PhaseDoneJS.handler(this, Resource.PHASE_INIT); @@ -86,14 +87,14 @@ var BigPipe = (function() { Resource.prototype.execute = function() { switch(this.type) { case Resource.TYPE_STYLESHEET: - this.node = document.createElement('link'); - this.node.setAttribute('rel', 'stylesheet'); - this.node.setAttribute('href', this.resourceURL); + this.node = document.createElement("link"); + this.node.setAttribute("rel", "stylesheet"); + this.node.setAttribute("href", this.HREF); break; case Resource.TYPE_JAVASCRIPT: - this.node = document.createElement('script'); - this.node.setAttribute('src', this.resourceURL); - this.node.setAttribute('async', true); + this.node = document.createElement("script"); + this.node.setAttribute("src", this.HREF); + this.node.setAttribute("async", true); break; default: return false; @@ -129,14 +130,14 @@ var BigPipe = (function() { //============================================================================== // Pagelet: Represents a single pagelet //============================================================================== - function Pagelet(data, HTML) { - this.ID = data.ID; - this.NEED = data.NEED; + function Pagelet(dataJSON, HTML) { + this.ID = dataJSON.ID; + this.NEED = dataJSON.NEED; this.HTML = HTML; - this.JSCode = data.CODE; - this.phaseDoneJS = data.PHASE; - this.stylesheets = data.RSRC[Resource.TYPE_STYLESHEET]; - this.javascripts = data.RSRC[Resource.TYPE_JAVASCRIPT]; + this.JSCode = dataJSON.CODE; + this.phaseDoneJS = dataJSON.PHASE; + this.stylesheets = dataJSON.RSRC[Resource.TYPE_STYLESHEET]; + this.javascripts = dataJSON.RSRC[Resource.TYPE_JAVASCRIPT]; this.phase = 0; this.resources = [[], []]; @@ -157,18 +158,13 @@ var BigPipe = (function() { // Pagelet: Initialize the pagelet resources //============================================================================== Pagelet.prototype.initializeResources = function() { - var resourceURL; - var phaseDoneJS; - - for(resourceURL in this.stylesheets) { - phaseDoneJS = this.stylesheets[resourceURL]; - this.attachResource(new Resource(resourceURL, Resource.TYPE_STYLESHEET, this.ID, phaseDoneJS)); - } + this.stylesheets.forEach(function(data) { + this.attachResource(new Resource(data, Resource.TYPE_STYLESHEET, this.ID)); + }.bind(this)); - for(resourceURL in this.javascripts) { - phaseDoneJS = this.javascripts[resourceURL]; - this.attachResource(new Resource(resourceURL, Resource.TYPE_JAVASCRIPT, this.ID, phaseDoneJS)); - } + this.javascripts.forEach(function(data) { + this.attachResource(new Resource(data, Resource.TYPE_JAVASCRIPT, this.ID)); + }.bind(this)); }; //============================================================================== -- cgit v1.2.3