Skip to content

Commit dedf7f5

Browse files
committed
Improve ease of extention
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
1 parent 02ab027 commit dedf7f5

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/Bridge.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public static function create(?ContainerInterface $container = null): App
3939
return $app;
4040
}
4141

42-
private static function createControllerInvoker(ContainerInterface $container): ControllerInvoker
42+
/**
43+
* Create an invoker with the default resolvers.
44+
*/
45+
protected static function createInvoker(ContainerInterface $container): Invoker
4346
{
4447
$resolvers = [
4548
// Inject parameters by name first
@@ -50,8 +53,14 @@ private static function createControllerInvoker(ContainerInterface $container):
5053
new DefaultValueResolver,
5154
];
5255

53-
$invoker = new Invoker(new ResolverChain($resolvers), $container);
56+
return new Invoker(new ResolverChain($resolvers), $container);
57+
}
5458

55-
return new ControllerInvoker($invoker);
59+
/**
60+
* Create a controller invoker with the default resolvers.
61+
*/
62+
protected static function createControllerInvoker(ContainerInterface $container): ControllerInvoker
63+
{
64+
return new ControllerInvoker(self::createInvoker($container));
5665
}
5766
}

src/ControllerInvoker.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public function __construct(InvokerInterface $invoker)
2424
* @param ServerRequestInterface $request The request object.
2525
* @param ResponseInterface $response The response object.
2626
* @param array $routeArguments The route's placeholder arguments
27-
* @return ResponseInterface|string The response from the callable.
2827
*/
2928
public function __invoke(
3029
callable $callable,
@@ -42,10 +41,26 @@ public function __invoke(
4241
// Inject the attributes defined on the request
4342
$parameters += $request->getAttributes();
4443

45-
return $this->invoker->call($callable, $parameters);
44+
return $this->processResponse($this->invoker->call($callable, $parameters));
4645
}
4746

48-
private static function injectRouteArguments(ServerRequestInterface $request, array $routeArguments): ServerRequestInterface
47+
/**
48+
* Allow for child classes to process the response.
49+
*
50+
* @param ResponseInterface|string $response The response from the callable.
51+
* @return ResponseInterface|string The processed response
52+
*/
53+
protected function processResponse($response)
54+
{
55+
return $response;
56+
}
57+
58+
/**
59+
* Inject route arguments into the request.
60+
*
61+
* @param array $routeArguments
62+
*/
63+
protected static function injectRouteArguments(ServerRequestInterface $request, array $routeArguments): ServerRequestInterface
4964
{
5065
$requestWithArgs = $request;
5166
foreach ($routeArguments as $key => $value) {

0 commit comments

Comments
 (0)