Closed
Description
Symfony version(s) affected: all ?
Description
The PhpMatcherDumper::export
method creates invalid code when a route attribute is an object.
How to reproduce
In symfony repository, run :
php -r 'include __DIR__."/vendor/autoload.php"; class A { private $a; }; $export = \Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper::export(new A); echo "$export\n"; eval("${export};");'
And it will produce
A::__set_state(array('."\n".' 'a' => NULL,'."\n".'))
PHP Parse error: syntax error, unexpected ''a'' (T_CONSTANT_ENCAPSED_STRING), expecting ')' in Command line code(1) : eval()'d code on line 1
Possible Solution
A patch is available here and a pull request can be done.
The patch might break the formatting rules inside the export
method, and I don't know why it's there.
Additional context
I don't know if the patch is a good idea, because a lot of solutions exist to deal with the issue :
- serialize the object (using
serialize
orvar_export
) before setting the attribute to the route, thus setting it to a string value ; - use a reference and get the value in a cache pool or provider service ;
- the object creation at each request might not be a good idea for mem/cpu perfs ‒ that's why I preferred to use another way to get my object, only when needed.