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
59 changes: 44 additions & 15 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const PHP_MODULES_DIR = path.join(PHP_BIN_DIR, "modules");
const PHP_LIB_DIR = path.join(PHP_PKG, "native/lib");
const COMPOSER_BIN = path.join(PHP_BIN_DIR, "composer");

type PhpSpawnConfig = {
command: string;
env: NodeJS.ProcessEnv;
};

export async function getPhpFiles(): Promise<RuntimeFiles> {
const files = await libphp.getFiles();

Expand Down Expand Up @@ -121,13 +126,45 @@ export async function runComposerScripts(composerFile: File, workPath: string):

export async function ensureLocalPhp(): Promise<boolean> {
try {
await spawnAsync('which', ['php', 'php-cgi'], { stdio: 'pipe' });
await spawnAsync('php', ['-v'], { stdio: 'pipe' });
return true;
} catch (e) {
return false;
}
}

export async function getPhpSpawnConfig(optsEnv: NodeJS.ProcessEnv = {}): Promise<PhpSpawnConfig> {
const env: NodeJS.ProcessEnv = {
...process.env,
...optsEnv,
COMPOSER_HOME: '/tmp',
};

if (process.platform === 'linux') {
return {
command: 'php',
env: {
...env,
PATH: `${PHP_BIN_DIR}${path.delimiter}${process.env.PATH || ''}`,
PHP_INI_EXTENSION_DIR: PHP_MODULES_DIR,
PHP_INI_SCAN_DIR: `:${path.resolve(__dirname, '../conf')}`,
LD_LIBRARY_PATH: `${PHP_LIB_DIR}:/usr/lib64:/lib64${process.env.LD_LIBRARY_PATH ? `:${process.env.LD_LIBRARY_PATH}` : ''}`
}
};
}

if (await ensureLocalPhp()) {
return {
command: 'php',
env,
};
}

throw new Error(
`Local builds on ${process.platform} require PHP to be installed and available on PATH.`
);
}

export async function readRuntimeFile(file: File): Promise<string> {
const blob = await FileBlob.fromStream({
stream: file.toStream(),
Expand All @@ -142,20 +179,12 @@ export async function readRuntimeFile(file: File): Promise<string> {

async function runPhp(args: ReadonlyArray<string>, opts: SpawnOptions = {}) {
try {
await spawnAsync('php', args,
{
...opts,
env: {
...process.env,
...(opts.env || {}),
COMPOSER_HOME: '/tmp',
PATH: `${PHP_BIN_DIR}:${process.env.PATH}`,
PHP_INI_EXTENSION_DIR: PHP_MODULES_DIR,
PHP_INI_SCAN_DIR: `:${path.resolve(__dirname, '../conf')}`,
LD_LIBRARY_PATH: `${PHP_LIB_DIR}:/usr/lib64:/lib64:${process.env.LD_LIBRARY_PATH}`
}
}
);
const { command, env } = await getPhpSpawnConfig(opts.env);

await spawnAsync(command, args, {
...opts,
env,
});
} catch (e) {
console.error(e);
process.exit(1);
Expand Down
28 changes: 28 additions & 0 deletions test/spec/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const path = require('path');

describe('php spawn config', () => {
const originalPlatform = process.platform;

afterEach(() => {
Object.defineProperty(process, 'platform', {
value: originalPlatform,
});
});

test('uses bundled runtime configuration on linux', async () => {
Object.defineProperty(process, 'platform', {
value: 'linux',
});

const utils = require(path.join(__dirname, '..', '..', 'dist', 'utils.js'));
const config = await utils.getPhpSpawnConfig({ TEST_ENV: '1' });

expect(config.command).toBe('php');
expect(config.env.TEST_ENV).toBe('1');
expect(config.env.PATH).toContain(`@libphp${path.sep}almalinux-9-v85`);
expect(config.env.PATH).toContain(path.delimiter);
expect(config.env.PHP_INI_EXTENSION_DIR).toContain(`${path.sep}modules`);
expect(config.env.PHP_INI_SCAN_DIR).toContain(`${path.sep}conf`);
expect(config.env.LD_LIBRARY_PATH).toContain('/usr/lib64:/lib64');
});
});