From a5fb328c6a38ac8086676fc64e6e78a39d6ef69e Mon Sep 17 00:00:00 2001 From: Jason Gill Date: Sat, 29 Jul 2023 19:46:06 -0400 Subject: [PATCH 1/2] Add support for reading PHP version from composer.lock or composer.json --- __tests__/utils.test.ts | 25 +++++++++++++++++++++++++ dist/index.js | 21 +++++++++++++++++++++ src/utils.ts | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 21feea57..db9c84d7 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -282,6 +282,31 @@ describe('Utils tests', () => { process.env['php-version'] = '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 + .mockReturnValueOnce(false) + .mockReturnValueOnce(false) + .mockReturnValueOnce(true); + readFileSync.mockReturnValue('{ "require": { "php": "^8.2" } }'); + expect(await utils.readPHPVersion()).toBe('^8.2'); + existsSync.mockClear(); readFileSync.mockClear(); }); diff --git a/dist/index.js b/dist/index.js index 4a719438..70550592 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1371,6 +1371,27 @@ async function readPHPVersion() { else if (versionFile !== '.php-version') { 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']; + } + if (composerFileContents['require'] && + composerFileContents['require']['php']) { + return composerFileContents['require']['php']; + } + } return 'latest'; } exports.readPHPVersion = readPHPVersion; diff --git a/src/utils.ts b/src/utils.ts index e25928a4..19cb8563 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -440,6 +440,38 @@ export async function readPHPVersion(): Promise { } else if (versionFile !== '.php-version') { 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']; + } + if ( + composerFileContents['require'] && + composerFileContents['require']['php'] + ) { + return composerFileContents['require']['php']; + } + } + return 'latest'; } From 95916a72b1f5cebc1f2cf80cf41b266090b37295 Mon Sep 17 00:00:00 2001 From: Jason Gill Date: Sat, 29 Jul 2023 19:48:28 -0400 Subject: [PATCH 2/2] Only use config.platform.php, as it's guaranteed to be the required version of PHP --- __tests__/utils.test.ts | 7 ------- dist/index.js | 4 ---- src/utils.ts | 6 ------ 3 files changed, 17 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index db9c84d7..f64be9bf 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -300,13 +300,6 @@ describe('Utils tests', () => { ); expect(await utils.readPHPVersion()).toBe('7.4.33'); - existsSync - .mockReturnValueOnce(false) - .mockReturnValueOnce(false) - .mockReturnValueOnce(true); - readFileSync.mockReturnValue('{ "require": { "php": "^8.2" } }'); - expect(await utils.readPHPVersion()).toBe('^8.2'); - existsSync.mockClear(); readFileSync.mockClear(); }); diff --git a/dist/index.js b/dist/index.js index 70550592..c61b659a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1387,10 +1387,6 @@ async function readPHPVersion() { composerFileContents['config']['platform']['php']) { return composerFileContents['config']['platform']['php']; } - if (composerFileContents['require'] && - composerFileContents['require']['php']) { - return composerFileContents['require']['php']; - } } return 'latest'; } diff --git a/src/utils.ts b/src/utils.ts index 19cb8563..c42b9310 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -464,12 +464,6 @@ export async function readPHPVersion(): Promise { ) { return composerFileContents['config']['platform']['php']; } - if ( - composerFileContents['require'] && - composerFileContents['require']['php'] - ) { - return composerFileContents['require']['php']; - } } return 'latest';