mlocati / powershell-phpmanager

A PowerShell module to install/update PHP, PHP extensions and Composer on Windows

Home Page:https://www.powershellgallery.com/packages/PhpManager

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ExtensionDetail from DLL Path causing error when using local file and not PECL

jmwamser opened this issue · comments

Due to PECL not having DLLs for the pdo_sqlsrv and sqlsrv for PHP 8+, I am trying to use PhpManager to install the files I have downloaded from the mssql git repo.

When I run Install-PhpExtension -Path C:\PHP\82 C:\temp\php_sqlsrv_82_nts.dll I receive this error: The variable '$newExtension' cannot be retrieved because it has not been set.

Looking into the Install-PhpExtension script I found the following line that I believe is causing the issue.

$oldExtension = Get-PhpExtension -Path $phpVersion.ExecutablePath | Where-Object { $_.Handle -eq $newExtension.Handle }

I believe this is because the variable is only set in the else part of the if statement on line 212.

if (Test-Path -Path $Extension -PathType Leaf) {
if ($Version -ne '') {
throw 'You can''t specify the -Version argument if you specify an existing file with the -Extension argument'
}
if ($null -ne $MinimumStability -and $MinimumStability -ne '') {
throw 'You can''t specify the -MinimumStability argument if you specify an existing file with the -Extension argument'
}
if ($null -ne $MaximumStability -and $MaximumStability -ne '') {
throw 'You can''t specify the -MaximumStability argument if you specify an existing file with the -Extension argument'
}
$dllPath = [System.IO.Path]::GetFullPath($Extension)
}
else {
if ($null -eq $MinimumStability -or $MinimumStability -eq '') {
$MinimumStability = $Script:PEARSTATE_STABLE
}
if ($null -eq $MaximumStability -or $MaximumStability -eq '') {
$MaximumStability = $Script:PEARSTATE_STABLE
}
$peclPackages = @(Get-PeclAvailablePackage)
$foundPeclPackages = @($peclPackages | Where-Object { $_ -eq $Extension })
if ($foundPeclPackages.Count -ne 1) {
$foundPeclPackages = @($peclPackages | Where-Object { $_ -like "*$Extension*" })
if ($foundPeclPackages.Count -eq 0) {
throw "No PECL extensions found containing '$Extension'"
}
if ($foundPeclPackages.Count -ne 1) {
throw ("Multiple PECL extensions found containing '$Extension':`n - " + [String]::Join("`n - ", $foundPeclPackages))
}
}
$peclPackageHandle = $foundPeclPackages[0]
switch ($peclPackageHandle) {
'xdebug' {
$availablePackageVersion = Get-XdebugExtension -PhpVersion $phpVersion -Version $Version -MinimumStability $MinimumStability -MaximumStability $MaximumStability
if ($null -ne $availablePackageVersion) {
$remoteFileIsZip = $false
$finalDllName = 'php_xdebug.dll'
} else {
$finalDllName = $null
}
}
default {
$availablePackageVersion = $null
$finalDllName = $null
}
}
if ($null -eq $availablePackageVersion) {
$peclPackageVersions = @(Get-PeclPackageVersion -Handle $peclPackageHandle -Version $Version -MinimumStability $MinimumStability -MaximumStability $MaximumStability)
if ($peclPackageVersions.Count -eq 0) {
if ($Version -eq '') {
throw "The PECL package $peclPackageHandle does not have any version with a stability between $MinimumStability and $MaximumStability"
}
throw "The PECL package $peclPackageHandle does not have any $Version version with a stability between $MinimumStability and $MaximumStability"
}
foreach ($peclPackageVersion in $peclPackageVersions) {
$archiveUrl = Get-PeclArchiveUrl -PackageHandle $peclPackageHandle -PackageVersion $peclPackageVersion -PhpVersion $phpVersion -MinimumStability $MinimumStability -MaximumStability $MaximumStability
if ($archiveUrl -eq '') {
Write-Verbose ("No Windows DLLs found for PECL package {0} {1} compatible with {2}" -f $peclPackageHandle, $peclPackageVersion, $phpVersion.DisplayName)
}
else {
$availablePackageVersion = @{PackageVersion = $peclPackageVersion; PackageArchiveUrl = $archiveUrl }
$remoteFileIsZip = $true
break
}
}
if ($null -eq $availablePackageVersion) {
throw "No compatible Windows DLL found for PECL package $peclPackageHandle with a stability between $MinimumStability and $MaximumStability"
}
}
Write-Verbose ("Downloading PECL package {0} {1} from {2}" -f $peclPackageHandle, $availablePackageVersion.PackageVersion, $availablePackageVersion.PackageArchiveUrl)
$downloadedFile, $keepDownloadedFile = Get-FileFromUrlOrCache -Url $availablePackageVersion.PackageArchiveUrl
$additionalFiles = @()
try {
if ($remoteFileIsZip) {
$tempFolder = New-TempDirectory
Expand-ArchiveWith7Zip -ArchivePath $downloadedFile -DestinationPath $tempFolder
$phpDlls = @(Get-ChildItem -Path $tempFolder\php_*.dll -File -Depth 0)
if ($phpDlls.Count -eq 0) {
$phpDlls = @(Get-ChildItem -Path $tempFolder\php_*.dll -File -Depth 1)
}
if ($phpDlls.Count -eq 0) {
throw ("No PHP DLL found in archive downloaded from {0}" -f $availablePackageVersion.PackageArchiveUrl)
}
if ($phpDlls.Count -ne 1) {
throw ("Multiple PHP DLL found in archive downloaded from {0}" -f $availablePackageVersion.PackageArchiveUrl)
}
$dllPath = $phpDlls[0].FullName
switch ($peclPackageHandle) {
'couchbase' {
$libcouchbaseDll = Join-Path -Path $tempFolder -ChildPath 'libcouchbase.dll'
if (Test-Path -LiteralPath $libcouchbaseDll -PathType Leaf) {
$additionalFiles += $libcouchbaseDll
}
}
'decimal' {
$libmpdecDll = Join-Path -Path $tempFolder -ChildPath 'libmpdec.dll'
if (Test-Path -LiteralPath $libmpdecDll -PathType Leaf) {
$additionalFiles += $libmpdecDll
}
}
'imagick' {
$additionalFiles += @(Get-ChildItem -Path $tempFolder\CORE_*.dll -File -Depth 1)
$additionalFiles += @(Get-ChildItem -Path $tempFolder\IM_*.dll -File -Depth 1)
$additionalFiles += @(Get-ChildItem -Path $tempFolder\FILTER_*.dll -File -Depth 1)
}
'memcached' {
$libmemcachedDll = Join-Path -Path $tempFolder -ChildPath 'libmemcached.dll'
$libhashkitDll = Join-Path -Path $tempFolder -ChildPath 'libhashkit.dll'
if (Test-Path -LiteralPath $libmemcachedDll -PathType Leaf) {
$additionalFiles += $libmemcachedDll
}
if (Test-Path -LiteralPath $libhashkitDll -PathType Leaf) {
$additionalFiles += $libhashkitDll
}
}
'yaml' {
$yamlDll = Join-Path -Path $tempFolder -ChildPath 'yaml.dll'
if (Test-Path -LiteralPath $yamlDll -PathType Leaf) {
$additionalFiles += $yamlDll
}
}
}
}
else {
$keepDownloadedFile = $true
$dllPath = $downloadedFile
}
$newExtension = Get-PhpExtensionDetail -PhpVersion $phpVersion -Path $dllPath
}
catch {
$keepDownloadedFile = $false
throw
}
finally {
if (-Not($keepDownloadedFile)) {
try {
Remove-Item -Path $downloadedFile -Force
}
catch {
Write-Debug 'Failed to remove temporary zip file'
}
}
}
}

$newExtension = Get-PhpExtensionDetail -PhpVersion $phpVersion -Path $dllPath

I wonder if moving line 212 right after the closing if statement bracket on line 228 would fix this issue.

Thank you for your hard work on this! I'll see if I can get a pull request put together.

Thanks for the report!

Install-PhpExtension -Path C:\PHP\82 C:\temp\php_sqlsrv_82_nts.dll

Did you mean Install-PhpExtension -Extension C:\PHP\82 C:\temp\php_sqlsrv_82_nts.dll, right?

I wonder if moving line 212 right after the closing if statement bracket on line 228 would fix this issue.

Nope, I think #114 is a better fix.

Thank you for the quick update. This fixed the issue I was experiencing.

I'll take a look at #114 so I can learn a bit more.