backdrop / backdrop-issues

Issue tracker for Backdrop core.
144 stars 40 forks source link

[DX] Use `token_get_all()` for parsing settings*.php files rather than using regex in `backdrop_rewrite_settings()` #6297

Open klonos opened 11 months ago

klonos commented 11 months ago

@yorkshire-pudding revived #2231 and has provided a PR for it that I started reviewing. It didn't take long to find out that our current methods of trying to parse and replace variables in settings.php files aren't robust enough to support certain things such as variable declarations that span across multiple lines. The regex's that we are using are making too many assumptions, which may not always be true, some of the following ones being the most obvious ones:

Anyway, the point is that a) we would end up with some very complex (and thus easy to break) regex's, and b) even then there are so many things that can go wrong with the assumptions we are making in backdrop_rewrite_settings(), since people might be manually editing their settings files in unpredictable ways. So I did some research trying to find a php parser that we could use, and luckily I came across token_get_all(), which is a native PHP function that uses the in-built Zend engine's lexical scanner, so:

I starting experimenting with it, and it looks very promising!

klonos commented 11 months ago
  • does most of the things we are after for free

Just to give a couple of examples re that:

() the returned array actually includes the integer values of these `T_constants instead of their names, but getting the respective constant/token name from this integer is easy to do viatoken_name()`.

yorkshire-pudding commented 1 month ago

@klonos - any progress on this?