indigophp/ini
INI Tools for PHP: parse and render INI with better defaults. Throws exceptions, converts special values (ints/bools like PHP 5.6.1), renders arrays back to INI, and lets you control output via renderer flags.
composer require indigophp/ini
use Indigo\Ini\Ini;
$iniContent = <<<INI
[database]
host = localhost
port = 3306
enabled = true
timeout = 15.5
INI;
$parsed = Ini::parse($iniContent);
// Returns:
// [
// 'database' => [
// 'host' => 'localhost',
// 'port' => 3306, // auto-converted to int
// 'enabled' => true, // auto-converted to bool
// 'timeout' => 15.5 // auto-converted to float
// ]
// ]
$config = [
'database' => [
'host' => 'localhost',
'port' => 3306,
'enabled' => true,
'timeout' => 15.5,
]
];
$iniString = Ini::render($config);
// Returns:
// [database]
// host = localhost
// port = 3306
// enabled = true
// timeout = 15.5
config/database.ini)$data = Ini::parse(file_get_contents('config/app.ini'));
config(['database.connections' => $data['database']]);
$config = [
'app' => [
'debug' => env('APP_DEBUG', false),
'timezone' => 'UTC',
]
];
Ini::RENDER_LONG_ARRAY):
$iniString = Ini::render($config, Ini::RENDER_LONG_ARRAY);
file_put_contents('config/app.ini', $iniString);
public function boot()
{
$iniData = Ini::parse(file_get_contents(config_path('custom.ini')));
config($iniData);
}
public function get($key, $default = null)
{
$iniPath = storage_path('config/'.$key.'.ini');
if (file_exists($iniPath)) {
return Ini::parse(file_get_contents($iniPath))[$key] ?? $default;
}
return $default;
}
| Use Case | Implementation Pattern |
|---|---|
Parsing .ini config files |
Ini::parse(file_get_contents('path.ini')) |
| Rendering config to INI | Ini::render($array, flags) |
| Type-safe config loading | Parse → Merge with config() helper |
| Dynamic INI generation | Build array → Render → Write to file |
Boolean Rendering Quirks:
true/false as 1/0 (PHP-style).Ini::RENDER_BOOL_STRING flag for "true"/"false" strings.Ini::render(['debug' => true], Ini::RENDER_BOOL_STRING);
// Output: debug = "true"
Unescaped Characters:
= or ; in values.Ini::parse($content, Ini::SCANNER_RAW) for strict parsing.Section Case Sensitivity:
[database]) are case-sensitive by default.array_change_key_case($parsed, CASE_LOWER);
Floating-Point Precision:
15.5 are parsed as float, but rendering may lose precision.Ini::RENDER_FLOAT_AS_STRING to preserve precision:Ini::render(['timeout' => 15.5], Ini::RENDER_FLOAT_AS_STRING);
// Output: timeout = "15.5"
try {
$data = Ini::parse($iniString);
} catch (\Indigo\Ini\Exception\ParseException $e) {
// Handle syntax errors
}
$flags = Ini::RENDER_LONG_ARRAY | Ini::RENDER_BOOL_STRING;
$output = Ini::render($config, $flags);
Ini::RENDER_* constants) from the package.Custom Type Conversion:
Override default parsing by extending the Indigo\Ini\Parser class:
class CustomParser extends \Indigo\Ini\Parser {
protected function convertValue($value) {
// Custom logic (e.g., parse dates)
return parent::convertValue($value);
}
}
Post-Parse Processing:
Use array_walk_recursive to transform parsed values:
array_walk_recursive($parsed, function(&$value) {
$value = strtolower($value); // Example: normalize strings
});
Renderer Decorators: Wrap the renderer for custom formatting:
$renderer = new \Indigo\Ini\Renderer();
$customRenderer = new class($renderer) {
private $renderer;
public function __construct($renderer) { $this->renderer = $renderer; }
public function render($array, $flags = 0) {
$output = $this->renderer->render($array, $flags);
return str_replace(["\r\n", "\n"], "\r\n", $output); // Force CRLF
}
};
$cacheKey = 'ini_config_'.md5($filePath);
$data = Cache::remember($cacheKey, now()->addHours(1), function() use ($filePath) {
return Ini::parse(file_get_contents($filePath));
});
$env = env('APP_ENV');
$iniPath = config_path("app_{$env}.ini");
if (file_exists($iniPath)) {
config(Ini::parse(file_get_contents($iniPath)));
}
How can I help you explore Laravel packages today?