Skip to content

Commit

Permalink
Add router middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
dederobert committed Aug 24, 2017
1 parent 9661234 commit 634c168
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 7 deletions.
6 changes: 0 additions & 6 deletions index.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DuskPHP;
namespace DuskPHP\Core;

use GuzzleHttp\Psr7\Response;
use Interop\Http\ServerMiddleware\DelegateInterface;
Expand Down
6 changes: 6 additions & 0 deletions src/Exception/RouterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

namespace DuskPHP\Core\Exception;


class RouterException extends \Exception {}
29 changes: 29 additions & 0 deletions src/HomePage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace DuskPHP\Core;


use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class HomePage implements MiddlewareInterface
{

/**
* Process an incoming server request and return a response, optionally delegating
* to the next middleware component to create the response.
*
* @param ServerRequestInterface $request
* @param DelegateInterface $delegate
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$response = $delegate->process($request);
$response->getBody()->write('Homepage');
return $response;
}
}
92 changes: 92 additions & 0 deletions src/Router/Route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Created by PhpStorm.
* User: dederobert
* Date: 25/08/17
* Time: 00:39
*/

namespace DuskPHP\Core \Router;


use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;

class Route
{
/**
* @var string
*/
private $path;

/**
* @var MiddlewareInterface
*/
private $middleware;

/**
* @var array
*/
private $params = [];

/**
* @var array
*/
private $matches = [];

function __construct(string $path,MiddlewareInterface $middleware)
{
$this->path = trim($path,'/');
$this->middleware = $middleware;
}

public function with($param, string $regex)
{
$this->params[$param] = str_replace('(', '(?:', $regex);
return $this;
}

/**
* @param string $url
* @return bool
*/
public function match(string $url): bool {
$url = trim($url, '/');
$path = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], $this->path);
$regex = "#^$path$#i";


if (!preg_match($regex, $url, $matches))
return false;
array_shift($matches);
$this->matches = $matches;
return true;
}

private function paramMatch(array $match): string {
if (isset($this->params[$match[1]])) {
return '(' . $this->params[$match[1]] . ')';
}
return '([^/]+';
}

public function call(ServerRequestInterface $request, DelegateInterface $delegate) {
// if (is_string($this->middleware)) {
// $params = explode('#', $this->middleware);
// $controller = "App\\Controller\\" . $params[0]."Controller";
// $controller = new $controller();
// return call_user_func_array([$controller, $params[1], $this->matches]);
// }
return $this->middleware->process($request, $delegate);
// return call_user_func_array($this->middleware, $this->matches);
}

public function getURL($params): string {
$path = $this->path;
foreach ($params as $k => $v)
$path = str_replace(":$k", $v, $path);
return $path;
}

}
94 changes: 94 additions & 0 deletions src/Router/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Created by PhpStorm.
* User: dederobert
* Date: 25/08/17
* Time: 00:38
*/

namespace DuskPHP\Core\Router;


use DuskPHP\Core\Exception\RouterException;
use GuzzleHttp\Psr7\Response;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class Router implements MiddlewareInterface
{
/**
* @var array
*/
private $routes = [];

private $namedRoutes = [];

public function get(string $path,MiddlewareInterface $middleware,string $name): Route
{
return $this->add($path, $middleware, $name, 'GET');
}

public function post(string $path, MiddlewareInterface $middleware,string $name): Route
{
return $this->add($path, $middleware, $name, 'POST');
}

private function add(string $path, MiddlewareInterface $middleware,string $name, string $method): Route
{
$route = new Route($path, $middleware);
$this->routes[$method][] = $route;

if ($name)
$this->namedRoutes[$name] = $route;
else
throw new RouterException('The name\'s route must be defined');

return $route;
}

// public function run(){
// if (!isset($this->routes[$_SERVER['REQUEST_METHOD']])) {
// throw new RouterException('REQUEST_METHOD does not exist');
// }
//
// foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) {
// if ($route->match($this->url)) {
// return $route->call();
// }
// }
//
// throw new RouterException('No matching routes');
// }

public function url($name, $params = []){
if (!isset($this->namedRoutes[$name]))
throw new RouterException('No route matches this name');
return $this->namedRoutes[$name]->getURl($params);
}

/**
* Process an incoming server request and return a response, optionally delegating
* to the next middleware component to create the response.
*
* @param ServerRequestInterface $request
* @param DelegateInterface $delegate
* @return ResponseInterface
* @throws RouterException
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
if (!isset($this->routes[$request->getMethod()]))
throw new RouterException($request->getMethod() . ' does not exist');

foreach ($this->routes[$request->getMethod()] as $route)
if ($route->match($request->getQueryParams()['url']))
return $route->call($request, $delegate);

$response = new Response();
$response->getBody()->write('Not found');
return $response->withStatus(404);

}
}

0 comments on commit 634c168

Please sign in to comment.