1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
<?php
/*
* This file is part of composer/xdebug-handler.
*
* (c) Composer <https://github.com/composer>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Composer\XdebugHandler\Tests\Helpers;
use Composer\XdebugHandler\Tests\Mocks\CoreMock;
/**
* This helper class allows us to mock the php ini files that the process would
* otherwise report via php_ini_loaded_file and php_ini_scanned_files.
*/
class IniHelper
{
/** @var string */
protected $loadedIni;
/** @var string */
protected $scanDir;
/** @var string[] */
protected $files;
/**
* @var null|array
* @phpstan-var null|array{0: false|string, 1: false|string}
*/
protected $envOptions;
/**
* envOptions is an array of additional environment values to set,
* comprising: [PHP_INI_SCAN_DIR, optional PHPRC]
*
* @phpstan-param null|array{0: false|string, 1: false|string} $envOptions
*/
public function __construct(?array $envOptions = null)
{
$this->envOptions = $envOptions;
$base = dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures';
$this->loadedIni = $base.DIRECTORY_SEPARATOR.'php.ini';
$this->scanDir = $base.DIRECTORY_SEPARATOR.'scandir';
}
public function setNoInis(): void
{
// Must have at least one entry
$this->files = [''];
$this->setEnvironment();
}
public function setLoadedIni(): void
{
$this->files = [
$this->loadedIni,
];
$this->setEnvironment();
}
public function setScannedInis(): void
{
$this->files = [
'',
$this->scanDir.DIRECTORY_SEPARATOR.'scan-one.ini',
$this->scanDir.DIRECTORY_SEPARATOR.'scan-two.ini',
$this->scanDir.DIRECTORY_SEPARATOR.'scan-empty.ini',
];
$this->setEnvironment();
}
public function setAllInis(): void
{
$this->files = [
$this->loadedIni,
$this->scanDir.DIRECTORY_SEPARATOR.'scan-one.ini',
$this->scanDir.DIRECTORY_SEPARATOR.'scan-two.ini',
$this->scanDir.DIRECTORY_SEPARATOR.'scan-empty.ini',
];
$this->setEnvironment();
}
public function setInaccessibleIni(): void
{
$this->files = [
'',
$this->scanDir.DIRECTORY_SEPARATOR.'scan-one.ini',
$this->scanDir.DIRECTORY_SEPARATOR.'scan-two.ini',
$this->scanDir.DIRECTORY_SEPARATOR.'scan-missing.ini',
];
$this->setEnvironment();
}
public function setSectionInis(string $sectionName): void
{
$this->files = [
$this->loadedIni,
$this->scanDir.DIRECTORY_SEPARATOR.'section-first.ini',
$this->scanDir.DIRECTORY_SEPARATOR.'section-'.$sectionName.'.ini',
];
$this->setEnvironment();
}
/**
* @return string[]
*/
public function getIniFiles(): array
{
return $this->files;
}
public function hasScannedInis(): bool
{
return count($this->files) > 1;
}
public function getLoadedIni(): string
{
return $this->loadedIni;
}
public function getScanDir(): string
{
return $this->scanDir;
}
private function setEnvironment(): void
{
// Set ORIGINAL_INIS. Values must be path-separated
$this->setEnv(CoreMock::ORIGINAL_INIS, implode(PATH_SEPARATOR, $this->files));
if ($this->envOptions !== null) {
list($scanDir, $phprc) = $this->envOptions;
$this->setEnv('PHP_INI_SCAN_DIR', $scanDir);
$this->setEnv('PHPRC', $phprc);
}
}
/**
* @param string $name
* @param string|false $value
*/
private function setEnv(string $name, $value): void
{
if (false !== $value) {
putenv($name.'='.$value);
$_SERVER[$name] = $value;
} else {
putenv($name);
unset($_SERVER[$name]);
}
}
}
|