mirror of
https://github.com/shivammathur/setup-php.git
synced 2024-11-22 11:51:07 +07:00
Add support for reading PHP version from composer.lock or composer.json
This commit is contained in:
parent
72ae4ccbe5
commit
a5fb328c6a
@ -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();
|
||||
});
|
||||
|
21
dist/index.js
vendored
21
dist/index.js
vendored
@ -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;
|
||||
|
32
src/utils.ts
32
src/utils.ts
@ -440,6 +440,38 @@ export async function readPHPVersion(): Promise<string> {
|
||||
} 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';
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user