diff --git a/docs/response-constructor.md b/docs/response-constructor.md index 2b897d0..57e7bf8 100644 --- a/docs/response-constructor.md +++ b/docs/response-constructor.md @@ -8,7 +8,7 @@ This is the interface for the response constructor: interface ResponseConstructorInterface { /** @param mixed $result */ - public function constructResponse($result): Response; + public function constructResponse($result, Request $request): Response; } ``` @@ -17,14 +17,14 @@ interface ResponseConstructorInterface Most of the time the result will be an object or array and be converted into JSON throught the JSONResponseConstructor. Obviously it needs the custom normalizers for the values objects to be able to do so. ```php -final class JsonResponseConstructor implements ResponseConstructorInterface +final class SerializerJsonResponseConstructor implements ResponseConstructorInterface { public function __construct( private SerializerInterface $serializer, ) { } - public function constructResponse($data): JsonResponse + public function constructResponse($data, Request $request): JsonResponse { return new JsonResponse( $this->serializer->serialize($data), @@ -44,7 +44,7 @@ A file should be returned as a binary and not as JSON, so we would need a custom final class FileResponseConstructor implements ResponseConstructorInterface { /** @param File $data */ - public function constructResponse($data): Response + public function constructResponse($data, Request $request): Response { return FileManagementHelper::binaryResponse( $data->fileContent, @@ -107,7 +107,7 @@ Such a callable would be send to a simple streamed response constructor: final class StreamedResponseConstructor implements ResponseConstructorInterface { /** @param callable $data */ - public function constructResponse($data): Response + public function constructResponse($data, Request $request): Response { return new StreamedResponse($data); } diff --git a/src/Controller/CommandController.php b/src/Controller/CommandController.php index 21f1596..e1cd0d9 100644 --- a/src/Controller/CommandController.php +++ b/src/Controller/CommandController.php @@ -133,6 +133,6 @@ public function handle( // Construct and return response $responseConstructor = $this->serviceMap->getResponseConstructor($configuration, $this->defaultResponseConstructorClass); - return $responseConstructor->constructResponse(null); + return $responseConstructor->constructResponse(null, $request); } } diff --git a/src/Controller/QueryController.php b/src/Controller/QueryController.php index e4998d0..195ff15 100644 --- a/src/Controller/QueryController.php +++ b/src/Controller/QueryController.php @@ -137,6 +137,6 @@ public function handle( // Construct and return response $responseConstructor = $this->serviceMap->getResponseConstructor($configuration, $this->defaultResponseConstructorClass); - return $responseConstructor->constructResponse($result); + return $responseConstructor->constructResponse($result, $request); } } diff --git a/src/ResponseConstructor/EmptyJsonResponseConstructor.php b/src/ResponseConstructor/EmptyJsonResponseConstructor.php index fcd1b47..0ad76b8 100644 --- a/src/ResponseConstructor/EmptyJsonResponseConstructor.php +++ b/src/ResponseConstructor/EmptyJsonResponseConstructor.php @@ -5,11 +5,12 @@ namespace DigitalCraftsman\CQRS\ResponseConstructor; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; final class EmptyJsonResponseConstructor implements ResponseConstructorInterface { - public function constructResponse($data): JsonResponse + public function constructResponse($data, Request $request): JsonResponse { return new JsonResponse('', Response::HTTP_NO_CONTENT, [], true); } diff --git a/src/ResponseConstructor/EmptyResponseConstructor.php b/src/ResponseConstructor/EmptyResponseConstructor.php index bf9c6f6..91ee736 100644 --- a/src/ResponseConstructor/EmptyResponseConstructor.php +++ b/src/ResponseConstructor/EmptyResponseConstructor.php @@ -4,11 +4,12 @@ namespace DigitalCraftsman\CQRS\ResponseConstructor; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; final class EmptyResponseConstructor implements ResponseConstructorInterface { - public function constructResponse($data): Response + public function constructResponse($data, Request $request): Response { return new Response(null, Response::HTTP_NO_CONTENT); } diff --git a/src/ResponseConstructor/ResponseConstructorInterface.php b/src/ResponseConstructor/ResponseConstructorInterface.php index 5a86e27..5ea8399 100644 --- a/src/ResponseConstructor/ResponseConstructorInterface.php +++ b/src/ResponseConstructor/ResponseConstructorInterface.php @@ -4,10 +4,11 @@ namespace DigitalCraftsman\CQRS\ResponseConstructor; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; interface ResponseConstructorInterface { /** @param ?mixed $data */ - public function constructResponse($data): Response; + public function constructResponse($data, Request $request): Response; }