Skip to content

Commit ad85e68

Browse files
authored
Merge pull request #79 from mlocati/install-extension-dependencies-manually
Add Install-PhpExtensionPrerequisites command
2 parents 726451a + 1d25c90 commit ad85e68

File tree

6 files changed

+157
-88
lines changed

6 files changed

+157
-88
lines changed

PhpManager/PhpManager.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# TypesToProcess = @()
2020
# FormatsToProcess = @()
2121
# NestedModules = @()
22-
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'
22+
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'
2323
CmdletsToExport = ''
2424
VariablesToExport = ''
2525
AliasesToExport = ''
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
function Install-ImagickPrerequisite() {
2+
<#
3+
.Synopsis
4+
Installs the prerequisites for the imagick PHP extension.
5+
6+
.Parameter PhpVersion
7+
The PhpVersion instance the extension will be installed for.
8+
9+
.Parameter InstallPath
10+
The path to a directory where the prerequisites should be installed.
11+
#>
12+
[OutputType()]
13+
param (
14+
[Parameter(Mandatory = $true, Position = 0)]
15+
[ValidateNotNull()]
16+
[PhpVersionInstalled] $PhpVersion,
17+
[Parameter(Mandatory = $true, Position = 1)]
18+
[ValidateNotNull()]
19+
[ValidateLength(1, [int]::MaxValue)]
20+
[string] $InstallPath
21+
)
22+
begin {
23+
}
24+
process {
25+
$rxSearch = '/ImageMagick-[\d\.\-]+-(VC|vc|vs)' + $PhpVersion.VCVersion + '-' + $PhpVersion.Architecture + '\.zip$'
26+
$pageUrl = 'https://windows.php.net/downloads/pecl/deps/'
27+
Set-NetSecurityProtocolType
28+
$webResponse = Invoke-WebRequest -UseBasicParsing -Uri $pageUrl
29+
$zipUrl = $null
30+
foreach ($link in $webResponse.Links) {
31+
$fullUrl = [Uri]::new([Uri]$pageUrl, $link.Href).AbsoluteUri
32+
if ($fullUrl -match $rxSearch) {
33+
$zipUrl = $fullUrl
34+
break
35+
}
36+
}
37+
if ($null -eq $zipUrl) {
38+
throw ('Unable to find the imagick package dependencies on {0} for {1}' -f $pageUrl, $PhpVersion.DisplayName)
39+
}
40+
Write-Verbose "Downloading and extracting $zipUrl"
41+
$zipFile, $keepZipFile = Get-FileFromUrlOrCache -Url $zipUrl
42+
try {
43+
$tempFolder = New-TempDirectory
44+
try {
45+
try {
46+
Expand-ArchiveWith7Zip -ArchivePath $zipFile -DestinationPath $tempFolder
47+
} catch {
48+
$keepZipFile = $false
49+
throw
50+
}
51+
$items = Get-ChildItem -LiteralPath $tempFolder -Recurse -File -Filter *.dll ` | Where-Object { $_.Name -like 'CORE_RL_*.dll' -or $_.Name -like 'IM_MOD_RL_*.dll' }
52+
foreach ($item in $items) {
53+
$destinationPath = [System.IO.Path]::Combine($InstallPath, $item.Name)
54+
Move-Item -LiteralPath $item.FullName -Destination $destinationPath -Force
55+
try {
56+
Reset-Acl -Path $destinationPath
57+
} catch {
58+
Write-Debug -Message "Failed to reset the ACL for $($destinationPath): $($_.Exception.Message)"
59+
}
60+
}
61+
} finally {
62+
try {
63+
Remove-Item -Path $tempFolder -Recurse -Force
64+
} catch {
65+
Write-Debug 'Failed to remove temporary folder'
66+
}
67+
}
68+
} finally {
69+
if (-Not($keepZipFile)) {
70+
try {
71+
Remove-Item -Path $zipFile
72+
} catch {
73+
Write-Debug 'Failed to remove temporary zip file'
74+
}
75+
}
76+
}
77+
}
78+
end {
79+
}
80+
}

PhpManager/private/Install-PhpExtensionPrerequisite.ps1

Lines changed: 0 additions & 85 deletions
This file was deleted.

PhpManager/public/Install-PhpExtension.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
.Parameter Path
2525
The path of the PHP installation.
2626
If omitted we'll use the one found in the PATH environment variable.
27+
28+
.Parameter NoDependncies
29+
Skip the installation of extension dependencies (youl'll have to manually call Install-PhpExtensionPrerequisite before calling Install-PhpExtension).
2730
#>
2831
[OutputType()]
2932
param (
@@ -47,7 +50,8 @@
4750
[ValidateNotNull()]
4851
[ValidateLength(1, [int]::MaxValue)]
4952
[string] $Path,
50-
[switch] $DontEnable
53+
[switch] $DontEnable,
54+
[switch] $NoDependncies
5155
)
5256
begin {
5357
}
@@ -222,7 +226,9 @@
222226
}
223227
else {
224228
Write-Verbose ("Installing new extension '{0}' version {1}" -f $newExtension.Name, $newExtension.Version)
225-
Install-PhpExtensionPrerequisite -PhpVersion $phpVersion -Extension $newExtension
229+
if (-not($NoDependncies)) {
230+
Install-PhpExtensionPrerequisite -Extension $newExtension.Handle -PhpPath $phpVersion.ActualFolder
231+
}
226232
if ($null -eq $finalDllName) {
227233
$newExtensionFilename = [System.IO.Path]::Combine($phpVersion.ExtensionsPath, [System.IO.Path]::GetFileName($dllPath))
228234
} else {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
function Install-PhpExtensionPrerequisite() {
2+
<#
3+
.Synopsis
4+
Installs the prerequisites of PHP extensions.
5+
6+
.Parameter Extension
7+
The name (or the handle) of the PHP extension(s) to be disabled.
8+
9+
.Parameter InstallPath
10+
The path to a directory where the prerequisites should be installed (it should be in your PATH environment variable).
11+
If omitted we'll install the dependencies in the PHP directory.
12+
13+
.Parameter PhpPath
14+
The path to the PHP installation.
15+
If omitted we'll use the one found in the PATH environment variable.
16+
17+
.Example
18+
Install-PhpExtensionPrerequisite imagick,zip
19+
#>
20+
[OutputType()]
21+
param (
22+
[Parameter(Mandatory = $true, Position = 0, HelpMessage = 'The name (or the handle) of the PHP extension(s) to be disabled')]
23+
[ValidateNotNull()]
24+
[ValidateLength(1, [int]::MaxValue)]
25+
[string[]] $Extension,
26+
[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')]
27+
[ValidateNotNull()]
28+
[ValidateLength(1, [int]::MaxValue)]
29+
[string] $InstallPath,
30+
[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')]
31+
[ValidateNotNull()]
32+
[ValidateLength(1, [int]::MaxValue)]
33+
[string] $PhpPath
34+
)
35+
begin {
36+
}
37+
process {
38+
if ($null -eq $PhpPath -or $PhpPath -eq '') {
39+
$phpVersion = [PhpVersionInstalled]::FromEnvironmentOne()
40+
Write-Verbose "Using PHP found in $($phpVersion.ActualFolder)"
41+
} else {
42+
$phpVersion = [PhpVersionInstalled]::FromPath($Path)
43+
}
44+
if ($null -eq $InstallPath -or $InstallPath -eq '') {
45+
$InstallPath = $phpVersion.ActualFolder
46+
} elseif (-not(Test-Path -LiteralPath $InstallPath -PathType Container)) {
47+
throw "The directory $InstallPath does not exist"
48+
}
49+
foreach ($wantedExtension in $Extension) {
50+
$wantedExtensionHandle = Get-PhpExtensionHandle -Name $wantedExtension
51+
Write-Verbose "Checking prerequisites for $wantedExtensionHandle"
52+
switch ($wantedExtensionHandle) {
53+
imagick {
54+
Install-ImagickPrerequisite -PhpVersion $phpVersion -InstallPath $InstallPath
55+
}
56+
default {
57+
Write-Verbose "No prerequisites needed for $wantedExtensionHandle"
58+
}
59+
}
60+
}
61+
}
62+
end {
63+
}
64+
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ Install-PhpExtension xdebug -Version 2.6
262262
Install-PhpExtension imagick -MinimumStability snapshot
263263
```
264264

265+
Some extensions require additional dependencies (for example `imagick`).
266+
By default, `Install-PhpExtension` automatically installs these dependencies in the directory where PHP is installed.
267+
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`.
268+
265269
PS: `Install-PhpExtension` can also be used to upgrade (or downgrade) a PHP extension to the most recent version available online.
266270

267271
### Getting the list of PHP versions available online

0 commit comments

Comments
 (0)