From 561c70cd900ff6b84a1ef6d2b16bf0075980fa84 Mon Sep 17 00:00:00 2001 From: Tomasz Narloch Date: Thu, 7 Dec 2017 17:58:53 +0100 Subject: [PATCH 1/3] Simpler loop in build method of StandardRules --- .../Component/Router/Rules/StandardRules.php | 83 ++++++++++++------- 1 file changed, 51 insertions(+), 32 deletions(-) diff --git a/libraries/src/Component/Router/Rules/StandardRules.php b/libraries/src/Component/Router/Rules/StandardRules.php index a9e0f18fade4b..fef1cee6e14c8 100644 --- a/libraries/src/Component/Router/Rules/StandardRules.php +++ b/libraries/src/Component/Router/Rules/StandardRules.php @@ -241,65 +241,84 @@ public function build(&$query, &$segments) } // Get the path from the view of the current URL and parse it to the menu item - $path = array_reverse($this->router->getPath($query), true); - $found = false; - $found2 = false; + $path = array_reverse($this->router->getPath($query), true); + $found = false; - for ($i = 0, $j = count($path); $i < $j; $i++) + // Id of the last added segment + $last_id = 0; + + foreach ($path as $element => $ids) { - reset($path); - $view = key($path); + $view = $views[$element]; + + if ($found === false && $item && $item->query['view'] === $element) + { + if ($view->key) + { + // Get id from menu item + $last_id = (int) $item->query[$view->key]; + } + + if ($view->nestable) + { + $found = true; + } + elseif ($view->children) + { + $found = true; + + continue; + } + } - if ($found) + if ($found === false) { - $ids = array_shift($path); + // Jump to the next view + continue; + } - if ($views[$view]->nestable) + if ($ids) + { + if ($view->nestable) { + $found2 = false; + foreach (array_reverse($ids, true) as $id => $segment) { if ($found2) { $segments[] = str_replace(':', '-', $segment); + $last_id = (int) $id; } - elseif ((int) $item->query[$views[$view]->key] == (int) $id) + elseif (($view->parent && !$view->parent->key) || $last_id === (int) $id) { $found2 = true; } } } - elseif (is_bool($ids)) + elseif ($ids === true) { - $segments[] = $views[$view]->name; + $segments[] = $element; + $last_id = 0; } else { - $segments[] = str_replace(':', '-', array_shift($ids)); + $segments[] = str_replace(':', '-', current($ids)); + $last_id = (int) key($ids); } } - elseif ($item->query['view'] !== $view) + + if ($view->name === $query['view']) { - array_shift($path); + // Remove key from query + unset($query[$view->key]); } - else - { - if (!$views[$view]->nestable) - { - array_shift($path); - } - else - { - $i--; - $found2 = false; - } - if (count($views[$view]->children)) - { - $found = true; - } + if ($view->parent_key) + { + // Remove parent key from query + unset($query[$view->parent_key]); } - - unset($query[$views[$view]->parent_key]); } if ($found) From 77ff7a8ef67d7bec84bff5039f7943a3f3992195 Mon Sep 17 00:00:00 2001 From: Tomasz Narloch Date: Fri, 8 Dec 2017 09:16:30 +0100 Subject: [PATCH 2/3] Now use last_id only --- .../src/Component/Router/Rules/StandardRules.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/libraries/src/Component/Router/Rules/StandardRules.php b/libraries/src/Component/Router/Rules/StandardRules.php index fef1cee6e14c8..9ceff2c65b894 100644 --- a/libraries/src/Component/Router/Rules/StandardRules.php +++ b/libraries/src/Component/Router/Rules/StandardRules.php @@ -244,20 +244,14 @@ public function build(&$query, &$segments) $path = array_reverse($this->router->getPath($query), true); $found = false; - // Id of the last added segment - $last_id = 0; - foreach ($path as $element => $ids) { $view = $views[$element]; if ($found === false && $item && $item->query['view'] === $element) { - if ($view->key) - { - // Get id from menu item - $last_id = (int) $item->query[$view->key]; - } + // Id of the last added segment from menu item + $last_id = $view->key ? (int) $item->query[$view->key] : 0; if ($view->nestable) { @@ -290,7 +284,7 @@ public function build(&$query, &$segments) $segments[] = str_replace(':', '-', $segment); $last_id = (int) $id; } - elseif (($view->parent && !$view->parent->key) || $last_id === (int) $id) + elseif ($last_id === 0 || $last_id === (int) $id) { $found2 = true; } From bf28ce670e5d0bbe93c83431f8a2f6ccd9dc7e7e Mon Sep 17 00:00:00 2001 From: Tomasz Narloch Date: Fri, 8 Dec 2017 09:45:25 +0100 Subject: [PATCH 3/3] Remove useless code --- libraries/src/Component/Router/Rules/StandardRules.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/libraries/src/Component/Router/Rules/StandardRules.php b/libraries/src/Component/Router/Rules/StandardRules.php index 9ceff2c65b894..2787304e2eba1 100644 --- a/libraries/src/Component/Router/Rules/StandardRules.php +++ b/libraries/src/Component/Router/Rules/StandardRules.php @@ -302,12 +302,6 @@ public function build(&$query, &$segments) } } - if ($view->name === $query['view']) - { - // Remove key from query - unset($query[$view->key]); - } - if ($view->parent_key) { // Remove parent key from query