Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge c654be5 into e0df00b
Browse files Browse the repository at this point in the history
  • Loading branch information
arueckauer committed May 20, 2019
2 parents e0df00b + c654be5 commit e187767
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 15 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -6,7 +6,9 @@ All notable changes to this project will be documented in this file, in reverse

### Added

- Nothing.
- [#58](https://github.com/zendframework/zend-config/pull/58) adds
`$processSections` to INI reader, allowing control over whether sections
should be parsed or not

### Changed

Expand Down
11 changes: 11 additions & 0 deletions docs/book/reader.md
Expand Up @@ -42,6 +42,17 @@ function. Please review this documentation to be aware of its specific behaviors
> $reader->setNestSeparator('-');
> ```
> ### Process Sections
>
> By default, the INI reader executes `parse_ini_file()` with the optional parameter `$process_sections` being `true`. The result is a multidimensional array, with the section names and settings included.
>
> To merge sections, set the parameter via `setProcessSections()` to `false` as follows.
>
> ```php
> $reader = new Zend\Config\Reader\Ini();
> $reader->setProcessSections(false);
> ```
The following example illustrates basic usage of `Zend\Config\Reader\Ini` for
loading configuration data from an INI file. In this example, configuration data
for both a production system and for a staging system exists.
Expand Down
42 changes: 39 additions & 3 deletions src/Reader/Ini.php
@@ -1,7 +1,7 @@
<?php
/**
* @see https://github.com/zendframework/zend-config for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-config/blob/master/LICENSE.md New BSD License
*/

Expand All @@ -28,6 +28,14 @@ class Ini implements ReaderInterface
*/
protected $directory;

/**
* Flag which determines whether sections are processed or not.
*
* @see https://www.php.net/parse_ini_file
* @var bool
*/
protected $processSections = true;

/**
* Set nest separator.
*
Expand All @@ -50,6 +58,34 @@ public function getNestSeparator()
return $this->nestSeparator;
}

/**
* Marks whether sections should be processed.
* When sections are not processed,section names are stripped and section
* values are merged
*
* @see https://www.php.net/parse_ini_file
* @param bool $processSections
* @return $this
*/
public function setProcessSections($processSections)
{
$this->processSections = (bool) $processSections;
return $this;
}

/**
* Get if sections should be processed
* When sections are not processed,section names are stripped and section
* values are merged
*
* @see https://www.php.net/parse_ini_file
* @return bool
*/
public function getProcessSections()
{
return $this->processSections;
}

/**
* fromFile(): defined by Reader interface.
*
Expand Down Expand Up @@ -78,7 +114,7 @@ function ($error, $message = '') use ($filename) {
},
E_WARNING
);
$ini = parse_ini_file($filename, true);
$ini = parse_ini_file($filename, $this->getProcessSections());
restore_error_handler();

return $this->process($ini);
Expand Down Expand Up @@ -107,7 +143,7 @@ function ($error, $message = '') {
},
E_WARNING
);
$ini = parse_ini_string($string, true);
$ini = parse_ini_string($string, $this->getProcessSections());
restore_error_handler();

return $this->process($ini);
Expand Down
129 changes: 118 additions & 11 deletions test/Reader/IniTest.php
@@ -1,7 +1,7 @@
<?php
/**
* @see https://github.com/zendframework/zend-config for the canonical source repository
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-config/blob/master/LICENSE.md New BSD License
*/

Expand Down Expand Up @@ -48,9 +48,9 @@ public function testFromString()
ECS;

$arrayIni = $this->reader->fromString($ini);
$this->assertEquals($arrayIni['test'], 'foo');
$this->assertEquals($arrayIni['bar'][0], 'baz');
$this->assertEquals($arrayIni['bar'][1], 'foo');
$this->assertEquals('foo', $arrayIni['test']);
$this->assertEquals('baz', $arrayIni['bar'][0]);
$this->assertEquals('foo', $arrayIni['bar'][1]);
}

public function testInvalidString()
Expand All @@ -74,9 +74,9 @@ public function testFromStringWithSection()
ECS;

$arrayIni = $this->reader->fromString($ini);
$this->assertEquals($arrayIni['all']['test'], 'foo');
$this->assertEquals($arrayIni['all']['bar'][0], 'baz');
$this->assertEquals($arrayIni['all']['bar'][1], 'foo');
$this->assertEquals('foo', $arrayIni['all']['test']);
$this->assertEquals('baz', $arrayIni['all']['bar'][0]);
$this->assertEquals('foo', $arrayIni['all']['bar'][1]);
}

public function testFromStringNested()
Expand All @@ -90,9 +90,116 @@ public function testFromStringNested()
ECS;

$arrayIni = $this->reader->fromString($ini);
$this->assertEquals($arrayIni['bla']['foo']['bar'], 'foobar');
$this->assertEquals($arrayIni['bla']['foobar'][0], 'foobarArray');
$this->assertEquals($arrayIni['bla']['foo']['baz'][0], 'foobaz1');
$this->assertEquals($arrayIni['bla']['foo']['baz'][1], 'foobaz2');
$this->assertEquals('foobar', $arrayIni['bla']['foo']['bar']);
$this->assertEquals('foobarArray', $arrayIni['bla']['foobar'][0]);
$this->assertEquals('foobaz1', $arrayIni['bla']['foo']['baz'][0]);
$this->assertEquals('foobaz2', $arrayIni['bla']['foo']['baz'][1]);
}

public function testFromFileParseSections()
{
$arrayIni = $this->reader->fromFile($this->getTestAssetPath('sections'));

$this->assertEquals('production', $arrayIni['production']['env']);
$this->assertEquals('foo', $arrayIni['production']['production_key']);
$this->assertEquals('staging', $arrayIni['staging : production']['env']);
$this->assertEquals('bar', $arrayIni['staging : production']['staging_key']);
}

public function testFromFileDontParseSections()
{
$reader = $this->reader;
$reader->setProcessSections(false);

$arrayIni = $reader->fromFile($this->getTestAssetPath('sections'));

$this->assertEquals('staging', $arrayIni['env']);
$this->assertEquals('foo', $arrayIni['production_key']);
$this->assertEquals('bar', $arrayIni['staging_key']);
}

public function testFromFileIgnoresNestingInSectionNamesWhenSectionsNotProcessed()
{
$reader = $this->reader;
$reader->setProcessSections(false);

$arrayIni = $reader->fromFile($this->getTestAssetPath('nested-sections'));

$this->assertArrayNotHasKey('environments.production', $arrayIni);
$this->assertArrayNotHasKey('environments.staging', $arrayIni);
$this->assertArrayNotHasKey('environments', $arrayIni);
$this->assertArrayNotHasKey('production', $arrayIni);
$this->assertArrayNotHasKey('staging', $arrayIni);
$this->assertEquals('staging', $arrayIni['env']);
$this->assertEquals('foo', $arrayIni['production_key']);
$this->assertEquals('bar', $arrayIni['staging_key']);
}

public function testFromStringParseSections()
{
$ini = <<<ECS
[production]
env='production'
production_key='foo'
[staging : production]
env='staging'
staging_key='bar'
ECS;
$arrayIni = $this->reader->fromString($ini);

$this->assertEquals('production', $arrayIni['production']['env']);
$this->assertEquals('foo', $arrayIni['production']['production_key']);
$this->assertEquals('staging', $arrayIni['staging : production']['env']);
$this->assertEquals('bar', $arrayIni['staging : production']['staging_key']);
}

public function testFromStringDontParseSections()
{
$ini = <<<ECS
[production]
env='production'
production_key='foo'
[staging : production]
env='staging'
staging_key='bar'
ECS;
$reader = $this->reader;
$reader->setProcessSections(false);

$arrayIni = $reader->fromString($ini);

$this->assertEquals('staging', $arrayIni['env']);
$this->assertEquals('foo', $arrayIni['production_key']);
$this->assertEquals('bar', $arrayIni['staging_key']);
}

public function testFromStringIgnoresNestingInSectionNamesWhenSectionsNotProcessed()
{
$ini = <<<ECS
[environments.production]
env='production'
production_key='foo'
[environments.staging]
env='staging'
staging_key='bar'
ECS;
$reader = $this->reader;
$reader->setProcessSections(false);

$arrayIni = $reader->fromString($ini);

$this->assertArrayNotHasKey('environments.production', $arrayIni);
$this->assertArrayNotHasKey('environments.staging', $arrayIni);
$this->assertArrayNotHasKey('environments', $arrayIni);
$this->assertArrayNotHasKey('production', $arrayIni);
$this->assertArrayNotHasKey('staging', $arrayIni);
$this->assertEquals('staging', $arrayIni['env']);
$this->assertEquals('foo', $arrayIni['production_key']);
$this->assertEquals('bar', $arrayIni['staging_key']);
}
}
7 changes: 7 additions & 0 deletions test/Reader/TestAssets/Ini/nested-sections.ini
@@ -0,0 +1,7 @@
[environments.production]
env='production'
production_key='foo'

[environments.staging]
env='staging'
staging_key='bar'
7 changes: 7 additions & 0 deletions test/Reader/TestAssets/Ini/sections.ini
@@ -0,0 +1,7 @@
[production]
env='production'
production_key='foo'

[staging : production]
env='staging'
staging_key='bar'

0 comments on commit e187767

Please sign in to comment.