Skip to content

Commit

Permalink
[Breaking] Refactor PHP.ini management, remove php.setPhpIniPath() an…
Browse files Browse the repository at this point in the history
…d php.setPhpIniEntry() (#1423)

Refactors PHP.ini management:

* Removes the `php.setPhpIniEntry()` methods that only worked correctly
when called before initializing the PHP runtime.
* Hardcodes the php.ini path to `/internal/shared/php.ini`. The
developer is welcome to override that file as needed, but they can't
customize the path. Once changing the path becomes important, let's add
that feature at the [Boot
Protocol](#1398)
level.
* Provides `getPhpIniEntries()`, `setPhpIniEntries()`, and
`withPHPIniValues()` helpers that work at any point of the PHP runtime
lifecycle.
* Moves a list of the default php.ini values from `php_wasm.c` to
`base-php.ts` where it can be customized without rebuilding the wasm
module.

## What problem is it solving?

Working with php.ini is highly problematic as you could never be sure
whether your `setPhpIni*` call would be effective. Then, at the API
level, the `setPhpIni*` methods were also excluded from some TypeScript
types but not from the others.

## Remaining work

* Rebuild all the PHP modules

## Testing Instructions

Confirm the CI tests pass.
  • Loading branch information
adamziel authored May 20, 2024
1 parent feb875a commit 100bfe5
Show file tree
Hide file tree
Showing 76 changed files with 786 additions and 8,042 deletions.
40 changes: 37 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@
"private": true,
"dependencies": {
"@preact/signals-react": "1.3.6",
"@types/ini": "4.1.0",
"axios": "1.6.1",
"classnames": "^2.3.2",
"comlink": "^4.4.1",
"express": "4.19.2",
"file-saver": "^2.0.5",
"fs-extra": "11.1.1",
"ini": "4.1.2",
"octokit": "3.1.1",
"octokit-plugin-create-pull-request": "5.1.1",
"react": "^18.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ import TSDocstring from '@site/src/components/TSDocstring';

## Customizing PHP.ini

The API client also allows you to change the php.ini file. You can either set a single entry or replace the entire file:
The API client also allows you to change the php.ini file:

```ts
// Set a single entry...
await client.setPhpIniEntry('display_errors', 'On');

// ... or replace the entire php.ini file
await client.setPhpIniPath('/path/to/php.ini');
await setPhpIniEntries(client, {
display_errors: 'On',
error_reporting: 'E_ALL',
});
```

## Managing files and directories
Expand Down
1 change: 0 additions & 1 deletion packages/php-wasm/compile/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,6 @@ RUN set -euxo pipefail; \
"_emscripten_sleep", \n\
"_wasm_sleep", \n\
"_wasm_set_phpini_path", \n\
"_wasm_set_phpini_entries", \n\
"_wasm_add_SERVER_entry", \n\
"_wasm_add_ENV_entry", \n\
"_wasm_read", \n\
Expand Down
40 changes: 0 additions & 40 deletions packages/php-wasm/compile/php/php_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,25 +512,6 @@ static const zend_function_entry additional_functions[] = {
#define TSRMLS_FETCH()
#endif

// Lowest precedence ini rules. May be overwritten by a /usr/local/etc/php.ini file:
const char WASM_HARDCODED_INI[] =
"error_reporting = E_ALL\n"
"display_errors = 1\n"
"html_errors = 1\n"
"display_startup_errors = On\n"
"log_errors = 1\n"
"always_populate_raw_post_data = -1\n"
"upload_max_filesize = 2000M\n"
"post_max_size = 2000M\n"
"disable_functions = curl_exec,curl_multi_exec\n"
"allow_url_fopen = Off\n"
"allow_url_include = Off\n"
"session.save_path = /home/web_user\n"
"implicit_flush = 1\n"
"output_buffering = 0\n"
"max_execution_time = 0\n"
"max_input_time = -1\n\0";

typedef struct wasm_array_entry
{
char *key;
Expand Down Expand Up @@ -672,12 +653,6 @@ void wasm_set_phpini_path(char *path)
phpini_path_override = strdup(path);
}

char *additional_phpini_entries = NULL;
void wasm_set_phpini_entries(char *ini_entries)
{
free(additional_phpini_entries);
additional_phpini_entries = strdup(ini_entries);
}

void wasm_init_server_context()
{
Expand Down Expand Up @@ -1543,21 +1518,6 @@ int php_wasm_init()
php_wasm_sapi_module.php_ini_path_override = phpini_path_override;
}

if (additional_phpini_entries != NULL)
{
int ini_entries_len = strlen(additional_phpini_entries);
additional_phpini_entries = realloc(additional_phpini_entries, ini_entries_len + sizeof(WASM_HARDCODED_INI));
memmove(additional_phpini_entries + sizeof(WASM_HARDCODED_INI) - 2, additional_phpini_entries, ini_entries_len + 1);
memcpy(additional_phpini_entries, WASM_HARDCODED_INI, sizeof(WASM_HARDCODED_INI) - 2);
php_wasm_sapi_module.ini_entries = strdup(additional_phpini_entries);
free(additional_phpini_entries);
}
else
{
php_wasm_sapi_module.ini_entries = malloc(sizeof(WASM_HARDCODED_INI));
memcpy(php_wasm_sapi_module.ini_entries, WASM_HARDCODED_INI, sizeof(WASM_HARDCODED_INI));
}

php_sapi_started = 1;
php_wasm_sapi_module.additional_functions = additional_functions;
if (php_wasm_sapi_module.startup(&php_wasm_sapi_module) == FAILURE)
Expand Down
Loading

0 comments on commit 100bfe5

Please sign in to comment.