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

Commit 93b69ea

Browse files
committed
Merge branch 'feature/58' into develop
Close #58
2 parents e0df00b + c654be5 commit 93b69ea

File tree

6 files changed

+185
-15
lines changed

6 files changed

+185
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ All notable changes to this project will be documented in this file, in reverse
66

77
### Added
88

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

1113
### Changed
1214

docs/book/reader.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ function. Please review this documentation to be aware of its specific behaviors
4242
> $reader->setNestSeparator('-');
4343
> ```
4444
45+
> ### Process Sections
46+
>
47+
> 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.
48+
>
49+
> To merge sections, set the parameter via `setProcessSections()` to `false` as follows.
50+
>
51+
> ```php
52+
> $reader = new Zend\Config\Reader\Ini();
53+
> $reader->setProcessSections(false);
54+
> ```
55+
4556
The following example illustrates basic usage of `Zend\Config\Reader\Ini` for
4657
loading configuration data from an INI file. In this example, configuration data
4758
for both a production system and for a staging system exists.

src/Reader/Ini.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-config for the canonical source repository
4-
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
4+
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license https://github.com/zendframework/zend-config/blob/master/LICENSE.md New BSD License
66
*/
77

@@ -28,6 +28,14 @@ class Ini implements ReaderInterface
2828
*/
2929
protected $directory;
3030

31+
/**
32+
* Flag which determines whether sections are processed or not.
33+
*
34+
* @see https://www.php.net/parse_ini_file
35+
* @var bool
36+
*/
37+
protected $processSections = true;
38+
3139
/**
3240
* Set nest separator.
3341
*
@@ -50,6 +58,34 @@ public function getNestSeparator()
5058
return $this->nestSeparator;
5159
}
5260

61+
/**
62+
* Marks whether sections should be processed.
63+
* When sections are not processed,section names are stripped and section
64+
* values are merged
65+
*
66+
* @see https://www.php.net/parse_ini_file
67+
* @param bool $processSections
68+
* @return $this
69+
*/
70+
public function setProcessSections($processSections)
71+
{
72+
$this->processSections = (bool) $processSections;
73+
return $this;
74+
}
75+
76+
/**
77+
* Get if sections should be processed
78+
* When sections are not processed,section names are stripped and section
79+
* values are merged
80+
*
81+
* @see https://www.php.net/parse_ini_file
82+
* @return bool
83+
*/
84+
public function getProcessSections()
85+
{
86+
return $this->processSections;
87+
}
88+
5389
/**
5490
* fromFile(): defined by Reader interface.
5591
*
@@ -78,7 +114,7 @@ function ($error, $message = '') use ($filename) {
78114
},
79115
E_WARNING
80116
);
81-
$ini = parse_ini_file($filename, true);
117+
$ini = parse_ini_file($filename, $this->getProcessSections());
82118
restore_error_handler();
83119

84120
return $this->process($ini);
@@ -107,7 +143,7 @@ function ($error, $message = '') {
107143
},
108144
E_WARNING
109145
);
110-
$ini = parse_ini_string($string, true);
146+
$ini = parse_ini_string($string, $this->getProcessSections());
111147
restore_error_handler();
112148

113149
return $this->process($ini);

test/Reader/IniTest.php

Lines changed: 118 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-config for the canonical source repository
4-
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
4+
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license https://github.com/zendframework/zend-config/blob/master/LICENSE.md New BSD License
66
*/
77

@@ -48,9 +48,9 @@ public function testFromString()
4848
ECS;
4949

5050
$arrayIni = $this->reader->fromString($ini);
51-
$this->assertEquals($arrayIni['test'], 'foo');
52-
$this->assertEquals($arrayIni['bar'][0], 'baz');
53-
$this->assertEquals($arrayIni['bar'][1], 'foo');
51+
$this->assertEquals('foo', $arrayIni['test']);
52+
$this->assertEquals('baz', $arrayIni['bar'][0]);
53+
$this->assertEquals('foo', $arrayIni['bar'][1]);
5454
}
5555

5656
public function testInvalidString()
@@ -74,9 +74,9 @@ public function testFromStringWithSection()
7474
ECS;
7575

7676
$arrayIni = $this->reader->fromString($ini);
77-
$this->assertEquals($arrayIni['all']['test'], 'foo');
78-
$this->assertEquals($arrayIni['all']['bar'][0], 'baz');
79-
$this->assertEquals($arrayIni['all']['bar'][1], 'foo');
77+
$this->assertEquals('foo', $arrayIni['all']['test']);
78+
$this->assertEquals('baz', $arrayIni['all']['bar'][0]);
79+
$this->assertEquals('foo', $arrayIni['all']['bar'][1]);
8080
}
8181

8282
public function testFromStringNested()
@@ -90,9 +90,116 @@ public function testFromStringNested()
9090
ECS;
9191

9292
$arrayIni = $this->reader->fromString($ini);
93-
$this->assertEquals($arrayIni['bla']['foo']['bar'], 'foobar');
94-
$this->assertEquals($arrayIni['bla']['foobar'][0], 'foobarArray');
95-
$this->assertEquals($arrayIni['bla']['foo']['baz'][0], 'foobaz1');
96-
$this->assertEquals($arrayIni['bla']['foo']['baz'][1], 'foobaz2');
93+
$this->assertEquals('foobar', $arrayIni['bla']['foo']['bar']);
94+
$this->assertEquals('foobarArray', $arrayIni['bla']['foobar'][0]);
95+
$this->assertEquals('foobaz1', $arrayIni['bla']['foo']['baz'][0]);
96+
$this->assertEquals('foobaz2', $arrayIni['bla']['foo']['baz'][1]);
97+
}
98+
99+
public function testFromFileParseSections()
100+
{
101+
$arrayIni = $this->reader->fromFile($this->getTestAssetPath('sections'));
102+
103+
$this->assertEquals('production', $arrayIni['production']['env']);
104+
$this->assertEquals('foo', $arrayIni['production']['production_key']);
105+
$this->assertEquals('staging', $arrayIni['staging : production']['env']);
106+
$this->assertEquals('bar', $arrayIni['staging : production']['staging_key']);
107+
}
108+
109+
public function testFromFileDontParseSections()
110+
{
111+
$reader = $this->reader;
112+
$reader->setProcessSections(false);
113+
114+
$arrayIni = $reader->fromFile($this->getTestAssetPath('sections'));
115+
116+
$this->assertEquals('staging', $arrayIni['env']);
117+
$this->assertEquals('foo', $arrayIni['production_key']);
118+
$this->assertEquals('bar', $arrayIni['staging_key']);
119+
}
120+
121+
public function testFromFileIgnoresNestingInSectionNamesWhenSectionsNotProcessed()
122+
{
123+
$reader = $this->reader;
124+
$reader->setProcessSections(false);
125+
126+
$arrayIni = $reader->fromFile($this->getTestAssetPath('nested-sections'));
127+
128+
$this->assertArrayNotHasKey('environments.production', $arrayIni);
129+
$this->assertArrayNotHasKey('environments.staging', $arrayIni);
130+
$this->assertArrayNotHasKey('environments', $arrayIni);
131+
$this->assertArrayNotHasKey('production', $arrayIni);
132+
$this->assertArrayNotHasKey('staging', $arrayIni);
133+
$this->assertEquals('staging', $arrayIni['env']);
134+
$this->assertEquals('foo', $arrayIni['production_key']);
135+
$this->assertEquals('bar', $arrayIni['staging_key']);
136+
}
137+
138+
public function testFromStringParseSections()
139+
{
140+
$ini = <<<ECS
141+
[production]
142+
env='production'
143+
production_key='foo'
144+
145+
[staging : production]
146+
env='staging'
147+
staging_key='bar'
148+
149+
ECS;
150+
$arrayIni = $this->reader->fromString($ini);
151+
152+
$this->assertEquals('production', $arrayIni['production']['env']);
153+
$this->assertEquals('foo', $arrayIni['production']['production_key']);
154+
$this->assertEquals('staging', $arrayIni['staging : production']['env']);
155+
$this->assertEquals('bar', $arrayIni['staging : production']['staging_key']);
156+
}
157+
158+
public function testFromStringDontParseSections()
159+
{
160+
$ini = <<<ECS
161+
[production]
162+
env='production'
163+
production_key='foo'
164+
165+
[staging : production]
166+
env='staging'
167+
staging_key='bar'
168+
169+
ECS;
170+
$reader = $this->reader;
171+
$reader->setProcessSections(false);
172+
173+
$arrayIni = $reader->fromString($ini);
174+
175+
$this->assertEquals('staging', $arrayIni['env']);
176+
$this->assertEquals('foo', $arrayIni['production_key']);
177+
$this->assertEquals('bar', $arrayIni['staging_key']);
178+
}
179+
180+
public function testFromStringIgnoresNestingInSectionNamesWhenSectionsNotProcessed()
181+
{
182+
$ini = <<<ECS
183+
[environments.production]
184+
env='production'
185+
production_key='foo'
186+
187+
[environments.staging]
188+
env='staging'
189+
staging_key='bar'
190+
ECS;
191+
$reader = $this->reader;
192+
$reader->setProcessSections(false);
193+
194+
$arrayIni = $reader->fromString($ini);
195+
196+
$this->assertArrayNotHasKey('environments.production', $arrayIni);
197+
$this->assertArrayNotHasKey('environments.staging', $arrayIni);
198+
$this->assertArrayNotHasKey('environments', $arrayIni);
199+
$this->assertArrayNotHasKey('production', $arrayIni);
200+
$this->assertArrayNotHasKey('staging', $arrayIni);
201+
$this->assertEquals('staging', $arrayIni['env']);
202+
$this->assertEquals('foo', $arrayIni['production_key']);
203+
$this->assertEquals('bar', $arrayIni['staging_key']);
97204
}
98205
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[environments.production]
2+
env='production'
3+
production_key='foo'
4+
5+
[environments.staging]
6+
env='staging'
7+
staging_key='bar'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[production]
2+
env='production'
3+
production_key='foo'
4+
5+
[staging : production]
6+
env='staging'
7+
staging_key='bar'

0 commit comments

Comments
 (0)