Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Implemented #19 (optimized interaction with pathfinder) #22

Merged
merged 1 commit into from
Dec 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"twig/twig": "v1.23.1",
"willdurand/negotiation": "v2.0.1",
"bitexpert/slf4psrlog": "^0.1.0",
"bitexpert/pathfinder": "^0.2.0"
"bitexpert/pathfinder": "^0.3.0"
},
"require-dev": {
"phpunit/php-code-coverage": "^2.1.0",
Expand Down
55 changes: 28 additions & 27 deletions composer.lock

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

8 changes: 3 additions & 5 deletions src/bitExpert/Adroit/Action/Resolver/ActionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
*/
namespace bitExpert\Adroit\Action\Resolver;

use Psr\Http\Message\ServerRequestInterface;

/**
* An action resolver will provide a {@link \bitExpert(Adriot\Action\Action} instance
* for the given $actionToken.
Expand All @@ -21,12 +19,12 @@
interface ActionResolver
{
/**
* Creates and returns an action object from the given {@link \Psr\Http\Message\ServerRequestInterface}.
* Creates and returns an action object using the given $actionToken.
* If no matching {@link \bitExpert\Adroit\Action\Action} instance could be found, null will be
* returned.
*
* @param ServerRequestInterface $request
* @param string $actionToken
* @return \bitExpert\Adroit\Action\Action|null
*/
public function resolve(ServerRequestInterface $request);
public function resolve($actionToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,23 @@ class ContainerAwareActionResolver implements ActionResolver
* @var \Psr\Log\LoggerInterface the logger instance.
*/
protected $logger;
/**
* @var string The request attribute to find the target in
*/
protected $targetRequestAttribute;

/**
* Creates a new {@link \bitExpert\Adroit\Action\Resolver\ContainerAwareActionResolver}.
*
* @param ContainerInterface $container
* @param string $targetRequestAttribute
*/
public function __construct(ContainerInterface $container, $targetRequestAttribute)
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->targetRequestAttribute = $targetRequestAttribute;
$this->logger = LoggerFactory::getLogger(__CLASS__);
}

/**
* {@inheritDoc}
*/
public function resolve(ServerRequestInterface $request)
public function resolve($actionToken)
{
$actionToken = $request->getAttribute($this->targetRequestAttribute);
if (!$this->container->has($actionToken)) {
$this->logger->error(
sprintf(
Expand Down
46 changes: 37 additions & 9 deletions src/bitExpert/Adroit/AdroitMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use bitExpert\Adroit\Responder\Resolver\ResponderResolver;
use bitExpert\Adroit\Responder\Responder;
use bitExpert\Pathfinder\Router;
use bitExpert\Pathfinder\RoutingResult;
use bitExpert\Slf4PsrLog\LoggerFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -71,8 +72,21 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
{
$this->logger->debug('Handling new request...');

$request = $this->resolveActionToken($request);
$action = $this->resolveAction($request);
$result = $this->resolveRoutingResult($request);

if (!$result->hasTarget()) {
$message = 'No action could be determined for the current request';
$response->getBody()->rewind();
$response->getBody()->write($message);
$response->getBody()->rewind();
$response = $response->withStatus(404);
return $response;
}

$action = $this->resolveAction($result->getTarget());

// inject params determined by the router into the request
$request = $this->injectParams($request, $result->getParams());

// execute the action
$responseOrPayload = $action->prepareAndExecute($request, $response);
Expand Down Expand Up @@ -107,26 +121,40 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
}

/**
* Resolves the actionToken from the given $request.
* Lets perform the router and returns a {@link \bitExpert\Pathfinder\RoutingResult} for the given $request.
*
* @param ServerRequestInterface $request
* @return ServerRequestInterface
* @return RoutingResult
*/
protected function resolveActionToken(ServerRequestInterface $request)
protected function resolveRoutingResult(ServerRequestInterface $request)
{
return $this->router->match($request);
}

/**
* Injects given params to the given request and returns a new {@link \Psr\Http\Message\ServerRequestInterface}
*
* @param ServerRequestInterface $request
* @param array $params
* @return ServerRequestInterface
*/
protected function injectParams(ServerRequestInterface $request, array $params = [])
{
$params = array_merge($request->getQueryParams(), $params);
// setting given params as query params
return $request->withQueryParams($params);
}

/**
* Tries to resolve an {@link \bitExpert\Adroit\Action\Action} instance by querying
* the $actionResolvers. In case no matching action instance could be found an
* exception gets thrown.
*
* @param ServerRequestInterface $request
* @param $actionToken
* @return Action
* @throws RuntimeException
*/
protected function resolveAction(ServerRequestInterface $request)
protected function resolveAction($actionToken)
{
$this->logger->debug('Trying to resolve action...');

Expand All @@ -141,7 +169,7 @@ protected function resolveAction(ServerRequestInterface $request)
continue;
}

$action = $resolver->resolve($request);
$action = $resolver->resolve($actionToken);
if ($action instanceof Action) {
// step out of the loop when an action could be found
// by the resolver. First resolver wins!
Expand All @@ -154,7 +182,7 @@ protected function resolveAction(ServerRequestInterface $request)
}
}

$message = 'An action could not be resolved from the given request!';
$message = 'An action could not be resolved for the given token!';
$this->logger->error($message);
throw new RuntimeException($message);
}
Expand Down
Loading