Skip to content

Commit

Permalink
Router redirect methods; fix to any method matches
Browse files Browse the repository at this point in the history
  • Loading branch information
donwilson committed Nov 2, 2023
1 parent f679e83 commit 536a014
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
11 changes: 4 additions & 7 deletions src/Magnetar/Router/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,29 +353,26 @@ public function matches(
HTTPMethodEnum $method,
string $path
): bool {
//print "Checking path[". $path ."] against ". $this->path_basic ." [". $this->path_regex ."]<br>\n";

// check method
// @todo use a bitwise match operation instead of in_array()?
if(!in_array($method, $this->methods)) {
// @TODO maybe something more performant like a bitwise match instead of in_array()
if((null !== $this->methods) && !in_array($method, $this->methods)) {
// request method isn't the same
return false;
}

// check path
if($this->path_is_regex) {
// regex path match?
if(!preg_match($this->path_regex, $path, $matches)) {
return false;
}

// path matches

// parse path matches
$this->parseMatchedPathVariables($matches);

return true;
} else {
// basic path match
// basic path match?
if($path !== $this->path_basic) {
return false;
}
Expand Down
39 changes: 35 additions & 4 deletions src/Magnetar/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magnetar\Http\Response;
use Magnetar\Router\RouteCollection;
use Magnetar\Router\Enums\HTTPMethodEnum;
use Magnetar\Http\RedirectResponse;
use Magnetar\Router\Exceptions\RouteUnassignedException;
use Magnetar\Router\Exceptions\CannotProcessRouteException;

Expand Down Expand Up @@ -330,22 +331,52 @@ public function options(
}

/**
* Group routes together under a common prefix and pass a child router instance to the callback to run matches against
* Group routes together under a common prefix and pass a child router instance to the callback to run matches against. Returns the contextualized route collection
* @param string $pattern The pattern to match against
* @param string $method The HTTP method to match against
* @return void
* @return RouteCollection
*/
public function group(string $pathPrefix, callable $callback): void {
public function group(string $pathPrefix, callable $callback): RouteCollection {
// instantiate a route collection, pass in the context, path prefix, and callback that will
// use a temporary router context to define routes. Automatically reverts context when finished
new RouteCollection(
return new RouteCollection(
$this,
$this->routeCollection,
$pathPrefix,
$callback
);
}

/**
* Assign a redirect rule for a given path
* @param string $pattern The pattern to match against
* @param string $redirect_path The URI to redirect to
* @param int $status The HTTP status code to use. Defaults to 302
* @return Route
*/
public function redirect(string $pattern, string $redirect_path, int $status=302): Route {
return $this->assignRoute(
null,
$pattern,
fn() => $this->container->instance('response', (new RedirectResponse)->to($redirect_path)->status($status))
);
}

/**
* Assign a permanent redirect (301) rule for a given path
* @param string $pattern The pattern to match against
* @param string $redirect_path The URI to redirect to
* @param int $status The HTTP status code to use
* @return Route
*/
public function permanentRedirect(string $pattern, string $redirect_path): Route {
return $this->assignRoute(
null,
$pattern,
fn() => $this->container->instance('response', (new RedirectResponse)->to($redirect_path)->permanent())
);
}

/**
* Catch magic method calls and assign routes based on the method name if it matches a valid HTTP method name
* @param string $name The method name
Expand Down

0 comments on commit 536a014

Please sign in to comment.