diff options
Diffstat (limited to 'include/classes/BigPipe/BigPipe.php')
-rwxr-xr-x | include/classes/BigPipe/BigPipe.php | 105 |
1 files changed, 83 insertions, 22 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 |