|
| 1 | +import { |
| 2 | + loadPHPExtension as loadPHPExtensionFromSource, |
| 3 | + type LoadedPHPExtension, |
| 4 | + type PHPExtensionIniDirective, |
| 5 | + type PHPExtensionLoadTiming, |
| 6 | + type PHPExtensionSourceFormat, |
| 7 | + type PHPWasmAsyncMode, |
| 8 | +} from '@php-wasm/universal'; |
| 9 | +import type { StepHandler } from '.'; |
| 10 | +import type { Directory } from '../v1/resources'; |
| 11 | + |
| 12 | +type SourceFile = File & { sourceUrl?: string }; |
| 13 | + |
| 14 | +/** |
| 15 | + * @inheritDoc loadPHPExtension |
| 16 | + * @example |
| 17 | + * |
| 18 | + * <code> |
| 19 | + * { |
| 20 | + * "step": "loadPHPExtension", |
| 21 | + * "source": { |
| 22 | + * "resource": "url", |
| 23 | + * "url": "https://example.com/extensions/example/manifest.json" |
| 24 | + * }, |
| 25 | + * "sourceFormat": "manifest" |
| 26 | + * } |
| 27 | + * </code> |
| 28 | + */ |
| 29 | +export interface LoadPHPExtensionStep<FileResource, DirectoryResource> { |
| 30 | + step: 'loadPHPExtension'; |
| 31 | + /** The extension `.so` file or a compile-extension `manifest.json`. */ |
| 32 | + source: FileResource; |
| 33 | + /** Defaults to "manifest" for `.json` files and "so" otherwise. */ |
| 34 | + sourceFormat?: PHPExtensionSourceFormat; |
| 35 | + /** Required when loading a `.so` file whose filename is not named after the extension. */ |
| 36 | + name?: string; |
| 37 | + /** Defaults to "auto". */ |
| 38 | + loadTiming?: PHPExtensionLoadTiming; |
| 39 | + /** Defaults to "extension". Use "zend_extension" for extensions like Xdebug. */ |
| 40 | + loadWithIniDirective?: PHPExtensionIniDirective; |
| 41 | + /** Extra `php.ini` entries to write next to the extension. */ |
| 42 | + iniEntries?: Record<string, string>; |
| 43 | + /** Extra files required by the extension, such as ICU data or shared libraries. */ |
| 44 | + extraFiles?: DirectoryResource; |
| 45 | + /** Where to write `extraFiles`. Defaults to an extension-specific assets directory. */ |
| 46 | + extraFilesPath?: string; |
| 47 | + /** Runtime environment variables needed by the extension. */ |
| 48 | + env?: Record<string, string>; |
| 49 | + /** Overrides manifest artifact selection. Defaults to the running PHP version. */ |
| 50 | + phpVersion?: string; |
| 51 | + /** Overrides manifest artifact selection. Defaults to the running async mode. */ |
| 52 | + asyncMode?: PHPWasmAsyncMode; |
| 53 | + /** Base URL for relative artifact paths in an inline or bundled manifest. */ |
| 54 | + manifestBaseUrl?: string; |
| 55 | + /** Where to install the extension `.so` and `.ini` files. */ |
| 56 | + extensionDir?: string; |
| 57 | +} |
| 58 | + |
| 59 | +export const loadPHPExtension: StepHandler< |
| 60 | + LoadPHPExtensionStep<File, Directory>, |
| 61 | + Promise<LoadedPHPExtension> |
| 62 | +> = async ( |
| 63 | + playground, |
| 64 | + { |
| 65 | + source, |
| 66 | + sourceFormat, |
| 67 | + name, |
| 68 | + loadTiming, |
| 69 | + loadWithIniDirective, |
| 70 | + iniEntries, |
| 71 | + extraFiles, |
| 72 | + extraFilesPath, |
| 73 | + env, |
| 74 | + phpVersion, |
| 75 | + asyncMode, |
| 76 | + manifestBaseUrl, |
| 77 | + extensionDir, |
| 78 | + } |
| 79 | +) => { |
| 80 | + const format = sourceFormat ?? inferSourceFormat(source); |
| 81 | + const bytes = new Uint8Array(await source.arrayBuffer()); |
| 82 | + |
| 83 | + return await loadPHPExtensionFromSource(playground, { |
| 84 | + source: |
| 85 | + format === 'manifest' |
| 86 | + ? { |
| 87 | + format, |
| 88 | + manifest: JSON.parse(new TextDecoder().decode(bytes)), |
| 89 | + baseUrl: manifestBaseUrl ?? getSourceUrl(source), |
| 90 | + } |
| 91 | + : { |
| 92 | + format, |
| 93 | + name: name ?? inferExtensionName(source), |
| 94 | + bytes, |
| 95 | + }, |
| 96 | + name, |
| 97 | + loadTiming, |
| 98 | + loadWithIniDirective, |
| 99 | + iniEntries, |
| 100 | + extraFiles: extraFiles |
| 101 | + ? { |
| 102 | + targetPath: extraFilesPath, |
| 103 | + files: extraFiles.files, |
| 104 | + } |
| 105 | + : undefined, |
| 106 | + env, |
| 107 | + phpVersion, |
| 108 | + asyncMode, |
| 109 | + extensionDir, |
| 110 | + }); |
| 111 | +}; |
| 112 | + |
| 113 | +function inferSourceFormat(source: File): PHPExtensionSourceFormat { |
| 114 | + return source.name.endsWith('.json') ? 'manifest' : 'so'; |
| 115 | +} |
| 116 | + |
| 117 | +function inferExtensionName(source: File): string | undefined { |
| 118 | + return source.name.endsWith('.so') ? source.name.slice(0, -3) : undefined; |
| 119 | +} |
| 120 | + |
| 121 | +function getSourceUrl(source: SourceFile): string | undefined { |
| 122 | + return source.sourceUrl; |
| 123 | +} |
0 commit comments