Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Modern Routing] make a simpler loop in StandardRules::build #19012

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 41 additions & 34 deletions libraries/src/Component/Router/Rules/StandardRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,65 +241,72 @@ 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++)
foreach ($path as $element => $ids)
{
reset($path);
$view = key($path);
$view = $views[$element];

if ($found)
if ($found === false && $item && $item->query['view'] === $element)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think $item check is not needed here as we check and make sure it is not null in previous code already

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

{
$ids = array_shift($path);
// Id of the last added segment from menu item
$last_id = $view->key ? (int) $item->query[$view->key] : 0;

if ($views[$view]->nestable)
if ($view->nestable)
{
$found = true;
}
elseif ($view->children)
{
$found = true;

continue;
}
}

if ($found === false)
{
// Jump to the next view
continue;
}

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 ($last_id === 0 || $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)
{
array_shift($path);
}
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)
Expand Down