prooph / http-middleware

PSR compatible middleware to integrate prooph with a middleware dispatcher
http://getprooph.org/
BSD 3-Clause "New" or "Revised" License
11 stars 4 forks source link

ResponseStrategy should be context aware #2

Open basz opened 6 years ago

basz commented 6 years ago

From @oqq on September 19, 2016 9:5

Hi!

Currently i see no clean way to provide some context metadata to response strategy. As example it would be handy to use the request or the query that was dispatched to the query bus, to get the provided metadata.

final class HtmlResponseStrategy implements ResponseStrategy
{
    /** @var TemplateRendererInterface */
    private $templateRenderer;

    /**
     * @param TemplateRendererInterface $templateRenderer
     */
    public function __construct(TemplateRendererInterface $templateRenderer)
    {
        $this->templateRenderer = $templateRenderer;
    }

    /**
     * @inheritDoc
     */
    public function fromPromise(Promise $promise, array $context)
    {
        /** @var ServerRequestInterface $request */
        $request = $context['request'];
        $template = $request->getAttribute('template');

        $data = null;

        $promise->done(function($result) use (&$data) {
            $data = $result;
        });

        return new HtmlResponse(
            $this->templateRenderer->render($template),
            ['data' => $data]
        );
    }
}

Without $context i have create a abstract factory to inject the template name in the strategy, which would ends in a config nighmare.

Or am i missing a already existing solution for that issue? Some hints would to be welcome.

Thanks!

Copied from original issue: prooph/psr7-middleware#14

basz commented 6 years ago

From @sandrokeil on September 19, 2016 20:49

That's a good point. I guess we should look to issue #11 too. With this you could transform a JSON response to a HTML or XML response. But maybe there is a better solution. At the moment I'm busy, but I can think more about that in the beginning of october.

I would like to avoid an additional parameter to the fromPromise method. /cc @bweston92

basz commented 6 years ago

From @oqq on September 22, 2016 12:29

How about with a ResponseStrategyFactory?

$responseStrategy = $this->responseStrategyFactory->createStrategyFromRequest($request);

return $responseStrategy->fromPromise(
    $this->queryBus->dispatch($query)
);
basz commented 6 years ago

From @prolic on December 23, 2017 13:47

@oqq @basz @sandrokeil Please move this issue to https://github.com/prooph/http-middleware/