$script:versions = @{ }

function Add-PhpVersion {
    [CmdletBinding()]
    param(
        [Parameter(Position=0, Mandatory=$true)]
        [Alias('Ver', 'Php')]
        [string]
        $Version,

        [Parameter(Position=1, Mandatory=$true)]
        [string]
        $Path,

        [Parameter(Mandatory=$false)]
        [switch]
        $Set = $false
    )

    $php = Join-Path $Path 'php.exe'

    if (-not (Test-Path $php)) {
        Write-Error "PHP is not installed in that directory, maybe you have misspeled that?"
        return
    }

    $script:versions[$Version] = $Path;

    if ($Set) {
        Set-PhpVersion $Version
    }
}

function Set-PhpVersion {
    [CmdletBinding()]
    param(
        [Parameter(Position=0)]
        [string]
        $Version
    );

    if (-not $script:versions.ContainsKey($Version)) {
        Write-Error "You have not declared $Version to be installed, maybe you should call Add-PhpVersion?"
        return
    }

    $env:Path = (@($env:Path -split ';' | Where-Object { (-not $script:versions.ContainsKey($_)) -and $_ }) + @($script:versions[$Version])) -join ';'
    $env:PHP_VER = $Version;

    Write-Output "PHP Version set to $Version installed in $($script:versions[$Version])"
}