Skip to content

Instantly share code, notes, and snippets.

@OlinB
Last active October 22, 2025 13:45
Show Gist options
  • Select an option

  • Save OlinB/ae14ba3325018a988a0f9c994b394b4c to your computer and use it in GitHub Desktop.

Select an option

Save OlinB/ae14ba3325018a988a0f9c994b394b4c to your computer and use it in GitHub Desktop.
Toggle Herd debug
<#
.SYNOPSIS
Toggles PHP Xdebug in Laravel Herd by commenting/uncommenting lines in php.ini files
.DESCRIPTION
This script finds all php.ini files in Herd's bin directory and toggles the Xdebug configuration
by commenting or uncommenting the relevant lines.
.EXAMPLE
.\Toggle-HerdDebug.ps1
#>
# Path to Herd's PHP bin directory
$herdBinPath = "$env:USERPROFILE\.config\herd\bin"
# Xdebug line patterns to look for
$xdebugPatterns = @(
'zend_extension=.*xdebug.*\.dll',
'xdebug\.mode=',
'xdebug\.start_with_request=',
'xdebug\.start_upon_error='
)
# Check if Herd bin directory exists
if (-not (Test-Path $herdBinPath)) {
Write-Error "Herd bin directory not found at: $herdBinPath"
Write-Host "Please check your Herd installation."
exit 1
}
# Find all php.ini files
$phpIniFiles = Get-ChildItem -Path $herdBinPath -Recurse -Filter "php.ini" | Where-Object { $_.DirectoryName -match "php\d+" }
if ($phpIniFiles.Count -eq 0) {
Write-Error "No php.ini files found in Herd bin directory"
exit 1
}
Write-Host "Found $($phpIniFiles.Count) php.ini file(s)" -ForegroundColor Cyan
# Determine current state by checking the first file
$firstFile = Get-Content $phpIniFiles[0].FullName
$isCurrentlyDisabled = $firstFile -match '^\s*;zend_extension=.*xdebug'
Write-Host "`nReading file: " $($phpIniFiles[0].FullName) -ForegroundColor Gray
$action = if ($isCurrentlyDisabled) { "Enabling" } else { "Disabling" }
Write-Host "`n$action Xdebug..." -ForegroundColor Yellow
# Process each php.ini file
foreach ($iniFile in $phpIniFiles) {
Write-Host "`nProcessing: $($iniFile.FullName)" -ForegroundColor Gray
try {
$content = Get-Content $iniFile.FullName
$modified = $false
$newContent = @()
foreach ($line in $content) {
$isXdebugLine = $false
# Check if this line matches any Xdebug pattern
foreach ($pattern in $xdebugPatterns) {
if ($line -match $pattern) {
$isXdebugLine = $true
break
}
}
if ($isXdebugLine) {
if ($isCurrentlyDisabled) {
# Enable: Remove leading semicolon and whitespace
if ($line -match '^\s*;(.*)') {
$newLine = $matches[1]
$newContent += $newLine
$modified = $true
# Write-Host " ✓ Uncommented: $newLine" -ForegroundColor Green
} else {
$newContent += $line
}
} else {
# Disable: Add semicolon if not already commented
if ($line -notmatch '^\s*;') {
$newLine = ";$line"
$newContent += $newLine
$modified = $true
# Write-Host " ✓ Commented: $newLine" -ForegroundColor Green
} else {
$newContent += $line
}
}
} else {
$newContent += $line
}
}
# Write back to file if modified
if ($modified) {
$newContent | Set-Content $iniFile.FullName -Encoding UTF8
Write-Host " File updated successfully" -ForegroundColor Cyan
} else {
Write-Host " No changes needed" -ForegroundColor Gray
}
} catch {
Write-Error "Failed to process $($iniFile.FullName): $_"
}
}
# Final status
$finalState = if ($isCurrentlyDisabled) { "ENABLED" } else { "DISABLED" }
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host "Xdebug is now: $finalState" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Restarting Herd..." -ForegroundColor Yellow
herd restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment