Skip to content

Commit

Permalink
Improve ease of extention
Browse files Browse the repository at this point in the history
In our usage we need to extend the ControllerInvoker to have responses
normalised from a generic Payload into a standard Response.

To make this easier it would be helpful if some private methods were
instead protected and the Invoker fetching the default resolver chain
were separated out from the creation of the ControllerInvoker.

Fixes #105
  • Loading branch information
andrewnicols committed Jun 19, 2024
1 parent 02ab027 commit dedf7f5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public static function create(?ContainerInterface $container = null): App
return $app;
}

private static function createControllerInvoker(ContainerInterface $container): ControllerInvoker
/**
* Create an invoker with the default resolvers.
*/
protected static function createInvoker(ContainerInterface $container): Invoker
{
$resolvers = [
// Inject parameters by name first
Expand All @@ -50,8 +53,14 @@ private static function createControllerInvoker(ContainerInterface $container):
new DefaultValueResolver,
];

$invoker = new Invoker(new ResolverChain($resolvers), $container);
return new Invoker(new ResolverChain($resolvers), $container);
}

return new ControllerInvoker($invoker);
/**
* Create a controller invoker with the default resolvers.
*/
protected static function createControllerInvoker(ContainerInterface $container): ControllerInvoker
{
return new ControllerInvoker(self::createInvoker($container));
}
}
21 changes: 18 additions & 3 deletions src/ControllerInvoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public function __construct(InvokerInterface $invoker)
* @param ServerRequestInterface $request The request object.
* @param ResponseInterface $response The response object.
* @param array $routeArguments The route's placeholder arguments
* @return ResponseInterface|string The response from the callable.
*/
public function __invoke(
callable $callable,
Expand All @@ -42,10 +41,26 @@ public function __invoke(
// Inject the attributes defined on the request
$parameters += $request->getAttributes();

return $this->invoker->call($callable, $parameters);
return $this->processResponse($this->invoker->call($callable, $parameters));
}

private static function injectRouteArguments(ServerRequestInterface $request, array $routeArguments): ServerRequestInterface
/**
* Allow for child classes to process the response.
*
* @param ResponseInterface|string $response The response from the callable.
* @return ResponseInterface|string The processed response
*/
protected function processResponse($response)
{
return $response;
}

/**
* Inject route arguments into the request.
*
* @param array $routeArguments
*/
protected static function injectRouteArguments(ServerRequestInterface $request, array $routeArguments): ServerRequestInterface
{
$requestWithArgs = $request;
foreach ($routeArguments as $key => $value) {
Expand Down

0 comments on commit dedf7f5

Please sign in to comment.