Skip to content

[PHP] Use a contextual SAPI name - #2424

Closed
adamziel wants to merge 7 commits into
trunkfrom
set-sapi-name
Closed

[PHP] Use a contextual SAPI name#2424
adamziel wants to merge 7 commits into
trunkfrom
set-sapi-name

Conversation

@adamziel

@adamziel adamziel commented Jul 25, 2025

Copy link
Copy Markdown
Collaborator

Motivation for the change, related issues

Uses a relevant SAPI name for the current execution context:

  • cli for CLI scripts
  • playground for everything else

We'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 be cli.

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 by php_sapi_name() function and the PHP_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 the PLAYGROUND_SAPI_NAME constant, and the set_sapi_name() function is always called in the auto_prepend_file script with the current PLAYGROUND_SAPI_NAME constant value.

Testing Instructions (or ideally a Blueprint)

@adamziel

Copy link
Copy Markdown
Collaborator Author

@johnbillion this PR will enable removing the recent query-monitor workaround

@adamziel
adamziel requested a review from a team as a code owner July 26, 2025 14:36
@adamziel
adamziel marked this pull request as draft August 8, 2025 22:36
@adamziel

Copy link
Copy Markdown
Collaborator Author

Since we're now capable of running the CLI SAPI and the embed SAPI as needed, this PR is no longer necessary. In fact, we should remove the setSapiName() semantics from Playground.

@adamziel adamziel closed this Sep 13, 2025
@nightnei

Copy link
Copy Markdown

@adamziel JFYI, in Studio we faced this issue and we are going to use the same workaround for consistency (with setting QM_TESTS as true).

I didn't get what it means -In fact, we should remove the setSapiName() semantics from Playground., by changing it to something non-cli? Since simply removing it seems not safe, as other plugins may expect to have some value of it.
Anyway, I am writing here just to ask - if one day you (the reader) remove workaround with QM_TESTS and fix setSapiName value - if it doesn't bother, let me or the YOLO team know so we can remove it from Studio as well. Thanks :)

ntwb added a commit to ntwb/studio that referenced this pull request Jul 28, 2026
Fix comment for Query Monitor plugin temporary fix:
- Introduced in c31863e via Automattic#2080 & WordPress/wordpress-playground#2424
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment