Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResponseStrategy should be context aware #2

Open
basz opened this issue Dec 23, 2017 · 3 comments
Open

ResponseStrategy should be context aware #2

basz opened this issue Dec 23, 2017 · 3 comments

Comments

@basz
Copy link
Member

basz commented Dec 23, 2017

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
Copy link
Member Author

basz commented Dec 23, 2017

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
Copy link
Member Author

basz commented Dec 23, 2017

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
Copy link
Member Author

basz commented Dec 23, 2017

From @prolic on December 23, 2017 13:47

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant