Skip to content

Instantly share code, notes, and snippets.

@nesk
Last active April 24, 2020 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesk/ed5932992904fc77a8874d3278826e26 to your computer and use it in GitHub Desktop.
Save nesk/ed5932992904fc77a8874d3278826e26 to your computer and use it in GitHub Desktop.
Xdebug Toggler: A simple command line tool to enable/disable Xdebug in your PHP config (only if installed via Homebrew on macOS).
#!/usr/bin/env php
<?php
/**
* Requirements: PHP installed via Homebrew on macOS.
*
* Installation: Copy the content of this file in `~/xdebug`.
*
* Usage:
*
* ~/xdebug enable
* ~/xdebug disable
*/
$command = $argv[1] ?? '(none)';
if (!in_array($command, ['enable', 'disable'], true)) {
echo "Unknown $command command.\n";
exit(1);
}
$version = implode('.', array_slice(explode('.', phpversion()), 0, 2));
$iniPath = "/usr/local/etc/php/$version/php.ini";
if (!file_exists($iniPath)) {
echo "Your PHP config can't be found at path \"$iniPath\".\n";
exit(2);
}
$config = file_get_contents($iniPath);
$applyConfig = function () use ($version) {
echo "Restarting PHP-FPM...\n";
exec("brew services restart php@$version", $output, $exitCode);
if ($exitCode !== 0) {
echo "An error occured while restarting php@$version:\n\n";
echo implode("\n", $output) . "\n";
exit(3);
}
};
echo "Using config at \"$iniPath\".\n\n";
if ($command === 'enable') {
if ($config[0] === ';') {
echo "Rewriting PHP config...\n";
file_put_contents($iniPath, mb_substr($config, 1));
$applyConfig();
echo "Xdebug has been successfully enabled.\n";
} else {
echo "Xdebug is already enabled.\n";
exit(4);
}
} else {
if ($config[0] !== ';') {
echo "Rewriting PHP config...\n";
file_put_contents($iniPath, ";$config");
$applyConfig();
echo "Xdebug has been successfully disabled.\n";
} else {
echo "Xdebug is already disabled.\n";
exit(4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment