Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Ini Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

composer require indigophp/ini

First Use Case: Parsing INI Files

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
//     ]
// ]

First Use Case: Rendering Arrays to INI

$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

Implementation Patterns

Parsing Workflow

  1. Read INI files from config paths (e.g., config/database.ini)
  2. Parse with type conversion:
    $data = Ini::parse(file_get_contents('config/app.ini'));
    
  3. Merge with Laravel config:
    config(['database.connections' => $data['database']]);
    

Rendering Workflow

  1. Prepare configuration array:
    $config = [
        'app' => [
            'debug' => env('APP_DEBUG', false),
            'timezone' => 'UTC',
        ]
    ];
    
  2. Render with custom flags (e.g., Ini::RENDER_LONG_ARRAY):
    $iniString = Ini::render($config, Ini::RENDER_LONG_ARRAY);
    
  3. Write to file:
    file_put_contents('config/app.ini', $iniString);
    

Integration with Laravel

  • Service Provider Bootstrapping:
    public function boot()
    {
        $iniData = Ini::parse(file_get_contents(config_path('custom.ini')));
        config($iniData);
    }
    
  • Dynamic Configuration Loading:
    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;
    }
    

Common Use Cases

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

Gotchas and Tips

Pitfalls

  1. Boolean Rendering Quirks:

    • Default renders true/false as 1/0 (PHP-style).
    • Use Ini::RENDER_BOOL_STRING flag for "true"/"false" strings.
    Ini::render(['debug' => true], Ini::RENDER_BOOL_STRING);
    // Output: debug = "true"
    
  2. Unescaped Characters:

    • INI files may contain unescaped = or ; in values.
    • Use Ini::parse($content, Ini::SCANNER_RAW) for strict parsing.
  3. Section Case Sensitivity:

    • Section names ([database]) are case-sensitive by default.
    • Normalize keys if case-insensitive behavior is needed:
    array_change_key_case($parsed, CASE_LOWER);
    
  4. Floating-Point Precision:

    • Values like 15.5 are parsed as float, but rendering may lose precision.
    • Use Ini::RENDER_FLOAT_AS_STRING to preserve precision:
    Ini::render(['timeout' => 15.5], Ini::RENDER_FLOAT_AS_STRING);
    // Output: timeout = "15.5"
    

Debugging Tips

  • Validate INI Syntax:
    try {
        $data = Ini::parse($iniString);
    } catch (\Indigo\Ini\Exception\ParseException $e) {
        // Handle syntax errors
    }
    
  • Inspect Rendered Output:
    $flags = Ini::RENDER_LONG_ARRAY | Ini::RENDER_BOOL_STRING;
    $output = Ini::render($config, $flags);
    
  • Check for Deprecated Flags:
    • Always reference the latest flags (e.g., Ini::RENDER_* constants) from the package.

Extension Points

  1. 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);
        }
    }
    
  2. Post-Parse Processing: Use array_walk_recursive to transform parsed values:

    array_walk_recursive($parsed, function(&$value) {
        $value = strtolower($value); // Example: normalize strings
    });
    
  3. 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
        }
    };
    

Laravel-Specific Tips

  • Cache Parsed INI Files:
    $cacheKey = 'ini_config_'.md5($filePath);
    $data = Cache::remember($cacheKey, now()->addHours(1), function() use ($filePath) {
        return Ini::parse(file_get_contents($filePath));
    });
    
  • Environment-Aware Parsing:
    $env = env('APP_ENV');
    $iniPath = config_path("app_{$env}.ini");
    if (file_exists($iniPath)) {
        config(Ini::parse(file_get_contents($iniPath)));
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity