aboutsummaryrefslogtreecommitdiffstats
path: root/include/classes
diff options
context:
space:
mode:
Diffstat (limited to 'include/classes')
-rwxr-xr-xinclude/classes/BigPipe/BigPipe.php105
-rwxr-xr-xinclude/classes/BigPipe/DemoPagelet.php17
-rwxr-xr-xinclude/classes/BigPipe/Pagelet.php74
-rwxr-xr-xinclude/classes/BigPipe/Resource.php74
-rw-r--r--include/classes/BigPipe/Resource/CSS.php20
-rw-r--r--include/classes/BigPipe/Resource/JS.php20
6 files changed, 243 insertions, 67 deletions
diff --git a/include/classes/BigPipe/BigPipe.php b/include/classes/BigPipe/BigPipe.php
index 1af4d8a..082ca14 100755
--- a/include/classes/BigPipe/BigPipe.php
+++ b/include/classes/BigPipe/BigPipe.php
@@ -2,23 +2,31 @@
namespace BigPipe;
class BigPipe {
- private static $enabled = TRUE;
- private static $debug = TRUE;
- private static $pagelets = [];
- private static $count = 0;
+ private static $debugging = FALSE;
+ private static $enabled = TRUE;
+ private static $pagelets = [];
+ private static $count = 0;
#===============================================================================
- # Return TRUE if the pipeline is enabled
+ # Enable or disable the pipeline mode
#===============================================================================
- public static function isEnabled() {
+ public static function enabled($change = NULL) {
+ if($change !== NULL) {
+ self::$enabled = (bool) $change;
+ }
+
return self::$enabled;
}
#===============================================================================
- # Enable or disable the pipeline mode
+ # Return if debugging is enabled or change
#===============================================================================
- public static function enablePipeline($enabled = TRUE) {
- return self::$enabled = (bool) $enabled;
+ public static function debugging($change = NULL): bool {
+ if($change !== NULL) {
+ self::$debugging = (bool) $change;
+ }
+
+ return self::$debugging;
}
#===============================================================================
@@ -33,20 +41,45 @@ class BigPipe {
# Prints a single pagelet response
#===============================================================================
private static function singleResponse(Pagelet $Pagelet, $last = FALSE) {
+ if(self::debugging()) {
+ self::addDebugPhaseDoneJS($Pagelet);
+
+ array_map('self::addDebugPhaseDoneJS', $Pagelet->getCSSResources());
+ array_map('self::addDebugPhaseDoneJS', $Pagelet->getJSResources());
+
+ usleep(rand(125, 175) * 2000);
+ }
+
+ $stylesheets = [];
+ $javascripts = [];
+
+ foreach($Pagelet->getCSSResources() as $Resource) {
+ $stylesheets[$Resource->getURL()] = $Resource->getPhaseDoneJS();
+ }
+
+ foreach($Pagelet->getJSResources() as $Resource) {
+ $javascripts[$Resource->getURL()] = $Resource->getPhaseDoneJS();
+ }
+
$pageletJSON = [
- 'ID' => $Pagelet->getID(), 'NEED' => $Pagelet->getDependencies(),
- 'RESOURCES' => ['CSS' => $Pagelet->getCSSFiles(), 'JS' => $Pagelet->getJSFiles(), 'JS_CODE' => removeLineBreaksAndTabs($Pagelet->getJSCode())],
- 'PHASES' => (object) $Pagelet->getPhaseDoneJS()
+ 'ID' => $Pagelet->getID(),
+ 'NEED' => $Pagelet->getDependencies(),
+ 'RSRC' => (object) [
+ Resource::TYPE_STYLESHEET => (object) $stylesheets,
+ Resource::TYPE_JAVASCRIPT => (object) $javascripts,
+ ],
+ 'CODE' => removeLineBreaksAndTabs($Pagelet->getJSCode()),
+ 'PHASE' => $Pagelet->getPhaseDoneJS()
];
if($last) {
- $pageletJSON['IS_LAST'] = true;
+ $pageletJSON['IS_LAST'] = TRUE;
}
$pageletHTML = removeLineBreaksAndTabs($Pagelet->getHTML());
$pageletHTML = str_replace('--', '--', $pageletHTML);
- $pageletJSON = json_encode($pageletJSON, (self::$debug ? JSON_PRETTY_PRINT : NULL));
+ $pageletJSON = json_encode($pageletJSON, (self::debugging() ? JSON_PRETTY_PRINT : NULL));
echo "<code class=\"hidden\" id=\"_{$Pagelet->getID()}\"><!-- {$pageletHTML} --></code>\n";
echo "<script>BigPipe.onPageletArrive({$pageletJSON}, document.getElementById(\"_{$Pagelet->getID()}\"));</script>\n\n";
@@ -71,13 +104,13 @@ class BigPipe {
foreach(array_reverse(self::$pagelets) as $priority => $pagelets) {
foreach($pagelets as $Pagelet) {
- if(!self::isEnabled()) {
- foreach($Pagelet->getCSSFiles() as $CSSFile) {
- echo "<link href=\"{$CSSFile}\" rel=\"stylesheet\" />\n";
+ if(!self::enabled()) {
+ foreach($Pagelet->getCSSResources() as $Resource) {
+ echo $Resource->renderHTML()."\n";
}
- foreach($Pagelet->getJSFiles() as $JSFile) {
- echo "<script src=\"{$JSFile}\"></script>\n";
+ foreach($Pagelet->getJSResources() as $Resource) {
+ echo $Resource->renderHTML()."\n";
}
foreach($Pagelet->getJSCode() as $JSCode) {
@@ -88,10 +121,38 @@ class BigPipe {
else {
self::singleResponse($Pagelet, (self::$count === ++$i));
self::flushOutputBuffer();
-
- self::$debug AND usleep((rand(250, 1000) * 1000));
}
}
}
}
-} \ No newline at end of file
+
+ #===============================================================================
+ # Add PhaseDoneJS for debugging Pagelet and Resource
+ #===============================================================================
+ private static function addDebugPhaseDoneJS($Instance) {
+ $objpath = str_replace('\\', '|', get_class($Instance));
+
+ if($Instance instanceof Pagelet) {
+ $message = "console.log(\"%%c[{$objpath}]%%c#(%%c%s%%c): PhaseDoneJS for phase: %s\", \"font-weight:bold\", \"color:#666\", \"color:#008B45\", \"color:#666\")";
+
+ $Instance->addPhaseDoneJS($Instance::PHASE_INIT, sprintf($message, $Instance->getID(), 'INIT'));
+ $Instance->addPhaseDoneJS($Instance::PHASE_LOADCSS, sprintf($message, $Instance->getID(), 'LOADCSS'));
+ $Instance->addPhaseDoneJS($Instance::PHASE_HTML, sprintf($message, $Instance->getID(), 'HTML'));
+ $Instance->addPhaseDoneJS($Instance::PHASE_LOADJS, sprintf($message, $Instance->getID(), 'LOADJS'));
+ $Instance->addPhaseDoneJS($Instance::PHASE_DONE, sprintf($message, $Instance->getID(), 'DONE'));
+
+ return $Instance;
+ }
+
+ if($Instance instanceof Resource) {
+ $message = "console.log(\"[{$objpath}]%%c#(%%c%s%%c): PhaseDoneJS for phase: %s\", \"color:#666\", \"color:#008B45\", \"color:#666\")";
+
+ $Instance->addPhaseDoneJS($Instance::PHASE_INIT, sprintf($message, $Instance->getID(), 'INIT'));
+ $Instance->addPhaseDoneJS($Instance::PHASE_LOAD, sprintf($message, $Instance->getID(), 'LOAD'));
+ $Instance->addPhaseDoneJS($Instance::PHASE_DONE, sprintf($message, $Instance->getID(), 'DONE'));
+
+ return $Instance;
+ }
+ }
+}
+?> \ No newline at end of file
diff --git a/include/classes/BigPipe/DemoPagelet.php b/include/classes/BigPipe/DemoPagelet.php
deleted file mode 100755
index adb9f89..0000000
--- a/include/classes/BigPipe/DemoPagelet.php
+++ /dev/null
@@ -1,17 +0,0 @@
-<?php
-namespace BigPipe;
-
-class DemoPagelet extends Pagelet {
-
- public function __construct($customID = NULL, $priority = Pagelet::PRIORITY_NORMAL, array $dependencies = []) {
- parent::__construct($customID, $priority, $dependencies);
-
- $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 436e12a..7502a02 100755
--- a/include/classes/BigPipe/Pagelet.php
+++ b/include/classes/BigPipe/Pagelet.php
@@ -2,11 +2,11 @@
namespace BigPipe;
class Pagelet {
- private $ID = NULL;
+ private $ID = '';
private $HTML = '';
private $JSCode = [];
- private $JSFiles = [];
- private $CSSFiles = [];
+ private $JSResources = [];
+ private $CSSResources = [];
private $phaseDoneJS = [];
private $dependencies = [];
private $tagname = 'div';
@@ -24,11 +24,11 @@ class Pagelet {
#===============================================================================
# Phase numbers for PhaseDoneJS
#===============================================================================
- const PHASE_ARRIVE = 0; # After the pagelet reached BigPipe
+ const PHASE_INIT = 0; # After the pagelet object was initialized
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_HTML = 2; # After the placeholder HTML was replaced
const PHASE_LOADJS = 3; # After all the JS resources have been loaded
- const PHASE_EXECJS = 4; # After the static JS code has been executed
+ 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, []);
@@ -53,20 +53,6 @@ class Pagelet {
}
#===============================================================================
- # Return the CSS resources
- #===============================================================================
- public function getCSSFiles() {
- return $this->CSSFiles;
- }
-
- #===============================================================================
- # Return the JS resources
- #===============================================================================
- public function getJSFiles() {
- return $this->JSFiles;
- }
-
- #===============================================================================
# Return the main JS code
#===============================================================================
public function getJSCode() {
@@ -81,17 +67,35 @@ class Pagelet {
}
#===============================================================================
- # Attach a CSS resource
+ # 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;
+ }
+ }
+
+ #===============================================================================
+ # Short: Add CSS resource by URL
#===============================================================================
- public function addCSS($href) {
- return $this->CSSFiles[] = $href;
+ public function addCSS($resourceURL): Resource {
+ return $this->addResource(new Resource\CSS($resourceURL));
}
#===============================================================================
- # Attach a JS resource
+ # Short: Add JS resource by URL
#===============================================================================
- public function addJS($href) {
- return $this->JSFiles[] = $href;
+ public function addJS($resourceURL): Resource {
+ return $this->addResource(new Resource\JS($resourceURL));
}
#===============================================================================
@@ -111,11 +115,25 @@ class Pagelet {
#===============================================================================
# Return all registered PhaseDoneJS callbacks
#===============================================================================
- public function getPhaseDoneJS() {
+ 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 {
@@ -134,7 +152,7 @@ class Pagelet {
#===============================================================================
public function __toString() {
$pageletHTML = "<{$this->tagname} id=\"{$this->getID()}\">";
- $pageletHTML .= !BigPipe::isEnabled() ? $this->getHTML() : NULL;
+ $pageletHTML .= !BigPipe::enabled() ? $this->getHTML() : NULL;
$pageletHTML .= "</{$this->tagname}>";
return $pageletHTML;
diff --git a/include/classes/BigPipe/Resource.php b/include/classes/BigPipe/Resource.php
new file mode 100755
index 0000000..d93ad16
--- /dev/null
+++ b/include/classes/BigPipe/Resource.php
@@ -0,0 +1,74 @@
+<?php
+namespace BigPipe;
+
+abstract class Resource {
+ private $ID = '';
+ private $type = '';
+ private $resourceURL = '';
+ private $phaseDoneJS = [];
+ private static $count = 0;
+
+ #===============================================================================
+ # Render resource HTML for disabled pipeline
+ #===============================================================================
+ abstract public function renderHTML();
+
+ #===============================================================================
+ # Resource types
+ #===============================================================================
+ const TYPE_STYLESHEET = 0;
+ const TYPE_JAVASCRIPT = 1;
+
+ #===============================================================================
+ # Phase numbers for PhaseDoneJS
+ #===============================================================================
+ const PHASE_INIT = 0; # Resource object has been initialized
+ const PHASE_LOAD = 1; # Loading of resource has been started
+ const PHASE_DONE = 2; # Loading of resource is done.
+
+ #===============================================================================
+ # Build resource
+ #===============================================================================
+ public function __construct($type, $resourceURL) {
+ $this->phaseDoneJS = array_pad($this->phaseDoneJS, 3, []);
+ $this->ID = 'R'.++self::$count;
+ $this->type = $type;
+ $this->resourceURL = $resourceURL;
+ }
+
+ #===============================================================================
+ # Return the unique ID
+ #===============================================================================
+ public function getID() {
+ return $this->ID;
+ }
+
+ #===============================================================================
+ # Return the resource type
+ #===============================================================================
+ public function getType() {
+ return $this->type;
+ }
+
+ #===============================================================================
+ # Return the resource URL
+ #===============================================================================
+ 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
new file mode 100644
index 0000000..71d2244
--- /dev/null
+++ b/include/classes/BigPipe/Resource/CSS.php
@@ -0,0 +1,20 @@
+<?php
+namespace BigPipe\Resource;
+
+class CSS extends \BigPipe\Resource {
+
+ #===============================================================================
+ # Build resource
+ #===============================================================================
+ public function __construct($resourceURL) {
+ parent::__construct(parent::TYPE_STYLESHEET, $resourceURL);
+ }
+
+ #===============================================================================
+ # Render resource HTML
+ #===============================================================================
+ public function renderHTML() {
+ return sprintf('<link data-id="%s" href="%s" rel="stylesheet" />', $this->getID(), $this->getURL());
+ }
+}
+?> \ No newline at end of file
diff --git a/include/classes/BigPipe/Resource/JS.php b/include/classes/BigPipe/Resource/JS.php
new file mode 100644
index 0000000..f9e109b
--- /dev/null
+++ b/include/classes/BigPipe/Resource/JS.php
@@ -0,0 +1,20 @@
+<?php
+namespace BigPipe\Resource;
+
+class JS extends \BigPipe\Resource {
+
+ #===============================================================================
+ # Build resource
+ #===============================================================================
+ public function __construct($resourceURL) {
+ parent::__construct(parent::TYPE_JAVASCRIPT, $resourceURL);
+ }
+
+ #===============================================================================
+ # Render resource HTML
+ #===============================================================================
+ public function renderHTML() {
+ return sprintf('<script data-id="%s" src="%s"></script>', $this->getID(), $this->getURL());
+ }
+}
+?> \ No newline at end of file