Skip to content

Commit

Permalink
Merge f70424e into 9ac9b63
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-caraiani committed Mar 29, 2018
2 parents 9ac9b63 + f70424e commit 793c703
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/Provider.php
Expand Up @@ -23,6 +23,7 @@ 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['globalrequest_middleware'] = $container->protect(function ($request, $response, $next) use ($container) {
Expand Down Expand Up @@ -79,6 +80,31 @@ protected function getSentry(Container $container): callable
};
}

/**
* Set controller() function into container.
*
* @param Container $container
*
* @return callable
*/
protected function setControllerLoader(Container $container): callable
{
return $container->protect(function (string $name) use ($container) {
$parts = \explode('_', $name);
$class = $container['config']('suit.controller');
foreach ($parts as $part) {
$class .= \ucfirst($part);
}
if (!$container->has('controller_'.$class)) {
$container['controller_'.$class] = function ($container) use ($class) {
return new $class($container);
};
}

return $container['controller_'.$class];
});
}

/**
* Set error handler with sentry.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/ProviderTest.php
Expand Up @@ -30,6 +30,12 @@ public function testSentryWithUserContext(): void
$this->assertEquals(['test' => 'user'], $this->container->sentry->context->user);
}

public function testControllerLoader(): void
{
$controller = $this->container['controller']('dummy_controller');
$this->assertInstanceOf('\Wtf\Root', $controller);
}

public function testErrorHander(): void
{
$middleware = $this->container->errorHandler;
Expand Down
1 change: 1 addition & 0 deletions tests/data/config/suit.php
Expand Up @@ -16,4 +16,5 @@
'dummy' => [
'has' => 'something',
],
'controller' => '\Wtf\Core\Tests\Dummy\\',
];
36 changes: 36 additions & 0 deletions tests/data/dummy/DummyController.php
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Wtf\Core\Tests\Dummy;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class DummyController extends \Wtf\Root
{
/**
* Invoke controller.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param array $args
*
* @return ResponseInterface
*/
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;
}
}

0 comments on commit 793c703

Please sign in to comment.