How to configure ob_tidyhandler dynamically?

The PHP build extension has the ob_tidyhandler Docs function, which works with PHP buffering Docs output as a callback, for example:

 ob_start('ob_tidyhandler'); 

I know that Tidy has many Docs configuration settings , however I hit the road block to configure the default configuration settings for the output buffer callback.

Most of the Tidy PHP function relates to neat documents or objects, however with an output buffer that is not available.

There is a tidy.default_config configuration tidy.default_config , however it cannot be changed at runtime, therefore it is not very dynamic.

Does anyone know if it's possible to set up a callback at all? I remember that I wrote my own callback, collected all the output, and repaired it with a call to tidy_repair_string Docs , which takes the configuration as an array. But I thought it would be nice not to do this and just pass the configuration to the output buffer.

+7
source share
1 answer

I quickly looked through the code for a neat extension, and there is a way to change the configuration of the output buffer handler.

However, this is not very good. It uses a code function, which, obviously, may change in future versions.

The function that processes the buffer processing, php_tidy_output_handler and line 1191 , calls the macro TIDY_SET_DEFAULT_CONFIG .

If tidy.default_config not set, the macro does nothing. If it is installed, the configuration file is read from the disk and the parameters are analyzed.

Since the configuration file is loaded while parsing the output buffer, before parsing can begin, you can modify the configuration file from your PHP script.

This means that you must do tidy.default_config = /file/writable/by/php and dynamically update this file to contain the parameters you need. (I told you this is not very good).


I immediately see a problem with this. There is a potential race condition.

If two scripts require different parameters, and both of them are executed simultaneously, there is a possibility that one of the scripts will get the wrong configuration.

The file is clearly not intended to change ad-hocly - and, as you can probably follow from the extension code, there is no other way to enter configuration parameters, since the parameters are document-specific.

  • Created a new TidyDoc.
  • A pair of set flags config + default_config loaded.
  • The buffer is parsed.

Sorry, it seems that in the end I'm just delivering bad news.

A better solution might be to call the ob_start callback, where you have full control over the parameters of the document.

Edit:

There was a little brainstorming and tried a few things to get around this. Everything was completed with an error.

I tried registering a custom thread wrapper to return script values ​​and set tidy.default_config = tidy://config . It turns out that stream wrappers are not allowed by the configuration loader, and this does not work.

One thing that I could not verify correctly is the configuration for each .user.ini directory or [PATH=] ini. They are available only with CGI / FastCGI SAPI (not FPM). I suspect that this probably will not help you either.

+10
source

All Articles