Skip to content

Commit

Permalink
Added default router, implements #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Chernyi committed Apr 3, 2018
1 parent 986e2aa commit 134a082
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 68 deletions.
4 changes: 1 addition & 3 deletions src/App.php
Expand Up @@ -55,9 +55,7 @@ public function __construct($container = [])
public function run($silent = false)
{
//Allow routing on application level via invokable class
if ($this->getContainer()->has('app_router')) {
$this->getContainer()['app_router']($this);
}
$this->getContainer()['app_router']($this);

return parent::run($silent);
}
Expand Down
45 changes: 13 additions & 32 deletions src/Provider.php
Expand Up @@ -20,12 +20,15 @@ class Provider implements ServiceProviderInterface
*/
public function register(Container $container): void
{
$container['suit_config'] = $this->getSuitConfig();
$container['config'] = $this->getConfig($container);
$container['sentry'] = $this->getSentry($container);
$container['controller'] = $this->setControllerLoader($container);
$container['errorHandler'] = $this->setErrorHandler($container);
$container['phpErrorHandler'] = $this->setErrorHandler($container);
$container['suit_config'] = function ($c) {
return new Config($c);
};
$container['config'] = $container->protect(function (string $string, $default = null) use ($container) {
return $container['suit_config']->__invoke($string, $default);
});
$container['app_router'] = function ($c) {
return new Router($c);
};
$container['globalrequest_middleware'] = $container->protect(function ($request, $response, $next) use ($container) {
if ($container->has('request')) {
unset($container['request']);
Expand All @@ -34,32 +37,10 @@ public function register(Container $container): void

return $next($request, $response);
});
}

/**
* Prepare config object.
*
* @return callable
*/
protected function getSuitConfig(): callable
{
return function ($c) {
return new Config($c);
};
}

/**
* Add 'shortcut' to suit config __invoke().
*
* @param Container $container
*
* @return callable
*/
protected function getConfig(Container $container): callable
{
return $container->protect(function (...$args) use ($container) {
return \call_user_func_array($container['suit_config'], $args);
});
$container['sentry'] = $this->getSentry($container);
$container['controller'] = $this->setControllerLoader($container);
$container['errorHandler'] = $this->setErrorHandler($container);
$container['phpErrorHandler'] = $this->setErrorHandler($container);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/Router.php
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Wtf;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

/**
* Application level router.
*/
class Router extends \Wtf\Root
{
/**
* Map routes from `routes.php` config.
*
* @param \Slim\App $app
*/
public function __invoke(\Slim\App $app): void
{
foreach ($this->config('routes') as $group_name => $routes) {
$app->group($group_name, function () use ($group_name, $routes): void {
$controller = ('/' === $group_name || !$group_name) ? 'index' : \trim($group_name, '/');

foreach ($routes as $name => $route) {
$methods = $route['methods'] ?? ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'];
$pattern = $route['pattern'] ?? '';
$callable = function (Request $request, Response $response, array $args = []) use ($controller, $route) {
return $this['controller']($controller)->__invoke($request, $response, $args);
};
$this->map($methods, $pattern, $callable)->setName($controller.'-'.$name);
}
});
}
}
}
1 change: 1 addition & 0 deletions tests/AppTest.php
Expand Up @@ -44,6 +44,7 @@ public function testRun(): void
//test app_router
foreach ($app->getContainer()->get('router')->getRoutes() as $route) {
$this->assertAttributeContains('/test/route', 'pattern', $route);
$this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $route($app->getContainer()->request, $app->getContainer()->response));
}

$appRouter = $app->getContainer()->get('app_router');
Expand Down
5 changes: 5 additions & 0 deletions tests/ProviderTest.php
Expand Up @@ -17,6 +17,11 @@ protected function setUp(): void
$this->container = $app->getContainer();
}

public function testAppRouter(): void
{
$this->assertInstanceOf('\Wtf\Router', $this->container->app_router);
}

public function testSentryWithoutUserContext(): void
{
$this->assertInstanceOf('\Raven_Client', $this->container->sentry);
Expand Down
10 changes: 9 additions & 1 deletion tests/data/config/routes.php
Expand Up @@ -3,5 +3,13 @@
declare(strict_types=1);

return [
'/test/route' => [],
'DummyController' => [
'test_route' => [
'pattern' => '/test/route',
'rbac' => [
'admin' => 'get',
'user' => 'post',
],
],
],
];
11 changes: 0 additions & 11 deletions tests/data/dummy/DummyController.php
Expand Up @@ -20,17 +20,6 @@ class DummyController extends \Wtf\Root
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$this->request = $request;
$this->response = $response;
$name = \explode('-', $request->getAttribute('route')->getName());
$action = \strtolower(\end($name)).'Action';

if (\method_exists($this, $action)) {
$response = \call_user_func([$this, $action]);
} else {
$response = $this->notFoundHandler($request, $response);
}

return $response;
}
}
4 changes: 0 additions & 4 deletions tests/data/dummy/Provider.php
Expand Up @@ -15,10 +15,6 @@ public function register(\Pimple\Container $container): void
return 'Hello, world!';
};

$container['app_router'] = function ($c) {
return new Router($c);
};

$container['example_middleware'] = $container->protect(function ($request, $response, $next) use ($container) {
return $next($request, $response);
});
Expand Down
17 changes: 0 additions & 17 deletions tests/data/dummy/Router.php

This file was deleted.

0 comments on commit 134a082

Please sign in to comment.