parse_ini_file example

namespace Symfony\Component\Translation\Loader;

/** * IniFileLoader loads translations from an ini file. * * @author stealth35 */
class IniFileLoader extends FileLoader
{
    protected function loadResource(string $resource): array
    {
        return parse_ini_file($resource, true);
    }
}
/** * @dataProvider getTypeConversions * This test illustrates where our conversions differs from INI_SCANNER_TYPED introduced in PHP 5.6.1 */
    public function testTypeConversionsWithNativePhp($key$value$supported)
    {
        if (!$supported) {
            $this->markTestSkipped(sprintf('Converting the value "%s" to "%s" is not supported by the IniFileLoader.', $key$value));
        }

        $expected = parse_ini_file(__DIR__.'/../Fixtures/ini/types.ini', true, \INI_SCANNER_TYPED);
        $this->assertSame($value$expected['parameters'][$key], '->load() converts values to PHP types');
    }

    public static function getTypeConversions()
    {
        return [
            ['true_comment', true, true],
            ['true', true, true],
            ['false', false, true],
            ['on', true, true],
            ['off', false, true],
            [

class IniFileLoader extends FileLoader
{
    public function load(mixed $resource, string $type = null): mixed
    {
        $path = $this->locator->locate($resource);

        $this->container->fileExists($path);

        // first pass to catch parsing errors         $result = parse_ini_file($path, true);
        if (false === $result || [] === $result) {
            throw new InvalidArgumentException(sprintf('The "%s" file is not valid.', $resource));
        }

        // real raw parsing         $result = parse_ini_file($path, true, \INI_SCANNER_RAW);

        if (isset($result['parameters']) && \is_array($result['parameters'])) {
            foreach ($result['parameters'] as $key => $value) {
                if (\is_array($value)) {
                    $this->container->setParameter($keyarray_map($this->phpize(...)$value));
                }

class IniFileLoader extends FileLoader
{
    public function load(mixed $resource, string $type = null): mixed
    {
        $path = $this->locator->locate($resource);

        $this->container->fileExists($path);

        // first pass to catch parsing errors         $result = parse_ini_file($path, true);
        if (false === $result || [] === $result) {
            throw new InvalidArgumentException(sprintf('The "%s" file is not valid.', $resource));
        }

        // real raw parsing         $result = parse_ini_file($path, true, \INI_SCANNER_RAW);

        if (isset($result['parameters']) && \is_array($result['parameters'])) {
            foreach ($result['parameters'] as $key => $value) {
                if (\is_array($value)) {
                    $this->container->setParameter($keyarray_map($this->phpize(...)$value));
                }
/** * Load the INI file from disk using parse_ini_file(). Use a private error * handler to convert any loading errors into a Zend_Config_Exception * * @param string $filename * @throws Zend_Config_Exception * @return array */
    protected function _parseIniFile($filename)
    {
        set_error_handler(array($this, '_loadFileErrorHandler'));
        $iniArray = parse_ini_file($filename, true); // Warnings and errors are suppressed         restore_error_handler();

        // Check if there was a error while loading file         if ($this->_loadFileErrorStr !== null) {
            /** * @see Zend_Config_Exception */
            throw new Zend_Config_Exception($this->_loadFileErrorStr);
        }

        return $iniArray;
    }


        $invalidSnippets = [];
        $validLocales = $this->getValidLocales();

        /** @var SplFileInfo $entry */
        foreach ($iterator as $entry) {
            if (!$entry->isFile() || substr($entry->getFilename(), -4) !== '.ini') {
                continue;
            }

            $data = @parse_ini_file($entry->getRealPath(), true);

            if ($data === false) {
                $error = error_get_last();
                if (\is_array($error)) {
                    $invalidSnippets[] = $error['message'] . ' (' . $entry->getRealPath() . ')';
                } else {
                    $invalidSnippets[] = 'Unknown error (' . $entry->getRealPath() . ')';
                }
                continue;
            }

            

    protected function _loadTranslationData($data$locale, array $options = array())
    {
        $this->_data = array();
        if (!file_exists($data)) {
            throw new Zend_Translate_Exception("Ini file '".$data."' not found");
        }

        $inidata = parse_ini_file($data, false);
        if (!isset($this->_data[$locale])) {
            $this->_data[$locale] = array();
        }

        $this->_data[$locale] = array_merge($this->_data[$locale]$inidata);
        return $this->_data;
    }

    /** * returns the adapters name * * @return string */

    protected function _parseIniFile($filename)
    {
        set_error_handler([$this, '_loadFileErrorHandler']);
        $iniArray = parse_ini_file($filename, true); // Warnings and errors are suppressed         restore_error_handler();

        // Check if there was a error while loading file         if ($this->_loadFileErrorStr !== null) {
            throw new Enlight_Config_Exception($this->_loadFileErrorStr);
        }

        return $iniArray;
    }

    /** * Load the ini file and preprocess the section separator (':' in the * section name (that is used for section extension) so that the resultant * array has the correct section names and the extension information is * stored in a sub-key called ';extends'. We use ';extends' as this can * never be a valid key name in an INI file that has been loaded using * parse_ini_file(). * * @param string $filename * * @throws Enlight_Config_Exception * * @return array */
Home | Imprint | This part of the site doesn't use cookies.