Php.psm1

Function Install-Php {
    Param(
        [String] $Version = '7',
        [String[]] $Extensions = @(),
        [String] $Path = [Environment]::GetFolderPath('ApplicationData') + '\Php'
    )
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    If (Test-Path -Path $Path) {
        Remove-Item -Path $Path -Force -Recurse | Out-Null
    }
    New-Item -ItemType Directory -Path $Path | Out-Null
    $Architecture = If ([System.Environment]::Is64BitOperatingSystem) { '-x64' } Else { '-x86' }
    $UriBase = 'https://windows.php.net'
    $Response = Invoke-WebRequest -UseBasicParsing -Uri ($UriBase + '/download')
    $PathDownload = ($Response.Links | Where-Object -Property 'Href' -Match "downloads/releases/(php-$Version[\d.]*-Win32-VC[\d]+$Architecture.zip)" | Select-Object -First 1).Href
    $FileZip = $Matches[1]
    $PathFileZip = ([System.IO.Path]::GetTempPath() + $FileZip)
    Invoke-WebRequest -UseBasicParsing ($UriBase + $PathDownload) -OutFile $PathFileZip
    Expand-Archive -Path $PathFileZip -DestinationPath $Path
    Remove-Item -Path $PathFileZip
    ForEach ($Target in 'User', 'Process') {
        $EnvPaths = [System.Environment]::GetEnvironmentVariable('Path', $Target).Split(';')
        If ($EnvPaths -NotContains $Path) {
            [System.Environment]::SetEnvironmentVariable('Path', (($EnvPaths + $Path) -Join ';'), $Target)
        }
    }
    Set-PhpExtensions -Names $Extensions
}

Function Uninstall-Php {
    Param(
        [String] $Path = [Environment]::GetFolderPath('ApplicationData') + '\Php'
    )
    Remove-Item -Path $Path -Force -Recurse
    ForEach ($Target in 'User', 'Process') {
        $EnvPaths = ([System.Environment]::GetEnvironmentVariable('Path', $Target)).Split(';')
        [System.Environment]::SetEnvironmentVariable('Path', (($EnvPaths | Where-Object { $_ -Ne $Path }) -Join ';'), $Target)
    }
}

Function Set-PhpExtensions {
    Param(
        [String[]] $Names = @()
    )
    $Php = Get-Command -Name php -ErrorAction Stop
    $Path = Split-Path -Path $Php.Source
    $PathFileIni = $Path + '\php.ini'
    If (Test-Path -Path $PathFileIni) {
        $Config = [String] (Get-Content -Path $PathFileIni | Select-String -Pattern 'zend_extension\s*=|extension\s*=|xdebug.' -NotMatch)
    }
    Else {
        $Config = ''
    }
    ForEach ($Name in $Names) {
        If ($Name -eq 'xdebug') {
            $Architecture = If ([System.Environment]::Is64BitOperatingSystem) { '-x86_64' } Else { '' }
            $VersionPhpMinor = [String]$Php.Version.Major + '.' + [String]$Php.Version.Minor
            $UriBase = 'http://xdebug.org'
            $Response = Invoke-WebRequest -UseBasicParsing -Uri ($UriBase + '/download.php')
            $UriDownload = ($Response.Links | Where-Object -Property 'href' -Match "files/(php_xdebug-[\d.]+-$VersionPhpMinor-vc[\d]+$Architecture.dll)" | Select-Object -First 1).href
            $PathFileXdebug = $Path + '\ext\' + [String]$Matches[1]
            Invoke-WebRequest -UseBasicParsing ($UriBase + '/' + $UriDownload) -OutFile $PathFileXdebug
            $Config += "`nzend_extension = `"$PathFileXdebug`"`nxdebug.remote_enable = on`nxdebug.remote_autostart = on"
        }
        Else {
            If (Test-Path -Path "$Path\ext\php_$Name.dll") {
                $Config += "`nextension = `"$Path\ext\php_$Name.dll`""
            }
        }
    }
    Set-Content -Path $PathFileIni -Value $Config
}

Function Get-PhpExtensions {
    $Php = Get-Command -Name php -ErrorAction Stop
    $Path = Split-Path -Path $Php.Source
    $PathFileIni = $Path + '\php.ini'
    (Get-Content -Path $PathFileIni | Select-String -Pattern '(zend_extension|extension)\s*=.*php_(\w+)').Matches |
        ForEach-Object {
        If ($_.Groups) {
            Write-Output $_.Groups[2].Value
        }
    }
}