Skip to content

Commit

Permalink
feat: route name, route middleware, route head
Browse files Browse the repository at this point in the history
- suport named route
- suport route view
- support route with HEAD method
  • Loading branch information
SonyPradana committed Sep 14, 2021
1 parent d8e82b9 commit 043373f
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add ```Request::class``` to handle http request
- Add ```Response::class``` to handle http respone
- Add ```RequestFactory::class``` to create request from global parameter
- ```Router::class``` suport router name, route view (override), support HEAD request method
- Route suport middleware ```Router::middleware```

### Changed
- Rename class ```RouterFactory::class``` to ```RouteFactory::class```
- Rename class ```RouterProvider::class``` to ```RouteProvider::class```

## [0.1.0] - 2021-07-12
### Added
Expand Down
11 changes: 11 additions & 0 deletions src/System/Router/AbstractMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace System\Router;

abstract class AbstractMiddleware
{
public function handle()
{
// ovveridedable
}
}
30 changes: 30 additions & 0 deletions src/System/Router/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace System\Router;

abstract class Controller
{
public function __invoke($invoke)
{
call_user_func([$this, $invoke]);
}

public static function renderView($view_name, $portal)
{
// overwrite
}

/**
* @var static This classs
*/
private self $_static;

/**
* Instance of controller.
* Shorthadn to crete new class
*/
public static function static()
{
return self::$_static ?? new static;
}
}
24 changes: 24 additions & 0 deletions src/System/Router/RouteNamed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace System\Router;

class RouteNamed
{
private $route;

public function __construct(array $route, ?string $default_name = 'global')
{
$route['name'] = $default_name ?? 'global';
$this->route = $route;
}

public function name(string $name)
{
$this->route['name'] =$name;
}

public function __destruct()
{
Router::addRoutes($this->route);
}
}
104 changes: 91 additions & 13 deletions src/System/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class Router
private static $pathNotFound = null;
private static $methodNotAllowed = null;

/**
* Short hand to readable regex url
*/
public static $patterns = Array (
'(:id)' => '(\d+)',
'(:num)' => '([0-9]*)',
Expand All @@ -17,6 +20,13 @@ class Router
'(:all)' => '(.*)',
);

public static function mapPatterns(string $url): string
{
$user_pattern = array_keys(self::$patterns);
$allow_pattern = array_values(self::$patterns);
return str_replace($user_pattern, $allow_pattern, $url);
}

/**
* Adding new router using array of router
* @param array $route Router array format (expression, function, method)
Expand All @@ -30,6 +40,12 @@ public static function addRoutes(array $route)
}
}

public static function mergeRoutes(array $array_routes)
{
// warning:: all item will push without validation
array_push(self::$routes, ...$array_routes);
}

/**
* Get routes has added
* @return array Routes array
Expand Down Expand Up @@ -59,24 +75,42 @@ public static function prefix(string $prefix)
return new RouteFactory($prefix);
}

public static function middleware(array $middlewares)
{
foreach ($middlewares as $middleware) {
if ($middleware instanceof AbstractMiddleware) {
$middleware->handle();
}
}
}

public static function view(string $uri, string $view_name, array $portal = [])
{
self::match('get', $uri,
fn() => Controller::renderView($view_name, $portal)
);
}

/**
* Function used to add a new route
* @param array|string $method Methods allow
* @param string $expression Route string or expression
* @param callable $function Function to call if route with allowed method is found
* @param callable|array $function Function to call if route with allowed method is found
*/
public static function match($method, string $uri, $callback)
{
$user_pattern = array_keys(self::$patterns);
$allow_pattern = array_values(self::$patterns);
$new_uri = str_replace($user_pattern, $allow_pattern, $uri);
$route = Array (
'expression' => $new_uri,
'function' => $callback,
'method' => $method
);
if (is_array($callback)) {
$callback = function() use ($callback) {
$a = new $callback[0];
return $a($callback[1]);
};
}

array_push(self::$routes, $route);
return new RouteNamed([
'method' => $method,
'expression' => self::mapPatterns($uri),
'function' => $callback
]);
}

/**
Expand All @@ -87,7 +121,7 @@ public static function match($method, string $uri, $callback)
*/
public static function any(string $expression, $function)
{
self::match(['get','post', 'put', 'patch', 'delete', 'options'], $expression, $function);
return self::match(['get', 'head', 'post', 'put', 'patch', 'delete', 'options'], $expression, $function);
}

/**
Expand All @@ -98,7 +132,7 @@ public static function any(string $expression, $function)
*/
public static function get(string $expression, $function)
{
self::match('get', $expression, $function);
return self::match(['get', 'head'], $expression, $function);
}

/**
Expand All @@ -109,7 +143,51 @@ public static function get(string $expression, $function)
*/
public static function post(string $expression, $function)
{
self::match('post', $expression, $function);
return self::match('post', $expression, $function);
}

/**
* Function used to add a new route [method: put]
* @param string $expression Route string or expression
* @param callable $function Function to call if route with allowed method is found
*
*/
public static function put(string $expression, $function)
{
return self::match('put', $expression, $function);
}

/**
* Function used to add a new route [method: patch]
* @param string $expression Route string or expression
* @param callable $function Function to call if route with allowed method is found
*
*/
public static function patch(string $expression, $function)
{
return self::match('patch', $expression, $function);
}

/**
* Function used to add a new route [method: delete]
* @param string $expression Route string or expression
* @param callable $function Function to call if route with allowed method is found
*
*/
public static function delete(string $expression, $function)
{
return self::match('delete', $expression, $function);
}

/**
* Function used to add a new route [method: options]
* @param string $expression Route string or expression
* @param callable $function Function to call if route with allowed method is found
*
*/
public static function options(string $expression, $function)
{
return self::match('options', $expression, $function);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/Router/BasicRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ private function registerRouterDiferentMethod()
{
Router::match( ['get'], '/get', function() {
echo "render success using get";
})->name('name_is_get');
Router::match( ['head'], '/head', function() {
echo "render success using get over head method";
});
Router::match( ['post'], '/post', function() {
echo "render success using post";
Expand Down Expand Up @@ -157,6 +160,7 @@ public function it_route_can_be_render_diferent_method(): void
{
$this->registerRouterDiferentMethod();
$get = $this->getRespone('get', '/get');
$head = $this->getRespone('head', '/head');
$post = $this->getRespone('post', '/post');
$put = $this->getRespone('put', '/put');
$patch = $this->getRespone('patch', '/patch');
Expand All @@ -168,6 +172,11 @@ public function it_route_can_be_render_diferent_method(): void
$get,
"render success using get"
);
$this->assertEquals(
"render success using get over head method",
$head,
"render success using get over head method"
);
$this->assertEquals(
"render success using post",
$post,
Expand Down Expand Up @@ -253,4 +262,18 @@ public function it_page_is_not_found(): void
'it must render "page is not found"'
);
}

/**
* @test
*/
public function it_can_pass_global_middleware(): void
{
require_once dirname(__DIR__) . '\Router\TestMiddleware.php';

Router::middleware([
new TestMiddleware,
]);

$this->assertEquals('oke', $_SERVER['middleware'], 'all route must pass global middleware');
}
}
11 changes: 11 additions & 0 deletions tests/Router/TestMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use System\Router\AbstractMiddleware;

class TestMiddleware extends AbstractMiddleware
{
public function handle()
{
$_SERVER['middleware'] = 'oke';
}
}

0 comments on commit 043373f

Please sign in to comment.