From bc28df7caae90d3dc0ce59c3001cdbad898aaa7f Mon Sep 17 00:00:00 2001 From: Renon Stewart Date: Wed, 29 Aug 2018 16:02:41 -0400 Subject: [PATCH] Issues #4 - Code refactor --- Model/Config/Source/Sections.php | 89 +++++++++++++++++++++----------- composer.json | 2 +- 2 files changed, 59 insertions(+), 32 deletions(-) diff --git a/Model/Config/Source/Sections.php b/Model/Config/Source/Sections.php index c7e63cd..ecb6a57 100644 --- a/Model/Config/Source/Sections.php +++ b/Model/Config/Source/Sections.php @@ -3,7 +3,7 @@ * Copyright © MagePal LLC. All rights reserved. * See COPYING.txt for license details. * http://www.magepal.com | support@magepal.com -*/ + */ namespace MagePal\CustomerAccountLinksManager\Model\Config\Source; @@ -14,6 +14,8 @@ class Sections implements \Magento\Framework\Option\ArrayInterface protected $links; + protected $list = []; + public function __construct( \Magento\Framework\App\Utility\Files $utilityFiles, \Magento\Framework\View\Element\Html\Links $links @@ -36,47 +38,72 @@ public function getSections() { $fileList = $this->utilityFiles->getLayoutFiles(['area_name' => 'frontend'], false); - $list = []; - foreach ($fileList as $configFile) { if (strpos($configFile, 'customer_account.xml') !== false) { $configXml = simplexml_load_file($configFile); + $this->processXmlElement($configXml); + } + } + + return $this->list; + } - if ($referenceBlocks = $configXml->body->referenceBlock) { - foreach ($referenceBlocks as $referenceBlock) { - if (!empty($referenceBlock->xpath('block/arguments/argument[@name="label"]'))) { - $list[(string) $referenceBlock->block['name']] = [ - 'value' => (string) $referenceBlock->block['name'], - 'label' => (string) $referenceBlock->xpath('block/arguments/argument[@name="label"]')[0] - ]; - } + /** + * @param $configXml + */ + protected function processXmlElement($configXml) + { + if ($referenceBlocks = $configXml->body->referenceBlock) { + foreach ($referenceBlocks as $referenceBlock) { + if (!empty($referenceBlock->xpath('block/arguments/argument[@name="label"]'))) { + $this->updateReferenceBlockList($referenceBlock); + } + } + } + + if (isset($configXml->body->referenceContainer) && isset($configXml->body->referenceContainer->block)) { + if (isset($configXml->body->referenceContainer->block->block) + && isset($configXml->body->referenceContainer->block->block->block) + ) { + $referenceContainerBlocks = $configXml->body->referenceContainer->block->block->block; + + for ($count = 0; $count < count($referenceContainerBlocks); $count++) { + if (!empty($referenceContainerBlocks[$count]->xpath('arguments/argument[@name="label"]'))) { + $this->updateReferenceContainerList($referenceContainerBlocks, $count); } } + } elseif (isset($configXml->body->referenceContainer->block->block)) { + $referenceContainerBlocks = $configXml->body->referenceContainer->block->block; - if (isset($configXml->body->referenceContainer)) { - if ($referenceContainerBlocks = $configXml->body->referenceContainer->block->block->block) { - for ($count = 0; $count < count($referenceContainerBlocks); $count++) { - if (!empty($referenceContainerBlocks[$count]->xpath('arguments/argument[@name="label"]'))) { - $list[(string) $referenceContainerBlocks[$count]['name']] = [ - 'value' => (string) $referenceContainerBlocks[$count]['name'], - 'label' => (string) $referenceContainerBlocks[$count]->xpath('arguments/argument[@name="label"]')[0] - ]; - } - } - } elseif ($referenceContainerBlocks = $configXml->body->referenceContainer->block->block) { - for ($count = 0; $count < count($referenceContainerBlocks); $count++) { - if (!empty($referenceContainerBlocks[$count]->xpath('arguments/argument[@name="label"]'))) { - $list[(string) $referenceContainerBlocks[$count]['name']] = [ - 'value' => (string) $referenceContainerBlocks[$count]['name'], - 'label' => (string) $referenceContainerBlocks[$count]->xpath('arguments/argument[@name="label"]')[0] - ]; - } - } + for ($count = 0; $count < count($referenceContainerBlocks); $count++) { + if (!empty($referenceContainerBlocks[$count]->xpath('arguments/argument[@name="label"]'))) { + $this->updateReferenceContainerList($referenceContainerBlocks, $count); } } } } + } - return $list; + /** + * @param $rcb + * @param $count + */ + protected function updateReferenceContainerList($rcb, $count) + { + $this->list[(string) $rcb[$count]['name']] = [ + 'value' => (string) $rcb[$count]['name'], + 'label' => (string) $rcb[$count]->xpath('arguments/argument[@name="label"]')[0] + ]; + } + + /** + * @param $rb + */ + protected function updateReferenceBlockList($rb) + { + $this->list[(string) $rb->block['name']] = [ + 'value' => (string) $rb->block['name'], + 'label' => (string) $rb->xpath('block/arguments/argument[@name="label"]')[0] + ]; } } diff --git a/composer.json b/composer.json index d5c5c4b..26855f4 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "magento/framework": "100.0.*|100.1.*|101.0.*" }, "type": "magento2-module", - "version": "1.1.4", + "version": "1.1.5", "autoload": { "files": [ "registration.php"