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

[WIP] Inline the endpoint search process #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
44 changes: 41 additions & 3 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Dispatcher implements RequestHandlerInterface
private $containerHasAuthProviders = false;
private $containerHasErrorHandler = false;
private $endpointList = self::ENDPOINT_LIST;
private $endpointMap;
private $parserList = self::PARSER_LIST;
private $psrMiddleware = [];

Expand Down Expand Up @@ -102,6 +103,7 @@ public function setParserList($parserList): self
*/
public function setEndpointList($endpointList): self
{
$this->endpointMap = null;
$this->endpointList = $endpointList;
return $this;
}
Expand Down Expand Up @@ -140,9 +142,10 @@ private function doDispatch(ServerRequestInterface $request): ResponseInterface
/** @var ?EndpointInterface */
$endpoint = null;
try {
[$fqcn, $uriData] = (new ClassMapper($this->endpointList))
->filter(strtoupper($request->getMethod()))
->search($request->getUri()->getPath());
// [$fqcn, $uriData] = (new ClassMapper($this->endpointList))
// ->filter(strtoupper($request->getMethod()))
// ->search($request->getUri()->getPath());
[$fqcn, $uriData] = $this->findEndpoint($request);
if (!$fqcn) {
throw new OutOfBoundsException('Endpoint not found', 404);
}
Expand Down Expand Up @@ -191,6 +194,41 @@ private function doDispatch(ServerRequestInterface $request): ResponseInterface
return $response;
}

private function findEndpoint(ServerRequestInterface $request): array
{
if ($this->endpointMap === null) {
if (is_string($this->endpointList)) {
if (!file_exists($this->endpointList)) {
throw new \InvalidArgumentException('Endpoint list not found');
}
$this->endpointMap = require $this->endpointList;
} elseif (is_array($this->endpointList)) {
$this->endpointMap = $this->endpointList;
} else {
// error
}
}
$method = strtoupper($request->getMethod());
if (!isset($this->endpointMap[$method])) {
throw new OutOfBoundsException('Endpoint not found', 404);
}
$endpoints = $this->endpointMap[$method];
$requestPath = $request->getUri()->getPath();
foreach ($endpoints as $path => $fqcn) {
$pattern = sprintf('#^%s$#', $path);
if (preg_match($pattern, $requestPath, $matches)) {
$urlParams = [];
foreach ($matches as $i => $match) {
if (is_string($i)) {
$urlParams[$i] = $match;
}
}
return [$fqcn, $urlParams];
}
}
return [null, null];
}

/**
* Parse the raw input body based on the content type
*
Expand Down