-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9661234
commit 634c168
Showing
6 changed files
with
222 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
namespace DuskPHP\Core\Exception; | ||
|
||
|
||
class RouterException extends \Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |