Skip to content

Commit

Permalink
update 'group' method
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Aug 20, 2023
1 parent 5fc8749 commit 5b2f23e
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 38 deletions.
110 changes: 110 additions & 0 deletions src/RouteGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

namespace Effectra\Router;

/**
* Class RouteGroup
* @package Effectra\Router
*/
class RouteGroup
{
/**
* RouteGroup constructor.
*
* @param string $prePath The prefix path for routes in this group.
* @param string $controller The controller class name.
* @param Route $router The Route instance for defining routes.
*/
public function __construct(
protected string $prePath,
protected string $controller,
protected Route $router
) {
}

/**
* Define a GET route.
*
* @param string $pattern The route pattern.
* @param string $method The controller method to call.
* @return void
*/
public function get(string $pattern, string $method): void
{
$this->router->get($this->prePath . $pattern, [$this->controller, $method]);
}

/**
* Define a POST route.
*
* @param string $pattern The route pattern.
* @param string $method The controller method to call.
* @return void
*/
public function post(string $pattern, string $method): void
{
$this->router->post($this->prePath . $pattern, [$this->controller, $method]);
}

/**
* Define a PUT route.
*
* @param string $pattern The route pattern.
* @param string $method The controller method to call.
* @return void
*/
public function put(string $pattern, string $method): void
{
$this->router->put($this->prePath . $pattern, [$this->controller, $method]);
}

/**
* Define a DELETE route.
*
* @param string $pattern The route pattern.
* @param string $method The controller method to call.
* @return void
*/
public function delete(string $pattern, string $method): void
{
$this->router->delete($this->prePath . $pattern, [$this->controller, $method]);
}

/**
* Define a PATCH route.
*
* @param string $pattern The route pattern.
* @param string $method The controller method to call.
* @return void
*/
public function patch(string $pattern, string $method): void
{
$this->router->patch($this->prePath . $pattern, [$this->controller, $method]);
}

/**
* Define an OPTIONS route.
*
* @param string $pattern The route pattern.
* @param string $method The controller method to call.
* @return void
*/
public function options(string $pattern, string $method): void
{
$this->router->options($this->prePath . $pattern, [$this->controller, $method]);
}

/**
* Define a route that responds to any HTTP method.
*
* @param string $pattern The route pattern.
* @param string $method The controller method to call.
* @return void
*/
public function any(string $pattern, string $method): void
{
$this->router->any($this->prePath . $pattern, [$this->controller, $method]);
}
}
47 changes: 9 additions & 38 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,50 +48,21 @@ public function auth(string $pattern, $controller): self
}
return $this;
}

/**
* Define a group of routes that share a common URL prefix.
*example `['get|info/{id}' => 'info']`
* Create a group of routes with a common prefix.
*
* @param string $common_route The common URL prefix for the group of routes.
* @param mixed $controller The controller for the group of routes.
* @param array $methods An array of HTTP methods and route patterns for the group of routes.
* @return $this
* @throws Exception if the route
* @param string $path The common prefix for routes in this group.
* @param string|object $controller The controller class name or instance.
* @param callable $routes A callable that defines routes within the group.
* @return self Returns the current Route instance.
*/
public function group(string $common_route, $controller, array $methods): self
public function group(string $path, $controller, callable $routes): self
{
$common_route = $this->remakeRoute($common_route);

foreach ($methods as $m) {

if (!is_array($m)) {
throw new Exception("Only array passed, like ['get|info/{id}' => 'info'] ");
}

if (!str_contains(key($m), '|')) {
throw new Exception("Separator '|' not found");
}

$ext_route = explode('|', key($m));

$action = $m[key($m)];

$method = trim($ext_route[0]);

if (!in_array($method, $this->methods)) {

throw new Exception("Http Method not exists !");
}

$route = trim(end($ext_route));

if (method_exists($controller, $action)) {
$this->$method($common_route . '/' . $route, [$controller, $action]);
}
}
call_user_func_array($routes, [new RouteGroup($path, $controller, $this)]);
return $this;
}

/**
*Define a group of CRUD routes that share a common URL prefix.
Expand Down

0 comments on commit 5b2f23e

Please sign in to comment.