Skip to content

Commit 8972465

Browse files
committed
fixed merging bug: merging with empty array caused error
1 parent 82b727b commit 8972465

File tree

6 files changed

+115
-30
lines changed

6 files changed

+115
-30
lines changed

src/Adapters/XmlAdapter.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,11 @@ private function parseStatement($child)
212212
}
213213
$arrayAttr = $argsElement[0]->getAttribute(self::ATTR_ARRAY);
214214
if (is_null($arrayAttr)) {
215-
$argsElement[0]->addAttribute(self::ATTR_ARRAY, self::ATTR_ARRAY_NUMERIC);
215+
if($argsElement[0]->count()) {
216+
$argsElement[0]->addAttribute(self::ATTR_ARRAY, self::ATTR_ARRAY_NUMERIC);
217+
} else {
218+
$argsElement[0]->addAttribute(self::ATTR_ARRAY, self::ATTR_ARRAY_STRING);
219+
}
216220
}
217221

218222
$attributes = $argsElement[0]->parseChildren();
@@ -229,7 +233,7 @@ private function parseStatement($child)
229233
*/
230234
private function parseChildren()
231235
{
232-
$arrayType = $this->getAttribute(self::ATTR_ARRAY, self::ATTR_ARRAY_ASSOCIATIVE);
236+
$arrayType = $this->getAttribute(self::ATTR_ARRAY);
233237
$space = (string) $this->getAttribute(self::ATTR_SPACE);
234238
if ($space !== self::ATTR_SPACE_PRESERVE and ! empty($space)) {
235239
throw new Nette\InvalidStateException("Attribute " . self::ATTR_SPACE . " has an unknown value '$space'");
@@ -241,7 +245,11 @@ private function parseChildren()
241245
return $res;
242246
}
243247

244-
if (!$this->count()) {
248+
if ($arrayType === NULL && $this->count()) {
249+
$arrayType = self::ATTR_ARRAY_ASSOCIATIVE;
250+
}
251+
252+
if ($arrayType === NULL) {
245253
$res = $this->getValue();
246254
$this->trim($res, $space);
247255
return $res;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* Test: Sallyx\Nette\DI\Config\Adapters\XmlAdapter
5+
*/
6+
7+
use Nette\DI\Config;
8+
use Nette\DI\Statement;
9+
use Tester\Assert;
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
$config = new Config\Loader;
14+
$config->addAdapter('xml','Sallyx\Nette\DI\Config\Adapters\XmlAdapter');
15+
$data = $config->load('files/xmlAdapter.merge.xml', 'production');
16+
Assert::same(array(
17+
'webname' => 'the example',
18+
'extensions' => array(
19+
'redis' => 'Kdyby\\Redis\\DI\\RedisExtension',
20+
'streamWrappers' => 'Sallyx\\Bridges\\StreamWrappers\\Nette\\DI\\StreamWrappersExtension',
21+
),
22+
'redis' => array(
23+
'journal' => TRUE,
24+
'storage' => TRUE,
25+
'session' => array('native' => FALSE, 'debugger' => TRUE),
26+
),
27+
), $data);
28+
29+
$data = $config->load('files/xmlAdapter.merge2.xml', 'development');
30+
Assert::same(array(
31+
'webname' => 'the example',
32+
'extensions' => array(
33+
),
34+
'redis' => array(
35+
),
36+
), $data);

tests/Adapters/XmlAdapter.phpt

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,3 @@ Assert::match(<<<EOD
7474
<config xmlns:nc="http://www.sallyx.org/xmlns/nette/config/1.0" xmlns="http://www.sallyx.org/xmlns/nette/config/1.0"><production><webname>the example</webname><database><adapter>pdo_mysql</adapter><params><host>db.example.com</host><username>dbuser</username><password>secret </password><dbname>dbname</dbname></params></database></production><development extends="production"><database><params><host>dev.example.com</host><username>devuser</username><password>devsecret</password></params></database><timeout number="10"></timeout><display_errors bool="1"></display_errors><html_errors bool="0"></html_errors><items array="numeric"><item number="10"></item><item number="20"></item></items><php><zlib.output_compression bool="1"></zlib.output_compression><date.timezone>Europe/Prague</date.timezone></php></development><nothing></nothing></config>
7575
EOD
7676
, $actual);
77-
78-
$data = $config->load('files/xmlAdapter.entity.xml');
79-
Assert::equal(array(
80-
new Statement('ent', array(1)),
81-
new Statement(array(
82-
new Statement('ent', array(2)),
83-
'inner',
84-
),
85-
array('3', '4')
86-
),
87-
new Statement(array(
88-
new Statement('ent', array('3')),
89-
'inner',
90-
),
91-
array('5','6')
92-
),
93-
), $data);
94-
95-
$data = $config->load('files/xmlAdapter.entity.xml');
96-
$config->save($data, TEMP_FILE);
97-
$actual = file_get_contents(TEMP_FILE);
98-
$actual = preg_replace('/\<([^\s\/>]+)(\s*[^\/>]*)\/\s*\>/i', '<$1$2></$1>', $actual);
99-
Assert::match(<<<EOD
100-
<?xml version="1.0"?>
101-
<config xmlns:nc="http://www.sallyx.org/xmlns/nette/config/1.0" xmlns="http://www.sallyx.org/xmlns/nette/config/1.0" array="numeric"><item statement="statement"><s><ent>ent</ent><args array="numeric"><item number="1"></item></args></s></item><item statement="statement"><s><ent>ent</ent><args array="numeric"><item number="2"></item></args></s><s><ent>inner</ent><args array="numeric"><item>3</item><item>4</item></args></s></item><item statement="statement"><s><ent>ent</ent><args array="numeric"><item>3</item></args></s><s><ent>inner</ent><args array="numeric"><item>5</item><item>6</item></args></s></item></config>
102-
EOD
103-
, $actual);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* Test: Sallyx\Nette\DI\Config\Adapters\XmlAdapter
5+
*/
6+
7+
use Nette\DI\Config;
8+
use Nette\DI\Statement;
9+
use Tester\Assert;
10+
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
define('TEMP_FILE', TEMP_DIR . '/cfg.xml');
15+
16+
17+
$config = new Config\Loader;
18+
$config->addAdapter('xml','Sallyx\Nette\DI\Config\Adapters\XmlAdapter');
19+
20+
$data = $config->load('files/xmlAdapter.entity.xml');
21+
Assert::equal(array(
22+
new Statement('ent', array(1)),
23+
new Statement(array(
24+
new Statement('ent', array(2)),
25+
'inner',
26+
),
27+
array('3', '4')
28+
),
29+
new Statement(array(
30+
new Statement('ent', array('3')),
31+
'inner',
32+
),
33+
array('5','6')
34+
),
35+
), $data);
36+
37+
$data = $config->load('files/xmlAdapter.entity.xml');
38+
$config->save($data, TEMP_FILE);
39+
$actual = file_get_contents(TEMP_FILE);
40+
$actual = preg_replace('/\<([^\s\/>]+)(\s*[^\/>]*)\/\s*\>/i', '<$1$2></$1>', $actual);
41+
Assert::match(<<<EOD
42+
<?xml version="1.0"?>
43+
<config xmlns:nc="http://www.sallyx.org/xmlns/nette/config/1.0" xmlns="http://www.sallyx.org/xmlns/nette/config/1.0" array="numeric"><item statement="statement"><s><ent>ent</ent><args array="numeric"><item number="1"></item></args></s></item><item statement="statement"><s><ent>ent</ent><args array="numeric"><item number="2"></item></args></s><s><ent>inner</ent><args array="numeric"><item>3</item><item>4</item></args></s></item><item statement="statement"><s><ent>ent</ent><args array="numeric"><item>3</item></args></s><s><ent>inner</ent><args array="numeric"><item>5</item><item>6</item></args></s></item></config>
44+
EOD
45+
, $actual);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<config xmlns:nc="http://www.sallyx.org/xmlns/nette/config/1.0" xmlns="http://www.sallyx.org/xmlns/nette/config/1.0">
3+
<production>
4+
<webname>the example</webname>
5+
<extensions>
6+
<redis>Kdyby\Redis\DI\RedisExtension</redis>
7+
<streamWrappers>Sallyx\Bridges\StreamWrappers\Nette\DI\StreamWrappersExtension</streamWrappers>
8+
</extensions>
9+
<redis>
10+
<journal bool="true" />
11+
<storage bool="true" />
12+
<session><native bool="false" /><debugger bool="true" /></session>
13+
</redis>
14+
</production>
15+
</config>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<config xmlns:nc="http://www.sallyx.org/xmlns/nette/config/1.0" xmlns="http://www.sallyx.org/xmlns/nette/config/1.0">
3+
<development>
4+
<webname>the example</webname>
5+
<extensions array="number" merging="replace"> </extensions>
6+
<redis array="number"> </redis>
7+
</development>
8+
</config>

0 commit comments

Comments
 (0)