Skip to content

Instantly share code, notes, and snippets.

@black-silence
Created March 4, 2021 14:23
Show Gist options
  • Save black-silence/74769c734dac4cb5ca1cb3ba750b8354 to your computer and use it in GitHub Desktop.
Save black-silence/74769c734dac4cb5ca1cb3ba750b8354 to your computer and use it in GitHub Desktop.
Create copy of php.ini without xdebug. When using phan, I don't want to have xdebug in the php.ini but I don't want to maintain a separate config file for phan. So I use php to create a custom php.ini without xdebug, then call php with the custom config to run phan.
<?php
declare(strict_types=1);
/**
* This reads the php.ini the current CLI process is using and creates a copy
* so we can kick out xdebug and use the created config for phan without having to remove xdebug globally.
*/
function cleanIniFile(string $content): string {
$noComments = preg_replace("/^;.*\n/m", '', $content);
return preg_replace("/\n+/m", "\n", $noComments);
}
// Fetch ini-file info from current phpinfo
ob_start();
phpinfo(INFO_GENERAL);
$general = ob_get_clean();
$matches = [];
if (preg_match('/^Loaded Configuration File => (.*)$/m', $general, $matches)) {
$configFilename = $matches[1];
} else {
throw new RuntimeException('Config file name not detected');
}
// Get basic php.ini without comments
$iniContent = cleanIniFile(file_get_contents($configFilename));
// Add all extra loaded ini files into the custom php.ini
if (preg_match("/Additional \.ini files parsed => (.*?)\n\n/s", $general, $matches)) {
$extraInis = explode(',', $matches[1]);
foreach ($extraInis as $extra) {
if (strpos($extra, 'xdebug') !== false) {
continue;
}
$filename = trim($extra);
$content = cleanIniFile(file_get_contents($filename));
$iniContent .= "\n" . trim($content);
}
}
file_put_contents(__DIR__ . '/phan-php.ini', $iniContent);
@black-silence
Copy link
Author

Usage: php create-php-ini.php && php -n -c phan-php.ini vendor/bin/phan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment