Skip to content

Commit

Permalink
update to crazy roter v2
Browse files Browse the repository at this point in the history
update code
update examples
  • Loading branch information
s2x committed Feb 20, 2019
1 parent abe06bd commit 05e5663
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"pimple/pimple": "~3.0",
"nyholm/psr7": "^1.0",
"nyholm/psr7-server": "^0.3.0",
"crazy-goat/router": "^1.0"
"crazy-goat/router": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
17 changes: 8 additions & 9 deletions examples/index.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
<?php
declare(strict_types=1);

use CrazyGoat\Router\DispatcherFactory;
use CrazyGoat\Router\RouteCollector;
use function CrazyGoat\Router\simpleDispatcher;
use CrazyGoat\Tiny\{App, Router};
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
use Pimple\Container;

error_reporting(E_ALL);
ini_set('display_errors', 'on');

include "../vendor/autoload.php";

$container = new Container();
$container['router'] = function () {
$dispatcher = simpleDispatcher(function(RouteCollector $r): void {
$r->addRoute('GET', '/', 'controller::main');
$r->addRoute('GET', '/hello[/{name}]', 'controller::hello');
});
$dispatcher = DispatcherFactory::createFromClosure(
function (RouteCollector $r): void {
$r->get('/', 'controller::main');
$r->get('/hello[/{name}]', 'controller::hello');
}
);
return new Router($dispatcher);
};

Expand All @@ -30,7 +29,7 @@

$container['controller::hello'] = function () {
return function (ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {
$response->getBody()->write('<h1>Hello '.$request->getAttribute('name', 'unknown').'!</h1>');
$response->getBody()->write('<h1>Hello ' . $request->getAttribute('name', 'unknown') . '!</h1>');
return $response;
};
};
Expand Down
18 changes: 10 additions & 8 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace CrazyGoat\Tiny;

use FastRoute\RouteCollector;
use function FastRoute\simpleDispatcher;
use CrazyGoat\Router\DispatcherFactory;
use CrazyGoat\Router\RouteCollector;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response;
use Nyholm\Psr7Server\ServerRequestCreator;
Expand Down Expand Up @@ -56,24 +56,26 @@ protected function initResponseFactory(): void
}
}

private function initRouter()
private function initRouter(): void
{
if (!$this->container->has('router')) {
$dispatcher = simpleDispatcher(function(RouteCollector $r) {
return;
});
$dispatcher = DispatcherFactory::createFromClosure(
function (RouteCollector $r): void {
return;
}
);
$this->router = new Router($dispatcher);
}
}

private function initErrorHandler()
private function initErrorHandler(): void
{
if (!$this->container->has('errorHandler')) {
$this->errorHandler = new SimpleErrorHandler();
}
}

private function initResponseRenderer()
private function initResponseRenderer(): void
{
if (!$this->container->has('responseRenderer')) {
$this->renderer = new SimpleRenderer();
Expand Down
20 changes: 10 additions & 10 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ class Route implements RouteInterface
/**
* @var array
*/
private $attributes;
private $variables;

/**
* @var array
*/
private $middlewares;
private $middlewareStack;

public function __construct(
string $handler,
?string $name = null,
array $arguments = [],
array $middlewares = []
array $variables = [],
array $middlewareStack = []
) {
$this->name = $name;
$this->handler = $handler;
$this->attributes = $arguments;
$this->middlewares = $middlewares;
$this->variables = $variables;
$this->middlewareStack = $middlewareStack;
}

public function getName(): ?string
Expand All @@ -50,14 +50,14 @@ public function getHandler(): string
return $this->handler;
}

public function getMiddlewares(): array
public function getMiddlewareStack(): array
{
return $this->middlewares;
return $this->middlewareStack;
}


public function getAttributes(): array
public function getVariables(): array
{
return $this->attributes;
return $this->variables;
}
}
32 changes: 17 additions & 15 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use CrazyGoat\Core\Exceptions\RouteNotFound;
use CrazyGoat\Core\Interfaces\RouteInterface;
use CrazyGoat\Core\Interfaces\RouterInterface;
use CrazyGoat\Router\Dispatcher;
use CrazyGoat\Router\Exceptions\MethodNotAllowed as CrazyRouterMethodNotAllowed;
use CrazyGoat\Router\Exceptions\RouteNotFound as CrazyRouterRouteNotFound;
use CrazyGoat\Router\Interfaces\Dispatcher;
use Psr\Http\Message\ServerRequestInterface;

class Router implements RouterInterface
Expand All @@ -34,21 +36,21 @@ public function __construct(Dispatcher $dispatcher)
*/
public function dispatch(ServerRequestInterface $request): RouteInterface
{
$routeInfo = $this->dispatcher->dispatch(
$request->getMethod(), $request->getUri()->getPath()
);
try {
$routeInfo = $this->dispatcher->dispatch(
$request->getMethod(), $request->getUri()->getPath()
);

switch ($routeInfo[0]) {
case Dispatcher::FOUND:
return new Route(
$routeInfo[1], null, $routeInfo[2], $routeInfo[3]
);
break;
case Dispatcher::METHOD_NOT_ALLOWED:
case Dispatcher::NOT_FOUND:
default:
throw new RouteNotFound('Route not found');
break;
return new Route(
$routeInfo->getHandler(),
null,
$routeInfo->getVariables(),
$routeInfo->getMiddlewareStack()
);
} catch (CrazyRouterRouteNotFound $exception) {
throw new RouteNotFound('Route not found');
} catch (CrazyRouterMethodNotAllowed $exception) {
throw new RouteNotFound('Route not found');
}
}
}

0 comments on commit 05e5663

Please sign in to comment.