Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5d72a56

Browse files
committedJun 19, 2024··
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 5d72a56

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed
 

‎src/Bridge.php

+18-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ 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+
* @param ContainerInterface $container
46+
* @return Invoker
47+
*/
48+
protected static function createInvoker(ContainerInterface $container): Invoker
4349
{
4450
$resolvers = [
4551
// Inject parameters by name first
@@ -50,8 +56,17 @@ private static function createControllerInvoker(ContainerInterface $container):
5056
new DefaultValueResolver,
5157
];
5258

53-
$invoker = new Invoker(new ResolverChain($resolvers), $container);
59+
return new Invoker(new ResolverChain($resolvers), $container);
60+
}
5461

55-
return new ControllerInvoker($invoker);
62+
/**
63+
* Create a controller invoker with the default resolvers.
64+
*
65+
* @param ContainerInterface $container
66+
* @return ControllerInvoker
67+
*/
68+
protected static function createControllerInvoker(ContainerInterface $container): ControllerInvoker
69+
{
70+
return new ControllerInvoker(self::createInvoker($container));
5671
}
5772
}

‎src/ControllerInvoker.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,28 @@ public function __invoke(
4242
// Inject the attributes defined on the request
4343
$parameters += $request->getAttributes();
4444

45-
return $this->invoker->call($callable, $parameters);
45+
return $this->processResponse($this->invoker->call($callable, $parameters));
4646
}
4747

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

0 commit comments

Comments
 (0)
Please sign in to comment.