Break out of loop sooner

This commit is contained in:
Valithor Obsidion 2025-02-04 15:20:51 -05:00
parent d42b6bdef7
commit ccb5a236a0

View File

@ -71,33 +71,22 @@ trait SegmentRouterTrait
} }
// params will hold any dynamic segments we find // params will hold any dynamic segments we find
$params = array_reduce( $params = [];
explode('/', trim($uri, '/')), foreach (explode('/', trim($uri, '/')) as $segment) {
function ($carry, $segment) use (&$node) { if (isset($node[$segment])) {
if (isset($node[$segment])) { $node = $node[$segment];
$node = $node[$segment]; } elseif (isset($node[':x'])) {
} elseif (isset($node[':x'])) { $params[] = $segment;
$carry[] = $segment; $node = $node[':x'];
$node = $node[':x']; } else {
} else { return ['code' => 404, 'handler' => null, 'params' => []];
throw new \Exception('404'); }
} }
return $carry;
},
[]
);
// if we found a handler for the method, return it and any params. if not, return a 405 // if we found a handler for the method, return it and any params. if not, return a 405
return isset($node[$method]) return isset($node[$method])
? [ ? ['code' => 200, 'handler' => $node[$method], 'params' => $params]
'code' => 200, : ['code' => 405, 'handler' => null, 'params' => []];
'handler' => $node[$method],
'params' => $params ?? []]
: [
'code' => 405,
'handler' => null,
'params' => []
];
} }
/** /**