Skip to content

Commit 235d5e6

Browse files
committed
Support enabling PHP extensions when multiple extension versions are installed
1 parent ffc6dec commit 235d5e6

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

PhpManager/public/Disable-PhpExtension.ps1

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,23 @@ function Disable-PhpExtension() {
5353
$extensionsToDisable = @()
5454
foreach ($wantedExtension in $Extension) {
5555
$foundExtensions = @($allExtensions | Where-Object { $_.Name -like $wantedExtension })
56-
if ($foundExtensions.Count -ne 1) {
56+
if ($foundExtensions.Count -eq 0) {
5757
$foundExtensions = @($allExtensions | Where-Object { $_.Handle -like $wantedExtension })
5858
if ($foundExtensions.Count -eq 0) {
5959
throw "Unable to find a locally available extension with name (or handle) `"$wantedExtension`": use the Enab-PhpExtension to download it"
6060
}
61-
if ($foundExtensions.Count -ne 1) {
62-
throw "Multiple extensions match the name (or handle) `"$wantedExtension`""
63-
}
64-
}
65-
$extensionToDisable = $foundExtensions[0]
66-
if ($extensionToDisable.State -eq $Script:EXTENSIONSTATE_BUILTIN) {
67-
throw ('The extension "' + $extensionToDisable.Name + '" is builtin: it can''t be disabled')
6861
}
69-
if ($extensionToDisable.State -eq $Script:EXTENSIONSTATE_DISABLED) {
70-
Write-Verbose ('The extension "' + $extensionToDisable.Name + '" is already disabled')
71-
} elseif ($extensionToDisable.State -ne $Script:EXTENSIONSTATE_ENABLED) {
72-
throw ('Unknown extension state: "' + $extensionToDisable.State + '"')
73-
} else {
74-
$extensionsToDisable += $extensionToDisable
62+
foreach ($extensionToDisable in $foundExtensions) {
63+
if ($extensionToDisable.State -eq $Script:EXTENSIONSTATE_BUILTIN) {
64+
throw ('The extension "' + $extensionToDisable.Name + '" is builtin: it can''t be disabled')
65+
}
66+
if ($extensionToDisable.State -eq $Script:EXTENSIONSTATE_DISABLED) {
67+
Write-Verbose ('The extension "' + $extensionToDisable.Name + '" is already disabled')
68+
} elseif ($extensionToDisable.State -ne $Script:EXTENSIONSTATE_ENABLED) {
69+
throw ('Unknown extension state: "' + $extensionToDisable.State + '"')
70+
} else {
71+
$extensionsToDisable += $extensionToDisable
72+
}
7573
}
7674
}
7775
if ($extensionsToDisable) {
@@ -109,13 +107,12 @@ function Disable-PhpExtension() {
109107
}
110108
}
111109
}
112-
if (-Not($disabled)) {
113-
throw "The entry in the php.ini file has not been found (?)"
110+
if ($disabled) {
111+
Set-PhpIniLine -Path $iniPath -Lines $newIniLines
112+
$extensionToDisable.State = $Script:EXTENSIONSTATE_ENABLED
113+
Write-Verbose ('The extension ' + $extensionToDisable.Name + ' v' + $extensionToDisable.Version + ' has been disabled')
114+
$iniLines = $newIniLines
114115
}
115-
Set-PhpIniLine -Path $iniPath -Lines $newIniLines
116-
$extensionToDisable.State = $Script:EXTENSIONSTATE_ENABLED
117-
Write-Verbose ('The extension ' + $extensionToDisable.Name + ' v' + $extensionToDisable.Version + ' has been disabled')
118-
$iniLines = $newIniLines
119116
}
120117
}
121118
}

PhpManager/public/Enable-PhpExtension.ps1

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,22 @@ function Enable-PhpExtension() {
4545
$extensionsToEnable = @()
4646
$allExtensions = Get-PhpExtension -Path $phpVersion.ExecutablePath
4747
foreach ($wantedExtension in $Extension) {
48-
$foundExtensions = @($allExtensions | Where-Object { $_.Name -like $wantedExtension })
48+
$match = $wantedExtension | Select-String -Pattern '^([^:]*)(?::(.*))?$'
49+
$wantedExtensionName = $match.Matches.Groups[1].Value
50+
$wantedExtensionVersion = $match.Matches.Groups[2].Value
51+
$foundExtensions = @($allExtensions | Where-Object { $_.Name -like $wantedExtensionName -and $_.Version -like ($wantedExtensionVersion + '*') })
4952
if ($foundExtensions.Count -ne 1) {
50-
$foundExtensions = @($allExtensions | Where-Object { $_.Handle -like $wantedExtension })
53+
$foundExtensions = @($allExtensions | Where-Object { $_.Handle -like $wantedExtensionName -and $_.Version -like ($wantedExtensionVersion + '*') })
5154
if ($foundExtensions.Count -eq 0) {
5255
throw "Unable to find a locally available extension with name (or handle) `"$Extension`": use the Install-PhpExtension to download it"
5356
}
5457
if ($foundExtensions.Count -ne 1) {
55-
throw "Multiple extensions match the name (or handle) `"$Extension`""
58+
$msg = "Multiple extensions match the name (or handle) `"$Extension`":"
59+
foreach ($foundExtension in $foundExtensions) {
60+
$msg += "`n- handle: $($foundExtension.Handle) version $($foundExtension.Version)"
61+
}
62+
$msg += "`nYou can filter the extension to enable by adding :version to the -Extension parameter (example: `"-Extension '$($foundExtension.Handle):$($foundExtension.Version)'`")"
63+
throw $msg
5664
}
5765
}
5866
$extensionsToEnable += $foundExtensions[0]

test/tests/Install-Enable-Extensions.Tests.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ Describe 'Install-Enable-Extensions' {
5858
$imagick.Type | Should -BeExactly 'Php'
5959
$imagick.State | Should -BeExactly 'Disabled'
6060
}
61+
It -Name 'should handle multiple extension versions' {
62+
$phpPath = Join-Path -Path $Global:PHPMANAGER_TESTPATH -ChildPath installs | Join-Path -ChildPath (New-Guid).Guid
63+
Install-Php -Version 7.1 -Architecture x64 -ThreadSafe $true -Path $phpPath
64+
$phpVersion = Get-Php -Path $phpPath
65+
try {
66+
Install-PhpExtension -Path $phpPath -Extension xdebug -Version 2.6 -DontEnable
67+
Move-Item -LiteralPath (Join-Path -Path $phpVersion.ExtensionsPath -ChildPath 'php_xdebug.dll') -Destination (Join-Path -Path $phpVersion.ExtensionsPath -ChildPath 'php_xdebug-2.6')
68+
Install-PhpExtension -Path $phpPath -Extension xdebug -Version 2.7 -MinimumStability alpha -DontEnable
69+
Move-Item -LiteralPath (Join-Path -Path $phpVersion.ExtensionsPath -ChildPath 'php_xdebug.dll') -Destination (Join-Path -Path $phpVersion.ExtensionsPath -ChildPath 'php_xdebug-2.7')
70+
Move-Item -LiteralPath (Join-Path -Path $phpVersion.ExtensionsPath -ChildPath 'php_xdebug-2.6') -Destination (Join-Path -Path $phpVersion.ExtensionsPath -ChildPath 'php_xdebug-2.6.dll')
71+
Move-Item -LiteralPath (Join-Path -Path $phpVersion.ExtensionsPath -ChildPath 'php_xdebug-2.7') -Destination (Join-Path -Path $phpVersion.ExtensionsPath -ChildPath 'php_xdebug-2.7.dll')
72+
{ Enable-PhpExtension -Path $phpPath -Extension xdebug } | Should -Throw
73+
{ Enable-PhpExtension -Path $phpPath -Extension 'xdebug:2.6' } | Should -Not -Throw
74+
Disable-PhpExtension -Path $phpPath -Extension xdebug
75+
{ Enable-PhpExtension -Path $phpPath -Extension 'xdebug:2.7' } | Should -Not -Throw
76+
} finally {
77+
try {
78+
Remove-Item -LiteralPath $phpPath -Recurse
79+
} catch {
80+
}
81+
}
82+
}
6183
} finally {
6284
foreach ($testCase in $testCases) {
6385
if (Test-Path -LiteralPath $testCase.path) {

0 commit comments

Comments
 (0)