aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2016-07-10 11:24:33 +0200
committerThomas Lange <code@nerdmind.de>2016-07-10 11:24:33 +0200
commitdc0b1a382b5f980cffe87e9965e659d07107026e (patch)
treed05bd1dc5a7140caf40bb19483c93e0fe0a757f4
parent28f2ba2a44ca932e16ba6791a07ed42b62fe6803 (diff)
downloadbigpipe-dc0b1a382b5f980cffe87e9965e659d07107026e.tar.gz
bigpipe-dc0b1a382b5f980cffe87e9965e659d07107026e.tar.xz
bigpipe-dc0b1a382b5f980cffe87e9965e659d07107026e.zip
Each resource has now an ID; Code improvements and comment blocks added.
-rwxr-xr-xinclude/classes/BigPipe/BigPipe.php33
-rw-r--r--include/classes/BigPipe/Item.php38
-rwxr-xr-xinclude/classes/BigPipe/Pagelet.php98
-rwxr-xr-xinclude/classes/BigPipe/Resource.php37
-rw-r--r--include/classes/BigPipe/Resource/CSS.php11
-rw-r--r--include/classes/BigPipe/Resource/JS.php11
-rwxr-xr-xstatic/bigpipe.js50
7 files changed, 144 insertions, 134 deletions
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 @@
<?php
+#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
+# BigPipe main class [Thomas Lange <tl@nerdmind.de>] #
+#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
+# #
+# 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 @@
+<?php
+#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
+# Abstract item class [Thomas Lange <tl@nerdmind.de>] #
+#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
+# #
+# 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 @@
<?php
+#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
+# Pagelet representation class [Thomas Lange <tl@nerdmind.de>] #
+#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
+# #
+# [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,11 +54,25 @@ 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
#===============================================================================
public function addHTML($HTML) {
@@ -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));
}
#===============================================================================
@@ -106,41 +108,6 @@ class Pagelet {
}
#===============================================================================
- # 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
#===============================================================================
public function setTagname($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 @@
<?php
+#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
+# Abstract Resource representation class [Thomas Lange <tl@nerdmind.de>] #
+#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
+# #
+# [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 @@
<?php
+#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
+# Resource representation class [CSS] [Thomas Lange <tl@nerdmind.de>] #
+#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
+# #
+# [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 @@
<?php
+#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
+# Resource representation class [JS] [Thomas Lange <tl@nerdmind.de>] #
+#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
+# #
+# [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));
};
//==============================================================================