diff --git a/README.md b/README.md index d12177fb..778162b0 100644 --- a/README.md +++ b/README.md @@ -398,6 +398,12 @@ Disable coverage for these reasons: - Shared extensions prefixed with `:` are disabled. - See [PHP extension support](#heavy_plus_sign-php-extension-support) for more info. +#### `ini-file` (optional) + +- Specify the base `php.ini` file. +- Accepts `production`, `development` or `none`. +- By default, production `php.ini` file is used. + #### `ini-values` (optional) - Specify the values you want to add to `php.ini`. diff --git a/__tests__/install.test.ts b/__tests__/install.test.ts index 7f836133..c06d55b4 100644 --- a/__tests__/install.test.ts +++ b/__tests__/install.test.ts @@ -22,6 +22,9 @@ jest.mock('../src/install', () => ({ const version: string = await utils.parseVersion( await utils.getInput('php-version', true) ); + const ini_file: string = await utils.parseIniFile( + await utils.getInput('ini-file', false) + ); const tool = await utils.scriptTool(os_version); const filename = os_version + (await utils.scriptExtension(os_version)); return [ @@ -29,6 +32,7 @@ jest.mock('../src/install', () => ({ tool, filename, version, + ini_file, __dirname ].join(' '); }) @@ -48,6 +52,7 @@ function setEnv( version: string | number, os: string, extension_csv: string, + ini_file: string, ini_values_csv: string, coverage_driver: string, tools: string @@ -55,6 +60,7 @@ function setEnv( process.env['php-version'] = version.toString(); process.env['RUNNER_OS'] = os; process.env['extensions'] = extension_csv; + process.env['ini-file'] = ini_file; process.env['ini-values'] = ini_values_csv; process.env['coverage'] = coverage_driver; process.env['tools'] = tools; @@ -62,25 +68,26 @@ function setEnv( describe('Install', () => { it.each` - version | os | extension_csv | ini_values_csv | coverage_driver | tools | output - ${'7.3'} | ${'darwin'} | ${''} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 7.3 ' + __dirname} - ${'7.3'} | ${'darwin'} | ${'a, b'} | ${'a=b'} | ${'x'} | ${''} | ${'initial script install extensions set coverage driver edit php.ini bash darwin.sh 7.3 ' + __dirname} - ${'7.4.1'} | ${'darwin'} | ${''} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 7.4 ' + __dirname} - ${'8'} | ${'darwin'} | ${''} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 8.0 ' + __dirname} - ${'8.0'} | ${'darwin'} | ${''} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 8.0 ' + __dirname} - ${'8.1'} | ${'darwin'} | ${''} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 8.1 ' + __dirname} - ${'7.3'} | ${'linux'} | ${''} | ${''} | ${''} | ${''} | ${'initial script bash linux.sh 7.3 ' + __dirname} - ${'7.3'} | ${'linux'} | ${'a, b'} | ${'a=b'} | ${'x'} | ${'phpunit'} | ${'initial script install extensions add_tool set coverage driver edit php.ini bash linux.sh 7.3 ' + __dirname} - ${'latest'} | ${'linux'} | ${''} | ${''} | ${''} | ${''} | ${'initial script bash linux.sh 8.1 ' + __dirname} - ${'7.0'} | ${'win32'} | ${''} | ${''} | ${''} | ${''} | ${'initial script pwsh win32.ps1 7.0 ' + __dirname} - ${'7.3'} | ${'win32'} | ${''} | ${''} | ${''} | ${''} | ${'initial script pwsh win32.ps1 7.3 ' + __dirname} - ${'7.3'} | ${'win32'} | ${'a, b'} | ${'a=b'} | ${'x'} | ${''} | ${'initial script install extensions set coverage driver edit php.ini pwsh win32.ps1 7.3 ' + __dirname} + version | os | extension_csv | ini_file | ini_values_csv | coverage_driver | tools | output + ${'7.3'} | ${'darwin'} | ${''} | ${'production'} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 7.3 production ' + __dirname} + ${'7.3'} | ${'darwin'} | ${'a, b'} | ${'development'} | ${'a=b'} | ${'x'} | ${''} | ${'initial script install extensions set coverage driver edit php.ini bash darwin.sh 7.3 development ' + __dirname} + ${'7.4.1'} | ${'darwin'} | ${''} | ${'none'} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 7.4 none ' + __dirname} + ${'8'} | ${'darwin'} | ${''} | ${''} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 8.0 production ' + __dirname} + ${'8.0'} | ${'darwin'} | ${''} | ${'development'} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 8.0 development ' + __dirname} + ${'8.1'} | ${'darwin'} | ${''} | ${'none'} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 8.1 none ' + __dirname} + ${'7.3'} | ${'linux'} | ${''} | ${'invalid'} | ${''} | ${''} | ${''} | ${'initial script bash linux.sh 7.3 production ' + __dirname} + ${'7.3'} | ${'linux'} | ${'a, b'} | ${'development'} | ${'a=b'} | ${'x'} | ${'phpunit'} | ${'initial script install extensions add_tool set coverage driver edit php.ini bash linux.sh 7.3 development ' + __dirname} + ${'latest'} | ${'linux'} | ${''} | ${'none'} | ${''} | ${''} | ${''} | ${'initial script bash linux.sh 8.1 none ' + __dirname} + ${'7.0'} | ${'win32'} | ${''} | ${'production'} | ${''} | ${''} | ${''} | ${'initial script pwsh win32.ps1 7.0 production ' + __dirname} + ${'7.3'} | ${'win32'} | ${''} | ${'development'} | ${''} | ${''} | ${''} | ${'initial script pwsh win32.ps1 7.3 development ' + __dirname} + ${'7.3'} | ${'win32'} | ${'a, b'} | ${'none'} | ${'a=b'} | ${'x'} | ${''} | ${'initial script install extensions set coverage driver edit php.ini pwsh win32.ps1 7.3 none ' + __dirname} `( 'Test install on $os for $version with extensions=$extension_csv, ini_values=$ini_values_csv, coverage_driver=$coverage_driver, tools=$tools', async ({ version, os, extension_csv, + ini_file, ini_values_csv, coverage_driver, tools, @@ -90,6 +97,7 @@ describe('Install', () => { version, os, extension_csv, + ini_file, ini_values_csv, coverage_driver, tools diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index daeb4e46..6772f6d0 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -71,6 +71,15 @@ describe('Utils tests', () => { expect(await utils.parseVersion('4.x')).toBe(undefined); }); + it('checking parseIniFile', async () => { + expect(await utils.parseIniFile('production')).toBe('production'); + expect(await utils.parseIniFile('development')).toBe('development'); + expect(await utils.parseIniFile('none')).toBe('none'); + expect(await utils.parseIniFile('php.ini-production')).toBe('production'); + expect(await utils.parseIniFile('php.ini-development')).toBe('development'); + expect(await utils.parseIniFile('invalid')).toBe('production'); + }) + it('checking asyncForEach', async () => { const array: Array = ['a', 'b', 'c']; let concat = ''; diff --git a/action.yml b/action.yml index 35916dc0..aec1d44b 100644 --- a/action.yml +++ b/action.yml @@ -12,6 +12,10 @@ inputs: extensions: description: 'Setup PHP extensions.' required: false + ini-file: + description: 'Set base ini file.' + default: 'production' + required: false ini-values: description: 'Add values to php.ini.' required: false diff --git a/dist/index.js b/dist/index.js index 234c64a2..be1868ce 100644 --- a/dist/index.js +++ b/dist/index.js @@ -472,12 +472,13 @@ async function run() { return; } const version = await utils.parseVersion(await utils.getInput('php-version', true)); + const ini_file = await utils.parseIniFile(await utils.getInput('ini-file', false)); if (version) { const os_version = process.platform; const tool = await utils.scriptTool(os_version); const script = os_version + (await utils.scriptExtension(os_version)); const location = await getScript(script, version, os_version); - await (0, exec_1.exec)(await utils.joins(tool, location, version, __dirname)); + await (0, exec_1.exec)(await utils.joins(tool, location, version, ini_file, __dirname)); } else { core.setFailed('Unable to get the PHP version'); @@ -911,7 +912,7 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.parseExtensionSource = exports.customPackage = exports.scriptTool = exports.scriptExtension = exports.joins = exports.getCommand = exports.getUnsupportedLog = exports.suppressOutput = exports.getExtensionPrefix = exports.CSVArray = exports.extensionArray = exports.writeScript = exports.readFile = exports.addLog = exports.stepLog = exports.log = exports.color = exports.asyncForEach = exports.parseVersion = exports.getManifestURL = exports.fetch = exports.getInput = exports.readEnv = void 0; +exports.parseExtensionSource = exports.customPackage = exports.scriptTool = exports.scriptExtension = exports.joins = exports.getCommand = exports.getUnsupportedLog = exports.suppressOutput = exports.getExtensionPrefix = exports.CSVArray = exports.extensionArray = exports.writeScript = exports.readFile = exports.addLog = exports.stepLog = exports.log = exports.color = exports.asyncForEach = exports.parseIniFile = exports.parseVersion = exports.getManifestURL = exports.fetch = exports.getInput = exports.readEnv = void 0; const fs = __importStar(__nccwpck_require__(147)); const https = __importStar(__nccwpck_require__(687)); const path = __importStar(__nccwpck_require__(17)); @@ -992,6 +993,17 @@ async function parseVersion(version) { } } exports.parseVersion = parseVersion; +async function parseIniFile(ini_file) { + switch (true) { + case /^(production|development|none)$/.test(ini_file): + return ini_file; + case /php\.ini-(production|development)$/.test(ini_file): + return ini_file.split('-')[1]; + default: + return 'production'; + } +} +exports.parseIniFile = parseIniFile; async function asyncForEach(array, callback) { for (let index = 0; index < array.length; index++) { await callback(array[index], index, array); diff --git a/src/install.ts b/src/install.ts index a3c9feae..262ca6aa 100644 --- a/src/install.ts +++ b/src/install.ts @@ -57,12 +57,17 @@ export async function run(): Promise { const version: string = await utils.parseVersion( await utils.getInput('php-version', true) ); + const ini_file: string = await utils.parseIniFile( + await utils.getInput('ini-file', false) + ); if (version) { const os_version: string = process.platform; const tool = await utils.scriptTool(os_version); const script = os_version + (await utils.scriptExtension(os_version)); const location = await getScript(script, version, os_version); - await exec(await utils.joins(tool, location, version, __dirname)); + await exec( + await utils.joins(tool, location, version, ini_file, __dirname) + ); } else { core.setFailed('Unable to get the PHP version'); } diff --git a/src/scripts/darwin.sh b/src/scripts/darwin.sh index 67f93253..8dc624a7 100644 --- a/src/scripts/darwin.sh +++ b/src/scripts/darwin.sh @@ -195,6 +195,18 @@ php_extra_version() { fi } +# Function to set php.ini +add_php_config() { + if ! [ -e "$ini_dir"/php.ini-development ]; then + sudo cp "$ini_dir"/php.ini "$ini_dir"/php.ini-development + fi + if [[ "$ini" = "production" || "$ini" = "development" ]]; then + sudo cp "$ini_dir"/php.ini-"$ini" "$ini_dir"/php.ini + elif [ "$ini" = "none" ]; then + echo '' | sudo tee "${ini_file[@]}" >/dev/null 2>&1 + fi +} + # Function to Setup PHP. setup_php() { step_log "Setup PHP" @@ -234,8 +246,9 @@ setup_php() { } # Variables -version=$1 -dist=$2 +version=${1:-'8.1'} +ini=${2:-'production'} +dist=$3 php_formula=shivammathur/php/php@"$version" brew_path="$(command -v brew)" brew_path_dir="$(dirname "$brew_path")" diff --git a/src/scripts/linux.sh b/src/scripts/linux.sh index 1491bfbf..82e91e1d 100644 --- a/src/scripts/linux.sh +++ b/src/scripts/linux.sh @@ -178,6 +178,27 @@ php_extra_version() { fi } +# Function to set php.ini +add_php_config() { + php_lib_dir=/usr/lib/php/"$version" + current_ini="$php_lib_dir"/php.ini-current + current=$(cat "$current_ini" 2>/dev/null) + if [ "$current" = "$ini" ]; then + return; + fi + if [[ "$ini" = "production" && "x$current" != "xproduction" ]]; then + echo "${ini_file[@]}" | xargs -n 1 -P 6 sudo cp "$php_lib_dir"/php.ini-production + if [ -e "$php_lib_dir"/php.ini-production.cli ]; then + sudo cp "$php_lib_dir"/php.ini-production.cli "$ini_dir"/php.ini + fi + elif [ "$ini" = "development" ]; then + echo "${ini_file[@]}" | xargs -n 1 -P 6 sudo cp "$php_lib_dir"/php.ini-development + elif [ "$ini" = "none" ]; then + echo '' | sudo tee "${ini_file[@]}" >/dev/null 2>&1 + fi + echo "$ini" | sudo tee "$current_ini" >/dev/null 2>&1 +} + # Function to Setup PHP setup_php() { step_log "Setup PHP" @@ -226,8 +247,9 @@ setup_php() { } # Variables -version=$1 -dist=$2 +version=${1:-'8.1'} +ini=${2:-'production'} +dist=$3 debconf_fix="DEBIAN_FRONTEND=noninteractive" apt_install="sudo $debconf_fix apt-fast install -y --no-install-recommends" scripts="${dist}"/../src/scripts diff --git a/src/scripts/unix.sh b/src/scripts/unix.sh index c864b583..dbee14e0 100644 --- a/src/scripts/unix.sh +++ b/src/scripts/unix.sh @@ -135,6 +135,7 @@ self_hosted_setup() { # Function to configure PHP configure_php() { + add_php_config ini_config_dir="${dist:?}"/../src/configs/ini ini_files=("$ini_config_dir"/php.ini) [[ "$version" =~ $jit_versions ]] && ini_files+=("$ini_config_dir"/jit.ini) diff --git a/src/scripts/win32.ps1 b/src/scripts/win32.ps1 index 8dcd10a8..1fd2c069 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -8,6 +8,11 @@ param ( [ValidateNotNull()] [ValidateLength(1, [int]::MaxValue)] [string] + $ini = 'production', + [Parameter(Position = 2, Mandatory = $true)] + [ValidateNotNull()] + [ValidateLength(1, [int]::MaxValue)] + [string] $dist ) @@ -166,10 +171,18 @@ Function Add-PhpCAInfo { # Function to set PHP config. Function Add-PhpConfig { + $current = Get-Content -Path $php_dir\php.ini-current -ErrorAction SilentlyContinue + if($ini -eq 'development' -or ($ini -eq 'production' -and $current -and $current -ne 'production')) { + Copy-Item -Path $php_dir\php.ini-$ini -Destination $php_dir\php.ini -Force + } elseif ($ini -eq 'none') { + Set-Content -Path $php_dir\php.ini -Value '' + } + Set-Content -Path $php_dir\php.ini-current -Value $ini $ini_config_dir = "$dist\..\src\configs\ini" $ini_files = @("$ini_config_dir\php.ini") $version -match $jit_versions -and ($ini_files += ("$ini_config_dir\jit.ini")) > $null 2>&1 $version -match $xdebug3_versions -and ($ini_files += ("$ini_config_dir\xdebug.ini")) > $null 2>&1 + Add-Content -Path $ini_config_dir\php.ini -Value extension_dir=$ext_dir Get-Content -Path $ini_files | Add-Content -Path $php_dir\php.ini } @@ -257,17 +270,18 @@ if ($null -eq $installed -or -not("$($installed.Version).".StartsWith(($version $extra_version = " ($( Get-Content $php_dir\COMMIT ))" } } else { - Install-Php -Version $version -Architecture $arch -ThreadSafe $ts -InstallVC -Path $php_dir -TimeZone UTC -InitialPhpIni Production -Force > $null 2>&1 + Install-Php -Version $version -Architecture $arch -ThreadSafe $ts -InstallVC -Path $php_dir -TimeZone UTC -InitialPhpIni production -Force > $null 2>&1 } + Add-PhpConfig } catch { } } else { - Set-PhpIniKey -Key 'extension_dir' -Value $ext_dir -Path $php_dir if($env:update -eq 'true') { Update-Php $php_dir >$null 2>&1 $status = "Updated to" } else { $status = "Found" } + Add-PhpConfig } $installed = Get-Php -Path $php_dir @@ -282,7 +296,6 @@ if($version -lt "5.5") { } Enable-PhpExtension -Extension $enable_extensions -Path $php_dir Add-PhpCAInfo -Add-PhpConfig Copy-Item -Path $dist\..\src\configs\pm\*.json -Destination $env:RUNNER_TOOL_CACHE Write-Output "::set-output name=php-version::$($installed.FullVersion)" Add-Log $tick "PHP" "$status PHP $($installed.FullVersion)$extra_version" diff --git a/src/utils.ts b/src/utils.ts index 1692fbc2..bc7992bc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -114,6 +114,22 @@ export async function parseVersion(version: string): Promise { } } +/** + * Function to parse ini file. + * + * @param ini_file + */ +export async function parseIniFile(ini_file: string): Promise { + switch (true) { + case /^(production|development|none)$/.test(ini_file): + return ini_file; + case /php\.ini-(production|development)$/.test(ini_file): + return ini_file.split('-')[1]; + default: + return 'production'; + } +} + /** * Async foreach loop *