Skip to content

Cache error while using anonymous class inside controller #30395

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

Closed
zim32 opened this issue Feb 27, 2019 · 10 comments
Closed

Cache error while using anonymous class inside controller #30395

zim32 opened this issue Feb 27, 2019 · 10 comments

Comments

@zim32
Copy link

zim32 commented Feb 27, 2019

Symfony version(s) affected: 4.2.3

Description
I got error Uncaught PHP Exception Symfony\Component\Cache\Exception\InvalidArgumentException: "Cache key "@anonymous" contains reserved characters {}()/@:
In my controller action method I am using anonymous class

...
$form = $this->createForm(SomeType::class, $this->getDataWrapper($data));
...
protected function getDataWrapper($data)
    {
        return new class ($data) {
            protected $data;

            public function __construct($data)
            {
                $this->data = $data;
            }

            public function getData() {
                return $this->data->getQuery()->getResult();
            }

            public function setData($data) {
                $this->data = $data;
            }
        };
    }

I was able to fix this by using ordinary, not anonymous class, but I think it should be fixed somehow

@xabbuh
Copy link
Member

xabbuh commented Feb 27, 2019

Are you able to create a small example application that allows to reproduce this issue? That would probably help a lot in identifying the root cause for this.

@zim32
Copy link
Author

zim32 commented Feb 27, 2019

The issue is specific. I was trying to provide data to form using anonymous class, which serves as a data wrapper. I think somewhere in form code, it is trying to work with cache, and throws this error. Only in prod env.

Please tell me if you need example application if you don't figure out how to reproduce.

@xabbuh
Copy link
Member

xabbuh commented Feb 27, 2019

Yes, please create such an example application.

@xabbuh xabbuh added the Form label Feb 27, 2019
@zim32
Copy link
Author

zim32 commented Feb 28, 2019

Unfortunately I have no time to create separate application.
The problem is that Cache system can not work with anonymous classes.

Maybe this is not a bug but just requirement not to use anonymous classes with cache system at all.

Screenshot
https://ibb.co/TRNPY26

@nicolas-grekas
Copy link
Member

Can you please share the full stack trace of the error?

@zim32
Copy link
Author

zim32 commented Feb 28, 2019

Yeah. But only in image format, sorry.
https://ibb.co/fkhZg5h

@xabbuh
Copy link
Member

xabbuh commented Mar 4, 2019

Just looking at the stack trace I don't know what causes this error or how to prevent it. Will you be able to provide an application that allows us to reproduce? Otherwise I suggest we close here as there isn't much we can do right now.

@zim32
Copy link
Author

zim32 commented Mar 5, 2019

Ok I'm closing due to very specific cache usage (anonymous classes)

@zim32 zim32 closed this as completed Mar 5, 2019
@nicolas-grekas
Copy link
Member

Reopening, I'd like to have a look.

@cadavre
Copy link

cadavre commented Mar 14, 2019

Confirmed this error. Happens when using anonymous class.

nicolas-grekas added a commit that referenced this issue Mar 15, 2019
This PR was merged into the 3.4 branch.

Discussion
----------

Fix Cache error while using anonymous class

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30395
| License       | MIT

Fix Cache error while using anonymous class

Commits
-------

036e722 Fix Cache error while using anonymous class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants