Skip to content

Instantly share code, notes, and snippets.

@texdc
Last active February 3, 2017 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save texdc/279a4ae82530a2bd9cfed56560ba5abc to your computer and use it in GitHub Desktop.
Save texdc/279a4ae82530a2bd9cfed56560ba5abc to your computer and use it in GitHub Desktop.
Higher-Order application building
<?php declare(strict_types=1);
namespace proj\http;
interface ApplicationInterface {
public function run();
public function use(callable $aMiddleware);
public function respondTo(string $anHttpMethod, string $aUriPath, callable $aResponder);
}
<?php declare(strict_types=1);
namespace proj\api\middleware;
use proj\html\TemplateManagerInterface as TemplateManager;
use const proj\http\STATUS_401;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
// $app->use(authenticate($config->get("api-key")));
function authenticate(string $anApiKey) : callable
{
return function (Request $aRequest, Response $aResponse, callable $aNextAction)
use ($anApiKey) : Response
{
if ($aRequest->getHeader("x-api-key") !== $anApiKey) {
return $aResponse->withStatusCode(STATUS_401);
}
return $aNextAction($aRequest, $aResponse);
};
}
// $app->use(format_as_json());
function format_as_json() : callable
{
return function (Request $aRequest, Response $aResponse, callable $aNextAction) : Response
{
if ($aRequest->getHeader("Content-Type") === "application/json") {
$body = $aResponse->getBody();
$aResponse->getBody()->write(json_encode($body));
}
return $aNextAction($aRequest, $aResponse);
};
}
// $app->use(format_as_html($services->get("template-manager")));
function format_as_html(TemplateManager $aTemplateManager) : callable
{
return function (Request $aRequest, Response $aResponse, callable $aNextAction)
use ($aTemplateManager) : Response
{
if ($aRequest->getHeader("Content-Type") === "text/html") {
$path = $aRequest->getUri()->getPath();
$renderer = $aTemplateManager->findForUriPath($path);
$aResponse = $renderer->render($aResponse));
}
return $aNextAction($aRequest, $aResponse);
};
}
<?php declare(strict_types=1);
namespace proj\api\route;
use proj\domain\user\ServiceInterface as UserService;
use const proj\domain\user\STATUS_ENABLED;
use const proj\domain\user\ORDER_BY_LASTNAME_ASC;
use proj\helper\PaginatorInterface as Paginator;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
// $app->respondTo("GET", "/users", list_users(
// $services->get("user-service"),
// $services->get("result-paginator"),
// $config->get("result-limit")
// ));
function list_users(UserService $aUserService, Paginator $aPaginator, int $aResultLimit) : callable
{
return function (Request $aRequest, Response $aResponse, callable $aNextAction)
use ($aUserService, $aPaginator, $aResultLimit) : Response
{
$status = $aRequest->getAttribute("status", STATUS_ENABLED);
$orderBy = $aRequest->getAttribute("order_by", ORDER_BY_LASTNAME_ASC);
$users = $aUserService->listAllWithStatus($status, $orderBy);
$page = $aRequest->getAttribute("page", 1);
$limit = $aRequest->getAttribute("limit", $aResultLimit);
$results = $aPaginator->paginate($users, $page, $limit);
$aResponse->getBody()->write($results);
return $aNextAction(null, $aResponse);
};
}
// $app->respondTo("GET", "/users/{id}", get_user($services->get("user-service")));
function get_user(UserService $aUserService) : callable
{
return function (Request $aRequest, Response $aResponse, callable $aNextAction)
use ($aUserService) : Response
{
$id = $aRequest->getAttribute("id");
$user = $aUserService->find($id);
$aResponse->getBody()->write($user);
return $aNextAction(null, $aResponse);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment