Skip to content

Commit

Permalink
Move twig render method to Responder
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed Apr 18, 2020
1 parent 8f3ed27 commit a6b5eb8
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 50 deletions.
14 changes: 7 additions & 7 deletions src/Action/Home/HomeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

namespace App\Action\Home;

use App\Responder\Responder;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Views\Twig;

/**
* Action.
*/
final class HomeAction
{
/**
* @var Twig
* @var Responder
*/
private $twig;
private $responder;

/**
* The constructor.
*
* @param Twig $twig The twig engine
* @param Responder $responder The responder
*/
public function __construct(Twig $twig)
public function __construct(Responder $responder)
{
$this->twig = $twig;
$this->responder = $responder;
}

/**
Expand All @@ -40,6 +40,6 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
'now' => date('d.m.Y H:i:s'),
];

return $this->twig->render($response, 'home/home.twig', $viewData);
return $this->responder->render($response, 'home/home.twig', $viewData);
}
}
14 changes: 7 additions & 7 deletions src/Action/Login/LoginAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

namespace App\Action\Login;

use App\Responder\Responder;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Views\Twig;

/**
* Action.
*/
final class LoginAction
{
/**
* @var Twig
* @var Responder
*/
private $twig;
private $responder;

/**
* The constructor.
*
* @param Twig $twig The twig engine
* @param Responder $responder The responder
*/
public function __construct(Twig $twig)
public function __construct(Responder $responder)
{
$this->twig = $twig;
$this->responder = $responder;
}

/**
Expand All @@ -36,6 +36,6 @@ public function __construct(Twig $twig)
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
return $this->twig->render($response, 'login/login.twig');
return $this->responder->render($response, 'login/login.twig');
}
}
2 changes: 1 addition & 1 deletion src/Action/Login/LoginSubmitAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
$url = 'login';
}

return $this->responder->redirect($request, $response, $url);
return $this->responder->redirect($response, $url);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Action/Login/LogoutAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
// Logout user
$this->session->invalidate();

return $this->responder->redirect($request, $response, 'login');
return $this->responder->redirect($response, 'login');
}
}
2 changes: 1 addition & 1 deletion src/Action/User/UserCreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
$userId = $this->userCreator->createUser($userData);

// Build the HTTP response
return $this->responder->encodeJson($response, [
return $this->responder->json($response, [
'user_id' => $userId,
]);
}
Expand Down
14 changes: 7 additions & 7 deletions src/Action/User/UserListAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

namespace App\Action\User;

use App\Responder\Responder;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Views\Twig;

/**
* Action.
*/
final class UserListAction
{
/**
* @var Twig
* @var Responder
*/
private $twig;
private $responder;

/**
* The constructor.
*
* @param Twig $twig The twig engine
* @param Responder $responder The responder
*/
public function __construct(Twig $twig)
public function __construct(Responder $responder)
{
$this->twig = $twig;
$this->responder = $responder;
}

/**
Expand All @@ -36,6 +36,6 @@ public function __construct(Twig $twig)
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
return $this->twig->render($response, 'user/user-list.twig');
return $this->responder->render($response, 'user/user-list.twig');
}
}
2 changes: 1 addition & 1 deletion src/Action/User/UserListDataTableAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
{
$params = (array)$request->getParsedBody();

return $this->responder->encodeJson($response, $this->userListDataTable->listAllUsers($params));
return $this->responder->json($response, $this->userListDataTable->listAllUsers($params));
}
}
14 changes: 7 additions & 7 deletions src/Action/User/UserViewAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace App\Action\User;

use App\Domain\User\Service\UserViewer;
use App\Responder\Responder;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Views\Twig;

/**
* Action.
Expand All @@ -18,20 +18,20 @@ final class UserViewAction
private $userReader;

/**
* @var Twig
* @var Responder
*/
private $twig;
private $responder;

/**
* The constructor.
*
* @param UserViewer $userViewer The service
* @param Twig $twig The twig engine
* @param Responder $responder The responder
*/
public function __construct(UserViewer $userViewer, Twig $twig)
public function __construct(UserViewer $userViewer, Responder $responder)
{
$this->userReader = $userViewer;
$this->twig = $twig;
$this->responder = $responder;
}

/**
Expand Down Expand Up @@ -60,6 +60,6 @@ public function __invoke(
];

// Render the twig template with the given view data
return $this->twig->render($response, 'user/user-view.twig', $viewData);
return $this->responder->render($response, 'user/user-view.twig', $viewData);
}
}
2 changes: 1 addition & 1 deletion src/Middleware/UserAuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}

// User is not logged in. Redirect to login page.
return $this->responder->redirect($request, $this->responder->createResponse(), 'login');
return $this->responder->redirect($this->responder->createResponse(), 'login');
}
}
71 changes: 57 additions & 14 deletions src/Responder/Responder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@

namespace App\Responder;

use App\Routing\UrlGenerator;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Routing\RouteContext;
use Slim\Views\Twig;
use UnexpectedValueException;

/**
* A generic responder.
*/
final class Responder
{
/**
* @var Twig
*/
private $twig;

/**
* @var UrlGenerator
*/
private $urlGenerator;

/**
* @var ResponseFactoryInterface
*/
Expand All @@ -21,10 +31,17 @@ final class Responder
/**
* The constructor.
*
* @param Twig $twig The twig engine
* @param UrlGenerator $urlGenerator The url generator
* @param ResponseFactoryInterface $responseFactory The response factory
*/
public function __construct(ResponseFactoryInterface $responseFactory)
{
public function __construct(
Twig $twig,
UrlGenerator $urlGenerator,
ResponseFactoryInterface $responseFactory
) {
$this->twig = $twig;
$this->urlGenerator = $urlGenerator;
$this->responseFactory = $responseFactory;
}

Expand All @@ -38,13 +55,26 @@ public function createResponse(): ResponseInterface
return $this->responseFactory->createResponse()->withHeader('Content-Type', 'text/html; charset=utf-8');
}

/**
* Output rendered template.
*
* @param ResponseInterface $response The response
* @param string $template Template pathname relative to templates directory
* @param array $data Associative array of template variables
*
* @return ResponseInterface The response
*/
public function render(ResponseInterface $response, string $template, array $data = []): ResponseInterface
{
return $this->twig->render($response, $template, $data);
}

/**
* Creates a redirect for the given url / route name.
*
* This method prepares the response object to return an HTTP Redirect
* response to the client.
*
* @param ServerRequestInterface $request The request
* @param ResponseInterface $response The response
* @param string $destination The redirect destination (url or route name)
* @param array<mixed> $data Named argument replacement data
Expand All @@ -53,16 +83,13 @@ public function createResponse(): ResponseInterface
* @return ResponseInterface The response
*/
public function redirect(
ServerRequestInterface $request,
ResponseInterface $response,
string $destination,
array $data = [],
array $queryParams = []
): ResponseInterface {
if (!filter_var($destination, FILTER_VALIDATE_URL)) {
$destination = RouteContext::fromRequest($request)
->getRouteParser()
->urlFor($destination, $data, $queryParams);
$destination = $this->urlGenerator->fullUrlFor($destination, $data, $queryParams);
}

return $response->withStatus(302)->withHeader('Location', $destination);
Expand All @@ -83,21 +110,37 @@ public function redirect(
*
* @return ResponseInterface The response
*/
public function encodeJson(
public function json(
ResponseInterface $response,
$data,
int $options = 0,
int $depth = 512
): ResponseInterface {
$response = $response->withHeader('Content-Type', 'application/json');
$response->getBody()->write($this->encodeJson($data, $options, $depth));

return $response;
}

/**
* Encode data to json string.
*
* @param mixed $data The data
* @param int $options Json encoding options
* @param int $depth Json encoding max depth
*
* @throws UnexpectedValueException
*
* @return string The json string
*/
private function encodeJson($data, int $options = 0, int $depth = 512): string
{
$json = (string)json_encode($data, $options, $depth);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new UnexpectedValueException(json_last_error_msg(), json_last_error());
}

$response = $response->withHeader('Content-Type', 'application/json');
$response->getBody()->write($json);

return $response;
return $json;
}
}
6 changes: 3 additions & 3 deletions tests/TestCase/Responder/ResponderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testEncodeJson(): void
{
$responder = $this->getContainer()->get(Responder::class);

$response = $responder->encodeJson(new Response(), ['success' => true]);
$response = $responder->json(new Response(), ['success' => true]);

static::assertSame('{"success":true}', (string)$response->getBody());
static::assertSame('application/json', $response->getHeaderLine('Content-Type'));
Expand All @@ -40,7 +40,7 @@ public function testRedirectUrl(): void
$responder = $this->getContainer()->get(Responder::class);

$request = $this->createRequest('GET', '/');
$response = $responder->redirect($request, new Response(), 'https://www.example.com/');
$response = $responder->redirect(new Response(), 'https://www.example.com/');

static::assertSame(302, $response->getStatusCode());
static::assertSame('https://www.example.com/', $response->getHeaderLine('Location'));
Expand All @@ -58,7 +58,7 @@ public function testRedirectRouteName(): void
$responder = $this->getContainer()->get(Responder::class);

$app->get('/foo', function ($request, $response) use ($responder) {
return $responder->redirect($request, $response, 'foo');
return $responder->redirect($response, 'foo');
})->setName('foo');

$request = $this->createRequest('GET', '/foo');
Expand Down

0 comments on commit a6b5eb8

Please sign in to comment.