Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions packages/php-wasm/node/src/lib/extensions/intl/with-intl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ export async function withIntl(
version: SupportedPHPVersion = LatestSupportedPHPVersion,
options: EmscriptenOptions
): Promise<EmscriptenOptions> {
/*
* The Intl extension is hard-coded to look for the `icudt74l` filename,
* which means the ICU data file must use that exact name.
*/
const dataName = 'icudt74l.dat';
const dataPath = `${__dirname}/shared/icu.dat`;
const ICUData = fs.readFileSync(dataPath);

const extensionName = 'intl.so';
const extensionPath = await getIntlExtensionModule(version);
const extension = fs.readFileSync(extensionPath);

const dataName = 'icu.dat';
const dataPath = `${__dirname}/shared/${dataName}`;
const ICUData = fs.readFileSync(dataPath);

return {
...options,
ENV: {
...options.ENV,
PHP_INI_SCAN_DIR: '/internal/shared/extensions',
ICU_DATA: '/internal/shared',
PHP_INI_SCAN_DIR: '/internal/private/extensions',
ICU_DATA: '/internal/private',
},
onRuntimeInitialized: (phpRuntime: PHPRuntime) => {
if (options.onRuntimeInitialized) {
Expand All @@ -37,19 +41,19 @@ export async function withIntl(
if (
!FSHelpers.fileExists(
phpRuntime.FS,
'/internal/shared/extensions'
phpRuntime.ENV.PHP_INI_SCAN_DIR
)
) {
phpRuntime.FS.mkdirTree('/internal/shared/extensions');
phpRuntime.FS.mkdirTree(phpRuntime.ENV.PHP_INI_SCAN_DIR);
}
if (
!FSHelpers.fileExists(
phpRuntime.FS,
`/internal/shared/extensions/${extensionName}`
`${phpRuntime.ENV.PHP_INI_SCAN_DIR}/${extensionName}`
)
) {
phpRuntime.FS.writeFile(
`/internal/shared/extensions/${extensionName}`,
`${phpRuntime.ENV.PHP_INI_SCAN_DIR}/${extensionName}`,
new Uint8Array(extension)
);
}
Expand All @@ -59,25 +63,22 @@ export async function withIntl(
if (
!FSHelpers.fileExists(
phpRuntime.FS,
'/internal/shared/extensions/intl.ini'
`${phpRuntime.ENV.PHP_INI_SCAN_DIR}/intl.ini`
)
) {
phpRuntime.FS.writeFile(
'/internal/shared/extensions/intl.ini',
`${phpRuntime.ENV.PHP_INI_SCAN_DIR}/intl.ini`,
[
`extension=/internal/shared/extensions/${extensionName}`,
`extension=${phpRuntime.ENV.PHP_INI_SCAN_DIR}/${extensionName}`,
].join('\n')
);
}
/*
* An ICU data file must be loaded to support Intl extension.
* To achieve this, a shared directory is mounted and referenced
* via the ICU_DATA environment variable.
* By default, this variable is set to '/internal/shared',
* By default, this variable is set to '/internal/private',
* which corresponds to the actual file location.
*
* The Intl extension is hard-coded to look for the `icudt74l` filename,
* which means the ICU data file must use that exact name.
*/
if (
!FSHelpers.fileExists(
Expand All @@ -87,7 +88,7 @@ export async function withIntl(
) {
phpRuntime.FS.mkdirTree(phpRuntime.ENV.ICU_DATA);
phpRuntime.FS.writeFile(
`${phpRuntime.ENV.ICU_DATA}/icudt74l.dat`,
`${phpRuntime.ENV.ICU_DATA}/${dataName}`,
new Uint8Array(ICUData)
);
}
Expand Down
18 changes: 9 additions & 9 deletions packages/php-wasm/node/src/lib/xdebug/with-xdebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function withXdebug(
...options,
ENV: {
...options.ENV,
PHP_INI_SCAN_DIR: '/internal/shared/extensions',
PHP_INI_SCAN_DIR: '/internal/private/extensions',
},
onRuntimeInitialized: (phpRuntime: PHPRuntime) => {
if (options.onRuntimeInitialized) {
Expand All @@ -37,19 +37,19 @@ export async function withXdebug(
if (
!FSHelpers.fileExists(
phpRuntime.FS,
'/internal/shared/extensions'
phpRuntime.ENV.PHP_INI_SCAN_DIR
)
) {
phpRuntime.FS.mkdirTree('/internal/shared/extensions');
phpRuntime.FS.mkdirTree(phpRuntime.ENV.PHP_INI_SCAN_DIR);
}
if (
!FSHelpers.fileExists(
phpRuntime.FS,
`/internal/shared/extensions/${fileName}`
`${phpRuntime.ENV.PHP_INI_SCAN_DIR}/${fileName}`
)
) {
phpRuntime.FS.writeFile(
`/internal/shared/extensions/${fileName}`,
`${phpRuntime.ENV.PHP_INI_SCAN_DIR}/${fileName}`,
new Uint8Array(extension)
);
}
Expand All @@ -59,14 +59,14 @@ export async function withXdebug(
if (
!FSHelpers.fileExists(
phpRuntime.FS,
'/internal/shared/extensions/xdebug.ini'
`${phpRuntime.ENV.PHP_INI_SCAN_DIR}/xdebug.ini`
)
) {
const ideKey = xdebugOptions?.ideKey || 'PLAYGROUNDCLI';
const ideKey = xdebugOptions?.ideKey || 'PHPWASMCLI';
phpRuntime.FS.writeFile(
'/internal/shared/extensions/xdebug.ini',
`${phpRuntime.ENV.PHP_INI_SCAN_DIR}/xdebug.ini`,
[
'zend_extension=/internal/shared/extensions/xdebug.so',
`zend_extension=${phpRuntime.ENV.PHP_INI_SCAN_DIR}/xdebug.so`,
'xdebug.mode=debug,develop',
'xdebug.start_with_request=yes',
`xdebug.idekey="${ideKey}"`,
Expand Down
14 changes: 8 additions & 6 deletions packages/php-wasm/node/src/test/php-dynamic-loading.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ describe.each(phpVersions)('PHP %s', async (phpVersion) => {

it('has its own ini file and entries', async () => {
const entries = php.readFileAsText(
'/internal/shared/extensions/xdebug.ini'
'/internal/private/extensions/xdebug.ini'
);

const expected = [
'zend_extension=/internal/shared/extensions/xdebug.so',
'zend_extension=/internal/private/extensions/xdebug.so',
'xdebug.mode=debug,develop',
'xdebug.start_with_request=yes',
'xdebug.idekey="PLAYGROUNDCLI"',
'xdebug.idekey="PHPWASMCLI"',
].join('\n');

expect(entries).toEqual(expected);
Expand Down Expand Up @@ -207,11 +207,11 @@ describe.each(phpVersions)('PHP %s', async (phpVersion) => {

it('has its own ini file and entries', async () => {
const entries = php.readFileAsText(
'/internal/shared/extensions/intl.ini'
'/internal/private/extensions/intl.ini'
);

const expected = [
'extension=/internal/shared/extensions/intl.so',
'extension=/internal/private/extensions/intl.so',
].join('\n');

expect(entries).toEqual(expected);
Expand All @@ -222,7 +222,9 @@ describe.each(phpVersions)('PHP %s', async (phpVersion) => {
* The Intl extension is hard-coded to look for the `icudt74l` filename,
* which means the ICU data file must use that exact name.
*/
expect(php.listFiles('/internal/shared')).toContain('icudt74l.dat');
expect(php.listFiles('/internal/private')).toContain(
'icudt74l.dat'
);
});

it('uses intl functions', async () => {
Expand Down
37 changes: 19 additions & 18 deletions packages/php-wasm/web/src/lib/extensions/intl/with-intl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,29 @@ export async function withIntl(
): Promise<EmscriptenOptions> {
const memoizedFetch = createMemoizedFetch(fetch);

/*
* The Intl extension is hard-coded to look for the `icudt74l` filename,
* which means the ICU data file must use that exact name.
*/
const dataName = 'icudt74l.dat';
const extensionName = 'intl.so';
const dataName = 'icu.dat';

const extensionPath = await getIntlExtensionModule(version);
// @ts-ignore
const dataPath = (await import('../../../../public/shared/icu.dat'))
.default;
const extensionPath = await getIntlExtensionModule(version);

const [extension, ICUData] = await Promise.all([
memoizedFetch(extensionPath).then((response) => response.arrayBuffer()),
const [ICUData, extension] = await Promise.all([
memoizedFetch(dataPath).then((response) => response.arrayBuffer()),
memoizedFetch(extensionPath).then((response) => response.arrayBuffer()),
]);

return {
...options,
ENV: {
...options.ENV,
PHP_INI_SCAN_DIR: '/internal/shared/extensions',
ICU_DATA: '/internal/shared',
PHP_INI_SCAN_DIR: '/internal/private/extensions',
ICU_DATA: '/internal/private',
},
onRuntimeInitialized: (phpRuntime: PHPRuntime) => {
if (options.onRuntimeInitialized) {
Expand All @@ -44,19 +48,19 @@ export async function withIntl(
if (
!FSHelpers.fileExists(
phpRuntime.FS,
'/internal/shared/extensions'
phpRuntime.ENV.PHP_INI_SCAN_DIR
)
) {
phpRuntime.FS.mkdirTree('/internal/shared/extensions');
phpRuntime.FS.mkdirTree(phpRuntime.ENV.PHP_INI_SCAN_DIR);
}
if (
!FSHelpers.fileExists(
phpRuntime.FS,
`/internal/shared/extensions/${extensionName}`
`${phpRuntime.ENV.PHP_INI_SCAN_DIR}/${extensionName}`
)
) {
phpRuntime.FS.writeFile(
`/internal/shared/extensions/${extensionName}`,
`${phpRuntime.ENV.PHP_INI_SCAN_DIR}/${extensionName}`,
new Uint8Array(extension)
);
}
Expand All @@ -66,25 +70,22 @@ export async function withIntl(
if (
!FSHelpers.fileExists(
phpRuntime.FS,
'/internal/shared/extensions/intl.ini'
`${phpRuntime.ENV.PHP_INI_SCAN_DIR}/intl.ini`
)
) {
phpRuntime.FS.writeFile(
'/internal/shared/extensions/intl.ini',
`${phpRuntime.ENV.PHP_INI_SCAN_DIR}/intl.ini`,
[
`extension=/internal/shared/extensions/${extensionName}`,
`extension=${phpRuntime.ENV.PHP_INI_SCAN_DIR}/${extensionName}`,
].join('\n')
);
}
/*
* An ICU data file must be loaded to support Intl extension.
* To achieve this, a shared directory is mounted and referenced
* via the ICU_DATA environment variable.
* By default, this variable is set to '/internal/shared',
* By default, this variable is set to '/internal/private',
* which corresponds to the actual file location.
*
* The Intl extension is hard-coded to look for the `icudt74l` filename,
* which means the ICU data file must use that exact name.
*/
if (
!FSHelpers.fileExists(
Expand All @@ -94,7 +95,7 @@ export async function withIntl(
) {
phpRuntime.FS.mkdirTree(phpRuntime.ENV.ICU_DATA);
phpRuntime.FS.writeFile(
`${phpRuntime.ENV.ICU_DATA}/icudt74l.dat`,
`${phpRuntime.ENV.ICU_DATA}/${dataName}`,
new Uint8Array(ICUData)
);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/php-wasm/web/src/test/php-dynamic-loading.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ SupportedPHPVersions.forEach((phpVersion) => {
);

const text = php.readFileAsText(
'/internal/shared/extensions/intl.ini'
'/internal/private/extensions/intl.ini'
);

php.exit();
Expand All @@ -81,7 +81,7 @@ SupportedPHPVersions.forEach((phpVersion) => {
}, phpVersion);

const expected = [
'extension=/internal/shared/extensions/intl.so',
'extension=/internal/private/extensions/intl.so',
].join('\n');

test.expect(result).toEqual(expected);
Expand All @@ -95,7 +95,7 @@ SupportedPHPVersions.forEach((phpVersion) => {
})
);

const list = php.listFiles('/internal/shared');
const list = php.listFiles('/internal/private');

php.exit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ export class PlaygroundCliBlueprintV2Worker extends PHPWorker {
phpWasmInitOptions: { nativeInternalDirPath },
},
followSymlinks: allow?.includes('follow-symlinks'),
withIntl: withIntl,
withIntl,
withXdebug,
});
},
Expand Down
4 changes: 2 additions & 2 deletions packages/playground/client/src/blueprints-v1-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
BlueprintReflection,
} from '.';
import { collectPhpLogs, logger } from '@php-wasm/logger';
import { consumeAPI } from '@php-wasm/universal';
import { consumeAPI, type UniversalPHP } from '@php-wasm/universal';

export class BlueprintsV1Handler {
private readonly options: StartPlaygroundOptions;
Expand Down Expand Up @@ -78,7 +78,7 @@ export class BlueprintsV1Handler {
corsProxy,
gitAdditionalHeadersCallback,
});
await runBlueprintV1Steps(compiled, playground);
await runBlueprintV1Steps(compiled, playground as UniversalPHP);
}

/**
Expand Down
Loading