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

Use requestStack to access current request when needed. #3132

Merged
merged 3 commits into from
Mar 20, 2022

Conversation

vlad-ghita
Copy link
Contributor

Storing current request in constructor isn't a good idea.
In general, it works (eg: when request from browser or most tests).

But not always.
EG: when test extends WebTestCase and login is executed through $this->loginUser(), then container is built without an active request.
Thus, request is null in constructors ...

Proper fix implies storing $requestStack and accessing current request when required.

This PR fixes Canonical and FrontendMenu (these failed in my use-case, so far).

@bobdenotter
Copy link
Member

bobdenotter commented Mar 20, 2022

Hi @vlad-ghita,

At first I though there might be danger of infinite recursion here?

    public function getRequest(): Request
    {
        // always try to use current request
        if ($this->request === null) {
            $this->request = $this->requestStack->getCurrentRequest() ?? Request::createFromGlobals();
            $this->init();
        }

        return $this->request;
    }

    public function init(): void
    {
        // Ensure in request cycle (even for override).
        if ($this->getRequest() === null || $this->getRequest()->getHost() === '') {
            return;
        }

But, that's not true, because init will only be called from getRequest. After which the Request is set.

PHPstan is complaining about it, though.. About it "will always evaluate to false". And i think it's correct. It looks like we can simplify it like this, by removing the bit that's "always false"

    public function init(): void
    {
        // Ensure in request cycle (even for override).
        if ($this->getRequest()->getHost() === '') {
            return;
        }

Would that work for you?

}

public function init(): void
{
// Ensure in request cycle (even for override).
if ($this->request === null || $this->request->getHost() === '') {
if ($this->getRequest() === null || $this->getRequest()->getHost() === '') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix PHPStan's remark:

Strict comparison using === between Symfony\Component\HttpFoundation\Request and null will always evaluate to false.

Suggested change
if ($this->getRequest() === null || $this->getRequest()->getHost() === '') {
if ($this->getRequest()->getHost() === '') {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with possible infinite loop.
I aimed to keep init() for backwards compatibility.
But maybe we should simply ditch it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See latest changes.
I added a request setter.
Please re-run checks.

My tests passed.

Copy link
Member

@bobdenotter bobdenotter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now, thanks @vlad-ghita 👍

I'll merge in after tests are done!

@bobdenotter bobdenotter merged commit adfab2e into bolt:master Mar 20, 2022
@bobdenotter bobdenotter changed the title Use requestStack to access current request when needed. Use requestStack to access current request when needed. Mar 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants