Merge pull request #754 from jasongill/develop

Add automatic detection of required PHP version from Composer's platform setting
This commit is contained in:
Shivam Mathur 2023-07-30 08:50:55 +05:30 committed by GitHub
commit 36e04fc85c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 0 deletions

View File

@ -282,6 +282,24 @@ describe('Utils tests', () => {
process.env['php-version'] = '8.2'; process.env['php-version'] = '8.2';
expect(await utils.readPHPVersion()).toBe('8.2'); expect(await utils.readPHPVersion()).toBe('8.2');
delete process.env['php-version-file'];
delete process.env['php-version'];
existsSync.mockReturnValueOnce(false).mockReturnValueOnce(true);
readFileSync.mockReturnValue(
'{ "platform-overrides": { "php": "7.3.25" } }'
);
expect(await utils.readPHPVersion()).toBe('7.3.25');
existsSync
.mockReturnValueOnce(false)
.mockReturnValueOnce(false)
.mockReturnValueOnce(true);
readFileSync.mockReturnValue(
'{ "config": { "platform": { "php": "7.4.33" } } }'
);
expect(await utils.readPHPVersion()).toBe('7.4.33');
existsSync.mockClear(); existsSync.mockClear();
readFileSync.mockClear(); readFileSync.mockClear();
}); });

17
dist/index.js vendored
View File

@ -1371,6 +1371,23 @@ async function readPHPVersion() {
else if (versionFile !== '.php-version') { else if (versionFile !== '.php-version') {
throw new Error(`Could not find '${versionFile}' file.`); throw new Error(`Could not find '${versionFile}' file.`);
} }
const composerLock = 'composer.lock';
if (fs_1.default.existsSync(composerLock)) {
const lockFileContents = JSON.parse(fs_1.default.readFileSync(composerLock, 'utf8'));
if (lockFileContents['platform-overrides'] &&
lockFileContents['platform-overrides']['php']) {
return lockFileContents['platform-overrides']['php'];
}
}
const composerJson = 'composer.json';
if (fs_1.default.existsSync(composerJson)) {
const composerFileContents = JSON.parse(fs_1.default.readFileSync(composerJson, 'utf8'));
if (composerFileContents['config'] &&
composerFileContents['config']['platform'] &&
composerFileContents['config']['platform']['php']) {
return composerFileContents['config']['platform']['php'];
}
}
return 'latest'; return 'latest';
} }
exports.readPHPVersion = readPHPVersion; exports.readPHPVersion = readPHPVersion;

View File

@ -440,6 +440,32 @@ export async function readPHPVersion(): Promise<string> {
} else if (versionFile !== '.php-version') { } else if (versionFile !== '.php-version') {
throw new Error(`Could not find '${versionFile}' file.`); throw new Error(`Could not find '${versionFile}' file.`);
} }
const composerLock = 'composer.lock';
if (fs.existsSync(composerLock)) {
const lockFileContents = JSON.parse(fs.readFileSync(composerLock, 'utf8'));
if (
lockFileContents['platform-overrides'] &&
lockFileContents['platform-overrides']['php']
) {
return lockFileContents['platform-overrides']['php'];
}
}
const composerJson = 'composer.json';
if (fs.existsSync(composerJson)) {
const composerFileContents = JSON.parse(
fs.readFileSync(composerJson, 'utf8')
);
if (
composerFileContents['config'] &&
composerFileContents['config']['platform'] &&
composerFileContents['config']['platform']['php']
) {
return composerFileContents['config']['platform']['php'];
}
}
return 'latest'; return 'latest';
} }