# Define variables $BASE_URL = "https://download.herdphp.com" $INSTALL_DIR = "$HOME\.config\herd-lite\bin" $PHP_BIN = "$INSTALL_DIR\php.exe" $COMPOSER_BIN = "$INSTALL_DIR\composer.phar" $COMPOSER_BAT = "$INSTALL_DIR\composer.bat" $LARAVEL_BIN = "$INSTALL_DIR\laravel.phar" $LARAVEL_BAT = "$INSTALL_DIR\laravel.bat" $PHP_INI = "$INSTALL_DIR\php.ini" # Create the directory if it doesn't exist if (-Not (Test-Path -Path $INSTALL_DIR)) { New-Item -ItemType Directory -Path $INSTALL_DIR | Out-Null } function Show-Spinner { param ( [int]$processId ) $spinner = @('|', '/', '-', '\') $i = 0 while (Get-Process -Id $processId -ErrorAction SilentlyContinue) { Write-Host -NoNewline -ForegroundColor Yellow ("`b" + $spinner[$i++ % $spinner.Length]) Start-Sleep -Milliseconds 100 } Write-Host -NoNewline "`b `b`n" } function Download-With-Spinner { param ( [string]$url, [string]$outputFile ) $webClient = New-Object System.Net.WebClient $downloadJob = Start-Job -ScriptBlock { param ($url, $outputFile) $webClient = New-Object System.Net.WebClient $webClient.DownloadFile($url, $outputFile) } -ArgumentList $url, $outputFile Show-Spinner -processId $downloadJob.Id Wait-Job -Id $downloadJob.Id Remove-Job -Id $downloadJob.Id if (Test-Path -Path $outputFile) { } else { Write-Host "Failed to download file from $url." -ForegroundColor Red exit 1 } } function Info { param ( [string]$message ) Write-Host " INFO $message" -BackgroundColor Blue -ForegroundColor White } function Success { param ( [string]$message ) Write-Host " SUCCESS $message " -BackgroundColor Green -ForegroundColor Black } function Error { param ( [string]$message ) Write-Host " ERROR $message " -BackgroundColor Red -ForegroundColor White } Clear-Host if (-Not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Error "Please run this script as an administrator." exit 1 } $HERD_IS_INSTALLED = Test-Path "$HOME\.config\herd\bin\php.bat" if ($HERD_IS_INSTALLED) { Info "You already use Laravel Herd - are you sure you want to install another copy of PHP? (y/n) " $response = Read-Host if ($response -ne "y") { exit 0 } } Info "Downloading PHP binary... " Download-With-Spinner "$BASE_URL/herd-lite/windows/php.exe" "$PHP_BIN" Info "Creating php.ini... " if (-Not (Test-Path -Path $PHP_INI)) { New-Item -ItemType File -Path $PHP_INI | Out-Null # Write content to php.ini @" variables_order = "EGPCS" opcache.enable=1 opcache.enable_cli=1 "@ | Set-Content -Path $PHP_INI } Info "Downloading Composer binary... " Download-With-Spinner "$BASE_URL/herd-lite/composer" "$COMPOSER_BIN" Download-With-Spinner "$BASE_URL/herd-lite/windows/composer.bat" "$COMPOSER_BAT" Info "Downloading Laravel Installer... " Download-With-Spinner "$BASE_URL/herd-lite/laravel" "$LARAVEL_BIN" Download-With-Spinner "$BASE_URL/herd-lite/windows/laravel.bat" "$LARAVEL_BAT" # Add the installation directory to the PATH if not already present $path = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User) if ($path -notlike "*$INSTALL_DIR*") { Info "Adding $INSTALL_DIR to your PATH... `n" [System.Environment]::SetEnvironmentVariable("Path", "$INSTALL_DIR;$path", [System.EnvironmentVariableTarget]::User) Info "Added $INSTALL_DIR to PATH. Please restart your terminal to apply changes. `n" # Add the installation directory to the PATH for the current session $env:Path += ";$INSTALL_DIR" # Update current process PATH environment variable if it needs updating. if ($env:Path -notlike "*$INSTALL_DIR*") { $env:Path = [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine); } } else { Info "$INSTALL_DIR is already in your PATH. `n" } function Box { param ( [string]$msg, [string]$content, [string]$content2, [string]$content3 ) $width = 70 # Total width of the box $msgLength = $msg.Length $filler = $width - $msgLength - 5 $contentLength = $content.Length $contentFiller = $width - 4 - $contentLength + 10 $content2Length = $content2.Length $content2Filler = $width - 4 - $content2Length + 8 $footerWidth = $width - 3 Write-Host "| " -NoNewline Write-Host -ForegroundColor Green $msg -NoNewline Write-Host ("-" * $filler) Write-Host "| $content" Write-Host "|" Write-Host "| $content2" Write-Host "| $content3" Write-Host "|" -NoNewline Write-Host ("-" * $footerWidth) } # Example usage Write-Host "" Box "Success!" "php, composer, and laravel have been installed successfully." "For a fully-featured dev environment for PHP, check out" "Laravel Herd. https://herd.laravel.com"