Skip to content

Commit

Permalink
routers can now have priorities
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Blanquera committed Jan 14, 2019
1 parent 8920886 commit e35bdfc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 52 deletions.
90 changes: 50 additions & 40 deletions src/Http/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,16 @@ public function process(RequestInterface $request, ...$args): bool
* @param *string $method The request method
* @param *string $pattern The route pattern
* @param *callable $callback The middleware handler
* @param int $priority if true will be prepended in order
*
* @return RouterInterface
*/
public function route(string $method, string $pattern, callable $callback): RouterInterface
public function route(
string $method,
string $pattern,
callable $callback,
int $priority = 0
): RouterInterface
{
//hard requirement
if (!is_callable($callback)) {
Expand Down Expand Up @@ -103,50 +109,54 @@ public function route(string $method, string $pattern, callable $callback): Rout
//we need the handler for later
$handler = $this->getEventHandler();

$handler->on($event, function (
RequestInterface $request,
...$args
) use (
$handler,
$callback,
$pattern,
$keys
) {
$route = $handler->getMeta();
$variables = array();
$parameters = array();

//sanitize the variables
foreach ($route['variables'] as $i => $variable) {
//if it's a * variable
if (!isset($keys[$i]) || strpos($keys[$i], '*') === 0) {
//it's a variable
if (strpos($variable, '/') === false) {
$variables[] = $variable;
$handler->on(
$event,
function (
RequestInterface $request,
...$args
) use (
$handler,
$callback,
$pattern,
$keys
) {
$route = $handler->getMeta();
$variables = array();
$parameters = array();

//sanitize the variables
foreach ($route['variables'] as $i => $variable) {
//if it's a * variable
if (!isset($keys[$i]) || strpos($keys[$i], '*') === 0) {
//it's a variable
if (strpos($variable, '/') === false) {
$variables[] = $variable;
continue;
}

$variables = array_merge($variables, explode('/', $variable));
continue;
}

$variables = array_merge($variables, explode('/', $variable));
continue;
//if it's a :parameter
if (isset($keys[$i])) {
$key = substr($keys[$i], 1);
$parameters[$key] = $variable;
}
}

//if it's a :parameter
if (isset($keys[$i])) {
$key = substr($keys[$i], 1);
$parameters[$key] = $variable;
}
}

$request
->setStage($parameters)
->setRoute(array(
'event' => $route['event'],
'variables' => $variables,
'parameters' => $parameters
));

return call_user_func($callback, $request, ...$args);
});
$request
->setStage($parameters)
->setRoute(array(
'event' => $route['event'],
'variables' => $variables,
'parameters' => $parameters
));

return call_user_func($callback, $request, ...$args);
},
$priority
);

return $this;
}
Expand Down
30 changes: 18 additions & 12 deletions src/Http/Router/RouterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,41 @@ trait RouterTrait
*
* @param *string $path The route path
* @param *callable $callback The middleware handler
* @param int $priority if true will be prepended in order
*
* @return RouterTrait
*/
public function all(string $path, callable $callback)
public function all(string $path, callable $callback, int $priority = 0)
{
return $this->route('all', $path, $callback);
return $this->route('all', $path, $callback, $priority);
}

/**
* Adds routing middleware for delete method
*
* @param *string $path The route path
* @param *callable $callback The middleware handler
* @param int $priority if true will be prepended in order
*
* @return RouterTrait
*/
public function delete(string $path, callable $callback)
public function delete(string $path, callable $callback, int $priority = 0)
{
return $this->route('delete', $path, $callback);
return $this->route('delete', $path, $callback, $priority);
}

/**
* Adds routing middleware for get method
*
* @param *string $path The route path
* @param *callable $callback The middleware handler
* @param int $priority if true will be prepended in order
*
* @return RouterTrait
*/
public function get(string $path, callable $callback)
public function get(string $path, callable $callback, int $priority = 0)
{
return $this->route('get', $path, $callback);
return $this->route('get', $path, $callback, $priority);
}

/**
Expand All @@ -89,25 +92,27 @@ public function getRouter()
*
* @param *string $path The route path
* @param *callable $callback The middleware handler
* @param int $priority if true will be prepended in order
*
* @return RouterTrait
*/
public function post(string $path, callable $callback)
public function post(string $path, callable $callback, int $priority = 0)
{
return $this->route('post', $path, $callback);
return $this->route('post', $path, $callback, $priority);
}

/**
* Adds routing middleware for put method
*
* @param *string $path The route path
* @param *callable $callback The middleware handler
* @param int $priority if true will be prepended in order
*
* @return RouterTrait
*/
public function put(string $path, callable $callback)
public function put(string $path, callable $callback, int $priority = 0)
{
return $this->route('put', $path, $callback);
return $this->route('put', $path, $callback, $priority);
}

/**
Expand All @@ -116,12 +121,13 @@ public function put(string $path, callable $callback)
* @param *string $method The request method
* @param *string $path The route path
* @param *callable $callback The middleware handler
* @param int $priority if true will be prepended in order
*
* @return RouterTrait
*/
public function route(string $method, string $path, callable $callback)
public function route(string $method, string $path, callable $callback, int $priority = 0)
{
$this->getRouter()->route($method, $path, $callback);
$this->getRouter()->route($method, $path, $callback, $priority);

return $this;
}
Expand Down

0 comments on commit e35bdfc

Please sign in to comment.