From bcf23c3ab443567991d602f6d06d87051aa3468b Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Sat, 21 Oct 2017 23:54:41 +0200 Subject: Code optimization: The method "getStructure" has been added to the abstract Item class. --- include/classes/BigPipe/BigPipe.php | 22 +--------------------- include/classes/BigPipe/Item.php | 5 +++++ include/classes/BigPipe/Pagelet.php | 24 ++++++++++++++++++++++++ include/classes/BigPipe/Resource.php | 7 +++++++ 4 files changed, 37 insertions(+), 21 deletions(-) (limited to 'include') diff --git a/include/classes/BigPipe/BigPipe.php b/include/classes/BigPipe/BigPipe.php index 2989a75..ab8c8f7 100755 --- a/include/classes/BigPipe/BigPipe.php +++ b/include/classes/BigPipe/BigPipe.php @@ -64,27 +64,7 @@ class BigPipe { usleep(rand(125, 175) * 2000); } - $stylesheets = []; - $javascripts = []; - - foreach($Pagelet->getResources()[Resource::TYPE_STYLESHEET] as $Resource) { - $stylesheets[] = ['ID' => $Resource->getID(), 'HREF' => $Resource->getURL(), 'PHASE' => $Resource->getPhaseDoneJS()]; - } - - foreach($Pagelet->getResources()[Resource::TYPE_JAVASCRIPT] as $Resource) { - $javascripts[] = ['ID' => $Resource->getID(), 'HREF' => $Resource->getURL(), 'PHASE' => $Resource->getPhaseDoneJS()]; - } - - $pageletJSON = [ - 'ID' => $Pagelet->getID(), - 'NEED' => $Pagelet->getDependencies(), - 'RSRC' => [ - Resource::TYPE_STYLESHEET => $stylesheets, - Resource::TYPE_JAVASCRIPT => $javascripts, - ], - 'CODE' => removeLineBreaksAndTabs($Pagelet->getJSCode()), - 'PHASE' => $Pagelet->getPhaseDoneJS() - ]; + $pageletJSON = $Pagelet->getStructure(); if($last) { $pageletJSON['IS_LAST'] = TRUE; diff --git a/include/classes/BigPipe/Item.php b/include/classes/BigPipe/Item.php index 5edf9c8..9c66eb9 100644 --- a/include/classes/BigPipe/Item.php +++ b/include/classes/BigPipe/Item.php @@ -14,6 +14,11 @@ abstract class Item { protected $ID = ''; protected $phaseDoneJS = []; + #=============================================================================== + # Required methods in child classes + #=============================================================================== + abstract public function getStructure(): array; + #=============================================================================== # Return the unique ID #=============================================================================== diff --git a/include/classes/BigPipe/Pagelet.php b/include/classes/BigPipe/Pagelet.php index 664883a..ae465ab 100755 --- a/include/classes/BigPipe/Pagelet.php +++ b/include/classes/BigPipe/Pagelet.php @@ -123,6 +123,30 @@ class Pagelet extends Item { return $this->tagHTML = $HTML; } + #=============================================================================== + # Return the pagelet structure + #=============================================================================== + public function getStructure(): array { + foreach($this->getResources()[Resource::TYPE_STYLESHEET] as $Resource) { + $stylesheets[] = $Resource->getStructure(); + } + + foreach($this->getResources()[Resource::TYPE_JAVASCRIPT] as $Resource) { + $javascripts[] = $Resource->getStructure(); + } + + return [ + 'ID' => $this->getID(), + 'NEED' => $this->getDependencies(), + 'RSRC' => [ + Resource::TYPE_STYLESHEET => $stylesheets ?? [], + Resource::TYPE_JAVASCRIPT => $javascripts ?? [] + ], + 'CODE' => removeLineBreaksAndTabs($this->getJSCode()), + 'PHASE' => $this->getPhaseDoneJS() + ]; + } + #=============================================================================== # Magic method: __toString() #=============================================================================== diff --git a/include/classes/BigPipe/Resource.php b/include/classes/BigPipe/Resource.php index 438b83c..4711d48 100755 --- a/include/classes/BigPipe/Resource.php +++ b/include/classes/BigPipe/Resource.php @@ -55,5 +55,12 @@ abstract class Resource extends Item { public function getURL() { return $this->resourceURL; } + + #=============================================================================== + # Return the resource structure + #=============================================================================== + public function getStructure(): array { + return ['ID' => $this->getID(), 'HREF' => $this->getURL(), 'PHASE' => $this->getPhaseDoneJS()]; + } } ?> \ No newline at end of file -- cgit v1.2.3