Skip to content

Commit

Permalink
Merge pull request #79 from mlocati/install-extension-dependencies-ma…
Browse files Browse the repository at this point in the history
…nually

Add Install-PhpExtensionPrerequisites command
  • Loading branch information
mlocati committed May 10, 2021
2 parents 726451a + 1d25c90 commit ad85e68
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 88 deletions.
2 changes: 1 addition & 1 deletion PhpManager/PhpManager.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# TypesToProcess = @()
# FormatsToProcess = @()
# NestedModules = @()
FunctionsToExport = 'Get-PhpAvailableVersion', 'Install-Php', 'Update-Php', 'Uninstall-Php', 'Get-Php', 'Set-PhpIniKey', 'Get-PhpIniKey', 'Get-PhpExtension', 'Enable-PhpExtension', 'Disable-PhpExtension', 'Install-PhpExtension', 'Update-PhpCAInfo', 'Set-PhpDownloadCache', 'Get-PhpDownloadCache', 'Initialize-PhpSwitcher', 'Add-PhpToSwitcher', 'Get-PhpSwitcher', 'Remove-PhpFromSwitcher', 'Switch-Php', 'Move-PhpSwitcher', 'Remove-PhpSwitcher', 'Install-Composer'
FunctionsToExport = 'Get-PhpAvailableVersion', 'Install-Php', 'Update-Php', 'Uninstall-Php', 'Get-Php', 'Set-PhpIniKey', 'Get-PhpIniKey', 'Get-PhpExtension', 'Enable-PhpExtension', 'Disable-PhpExtension', 'Install-PhpExtension', 'Install-PhpExtensionPrerequisite', 'Update-PhpCAInfo', 'Set-PhpDownloadCache', 'Get-PhpDownloadCache', 'Initialize-PhpSwitcher', 'Add-PhpToSwitcher', 'Get-PhpSwitcher', 'Remove-PhpFromSwitcher', 'Switch-Php', 'Move-PhpSwitcher', 'Remove-PhpSwitcher', 'Install-Composer'
CmdletsToExport = ''
VariablesToExport = ''
AliasesToExport = ''
Expand Down
80 changes: 80 additions & 0 deletions PhpManager/private/Install-ImagickPrerequisite.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
function Install-ImagickPrerequisite() {
<#
.Synopsis
Installs the prerequisites for the imagick PHP extension.
.Parameter PhpVersion
The PhpVersion instance the extension will be installed for.
.Parameter InstallPath
The path to a directory where the prerequisites should be installed.
#>
[OutputType()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNull()]
[PhpVersionInstalled] $PhpVersion,
[Parameter(Mandatory = $true, Position = 1)]
[ValidateNotNull()]
[ValidateLength(1, [int]::MaxValue)]
[string] $InstallPath
)
begin {
}
process {
$rxSearch = '/ImageMagick-[\d\.\-]+-(VC|vc|vs)' + $PhpVersion.VCVersion + '-' + $PhpVersion.Architecture + '\.zip$'
$pageUrl = 'https://windows.php.net/downloads/pecl/deps/'
Set-NetSecurityProtocolType
$webResponse = Invoke-WebRequest -UseBasicParsing -Uri $pageUrl
$zipUrl = $null
foreach ($link in $webResponse.Links) {
$fullUrl = [Uri]::new([Uri]$pageUrl, $link.Href).AbsoluteUri
if ($fullUrl -match $rxSearch) {
$zipUrl = $fullUrl
break
}
}
if ($null -eq $zipUrl) {
throw ('Unable to find the imagick package dependencies on {0} for {1}' -f $pageUrl, $PhpVersion.DisplayName)
}
Write-Verbose "Downloading and extracting $zipUrl"
$zipFile, $keepZipFile = Get-FileFromUrlOrCache -Url $zipUrl
try {
$tempFolder = New-TempDirectory
try {
try {
Expand-ArchiveWith7Zip -ArchivePath $zipFile -DestinationPath $tempFolder
} catch {
$keepZipFile = $false
throw
}
$items = Get-ChildItem -LiteralPath $tempFolder -Recurse -File -Filter *.dll ` | Where-Object { $_.Name -like 'CORE_RL_*.dll' -or $_.Name -like 'IM_MOD_RL_*.dll' }
foreach ($item in $items) {
$destinationPath = [System.IO.Path]::Combine($InstallPath, $item.Name)
Move-Item -LiteralPath $item.FullName -Destination $destinationPath -Force
try {
Reset-Acl -Path $destinationPath
} catch {
Write-Debug -Message "Failed to reset the ACL for $($destinationPath): $($_.Exception.Message)"
}
}
} finally {
try {
Remove-Item -Path $tempFolder -Recurse -Force
} catch {
Write-Debug 'Failed to remove temporary folder'
}
}
} finally {
if (-Not($keepZipFile)) {
try {
Remove-Item -Path $zipFile
} catch {
Write-Debug 'Failed to remove temporary zip file'
}
}
}
}
end {
}
}
85 changes: 0 additions & 85 deletions PhpManager/private/Install-PhpExtensionPrerequisite.ps1

This file was deleted.

10 changes: 8 additions & 2 deletions PhpManager/public/Install-PhpExtension.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
.Parameter Path
The path of the PHP installation.
If omitted we'll use the one found in the PATH environment variable.
.Parameter NoDependncies
Skip the installation of extension dependencies (youl'll have to manually call Install-PhpExtensionPrerequisite before calling Install-PhpExtension).
#>
[OutputType()]
param (
Expand All @@ -47,7 +50,8 @@
[ValidateNotNull()]
[ValidateLength(1, [int]::MaxValue)]
[string] $Path,
[switch] $DontEnable
[switch] $DontEnable,
[switch] $NoDependncies
)
begin {
}
Expand Down Expand Up @@ -222,7 +226,9 @@
}
else {
Write-Verbose ("Installing new extension '{0}' version {1}" -f $newExtension.Name, $newExtension.Version)
Install-PhpExtensionPrerequisite -PhpVersion $phpVersion -Extension $newExtension
if (-not($NoDependncies)) {
Install-PhpExtensionPrerequisite -Extension $newExtension.Handle -PhpPath $phpVersion.ActualFolder
}
if ($null -eq $finalDllName) {
$newExtensionFilename = [System.IO.Path]::Combine($phpVersion.ExtensionsPath, [System.IO.Path]::GetFileName($dllPath))
} else {
Expand Down
64 changes: 64 additions & 0 deletions PhpManager/public/Install-PhpExtensionPrerequisite.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
function Install-PhpExtensionPrerequisite() {
<#
.Synopsis
Installs the prerequisites of PHP extensions.
.Parameter Extension
The name (or the handle) of the PHP extension(s) to be disabled.
.Parameter InstallPath
The path to a directory where the prerequisites should be installed (it should be in your PATH environment variable).
If omitted we'll install the dependencies in the PHP directory.
.Parameter PhpPath
The path to the PHP installation.
If omitted we'll use the one found in the PATH environment variable.
.Example
Install-PhpExtensionPrerequisite imagick,zip
#>
[OutputType()]
param (
[Parameter(Mandatory = $true, Position = 0, HelpMessage = 'The name (or the handle) of the PHP extension(s) to be disabled')]
[ValidateNotNull()]
[ValidateLength(1, [int]::MaxValue)]
[string[]] $Extension,
[Parameter(Mandatory = $false, Position = 1, HelpMessage = 'The path to a directory where the prerequisites should be installed (it should be in your PATH environment variable); if omitted we''ll install the dependencies in the PHP directory')]
[ValidateNotNull()]
[ValidateLength(1, [int]::MaxValue)]
[string] $InstallPath,
[Parameter(Mandatory = $false, Position = 2, HelpMessage = 'The path to the PHP installation; if omitted we''ll use the one found in the PATH environment variable')]
[ValidateNotNull()]
[ValidateLength(1, [int]::MaxValue)]
[string] $PhpPath
)
begin {
}
process {
if ($null -eq $PhpPath -or $PhpPath -eq '') {
$phpVersion = [PhpVersionInstalled]::FromEnvironmentOne()
Write-Verbose "Using PHP found in $($phpVersion.ActualFolder)"
} else {
$phpVersion = [PhpVersionInstalled]::FromPath($Path)
}
if ($null -eq $InstallPath -or $InstallPath -eq '') {
$InstallPath = $phpVersion.ActualFolder
} elseif (-not(Test-Path -LiteralPath $InstallPath -PathType Container)) {
throw "The directory $InstallPath does not exist"
}
foreach ($wantedExtension in $Extension) {
$wantedExtensionHandle = Get-PhpExtensionHandle -Name $wantedExtension
Write-Verbose "Checking prerequisites for $wantedExtensionHandle"
switch ($wantedExtensionHandle) {
imagick {
Install-ImagickPrerequisite -PhpVersion $phpVersion -InstallPath $InstallPath
}
default {
Write-Verbose "No prerequisites needed for $wantedExtensionHandle"
}
}
}
}
end {
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ Install-PhpExtension xdebug -Version 2.6
Install-PhpExtension imagick -MinimumStability snapshot
```

Some extensions require additional dependencies (for example `imagick`).
By default, `Install-PhpExtension` automatically installs these dependencies in the directory where PHP is installed.
If you want to install them in another directory, you have to call the `Install-PhpExtensionPrerequisite` command, and specify the `-NoDependncies` option for `Install-PhpExtension`.

PS: `Install-PhpExtension` can also be used to upgrade (or downgrade) a PHP extension to the most recent version available online.

### Getting the list of PHP versions available online
Expand Down

0 comments on commit ad85e68

Please sign in to comment.