Skip to content

Implemented Arbitrary Parameters match from Path #4

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

Open
wants to merge 2 commits 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
36 changes: 20 additions & 16 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ class Application

protected $response;

protected $patterns = array(
':id' => '(\d+)',
':hash' => '([a-f\d+]{32})'
);
protected $patterns = array();

public function __construct()
{
Expand Down Expand Up @@ -124,6 +121,20 @@ public function run()

foreach ($this->router->getRoutes() as $route)
{
$parts = explode('/', $route->getPath());
$patterns = [];
$pattern = "";
foreach($parts as $i=>$part) {
if(substr( $part, 0, 1 ) == ":") {
$patterns[$part] = $i;
$pattern .= "/" . "(.+)";
} else {
$pattern .= "/" . $part;
}
}
$pattern = "#^" . substr($pattern, 1) . "$#";
$this->patterns = $patterns;

$match1 = in_array($route->getMethod(), array('all', 'use', strtolower($this->request->method)));
if (!$match1)
{
Expand All @@ -132,7 +143,6 @@ public function run()

$use = $route->getMethod() == "use";

$pattern = sprintf("#^%s$#", str_replace(array_keys($this->patterns), array_values($this->patterns), $route->getPath()));
if (!$use)
{
$match2 = null;
Expand All @@ -141,7 +151,7 @@ public function run()
continue;
} else {
array_shift($match2);
$this->setParams($match2);
$this->setParams();
}
} else {
if (!($route->getPath() == '*' || preg_match($pattern, $this->request->path)))
Expand All @@ -165,19 +175,13 @@ public function run()
}
}

protected function setParams(array $match2): Application
protected function setParams(): Application
{
foreach ($match2 as $match)
{
foreach ($this->patterns as $param => $pattern)
$parts = explode('/', $this->request->path);
foreach ($this->patterns as $param => $i)
{
if (preg_match("#^$pattern$#", $match))
{
$this->request->params[substr($param, 1)] = $match;
}
$this->request->params[substr($param, 1)] = $parts[$i];
}
}

return $this;
}

Expand Down