[PHP] Use a contextual SAPI name #2424
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation for the change, related issues
Uses a relevant SAPI name for the current execution context:
cli
for CLI scriptsplayground
for everything elseWe've been struggling with the SAPI name for a while now. Some tools, like WP-CLI, expect the SAPI name to be
cli
– so we've hardcoded that. However, that broke other tools, such as the query-monitor plugin, that expect the SAPI name NOT to becli
.Before this PR we could only set the SAPI name once – before the first PHP code execution. With this PR, we can set it at any point.
Implementation details
This PR ships a
set_sapi_name( $new_name )
PHP function that updates the value returned byphp_sapi_name()
function and thePHP_SAPI
constant.Why a PHP runtime function? It's more convenient than controlling that from the TypeScript runtime via
php.setSapiName()
. We only need to maintain a single code path where we know the PHP runtime was already initialized. There's no need to check what if we're before the first code execution? Or what if we've trashed the runtime already? Or what do we do after the runtime hotswapping?The
.setSapiName()
now sets thePLAYGROUND_SAPI_NAME
constant, and theset_sapi_name()
function is always called in theauto_prepend_file
script with the currentPLAYGROUND_SAPI_NAME
constant value.Testing Instructions (or ideally a Blueprint)