From 21824df56bd13d81f10ac0b86c5cad31def51f3e Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Sat, 25 Jun 2016 15:34:34 +0200 Subject: Update with new features and code improvements. + New classes have been added: "BigPipe\Resource", "BigPipe\Resource\CSS", "BigPipe\Resource\JS". If you wan't to add a resource to a Pagelet, you can now built a new resource object with the "CSS" or "JS" class (the "Resource" class is abstract and the parent class of "CSS" and "JS") and can add PhaseDoneJS callbacks for this resources with "Resource::addPhaseDoneJS()" similar to "Pagelet::addPhaseDoneJS()". To add the resource to your pagelet: "$Pagelet->addResource($Resource)". Of course, you can still use the two SHORT methods "Pagelet::addCSS()" and "Pagelet::addJS()" which needs only one parameter with the resource URL. + Each resource has now PhaseDoneJS callbacks for 3 phases (Object initializied [INIT], Loading started [LOAD], Loading complete [DONE]). + Check your usage of the PhaseDoneJS callback constants of the Pagelet class: These constants have been renamed. + Debugging mode: Just set BigPipe::debugging(TRUE) and each pagelet and resource will be pass through a function which adds PhaseDoneJS callbacks with debug informations (Look now at the Javascript console and enjoy the beautiful colors which makes the debug informations better readable for your eyes. \(o_o)/). + Several code improvements on almost all files. --- include/classes/BigPipe/BigPipe.php | 105 ++++++++++++++++++++++++++++-------- 1 file changed, 83 insertions(+), 22 deletions(-) (limited to 'include/classes/BigPipe/BigPipe.php') 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 "getID()}\">\n"; echo "\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 "\n"; + if(!self::enabled()) { + foreach($Pagelet->getCSSResources() as $Resource) { + echo $Resource->renderHTML()."\n"; } - foreach($Pagelet->getJSFiles() as $JSFile) { - echo "\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 -- cgit v1.2.3