Skip to content

Commit f77cdda

Browse files
committed
[DependencyInjection] fixed ini file values conversion
1 parent ef48f59 commit f77cdda

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

src/Symfony/Component/DependencyInjection/Loader/IniFileLoader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Config\Resource\FileResource;
1515
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
16+
use Symfony\Component\Config\Util\XmlUtils;
1617

1718
/**
1819
* IniFileLoader loads parameters from INI files.
@@ -30,14 +31,14 @@ public function load($resource, $type = null)
3031

3132
$this->container->addResource(new FileResource($path));
3233

33-
$result = parse_ini_file($path, true);
34+
$result = parse_ini_file($path, true, INI_SCANNER_RAW);
3435
if (false === $result || array() === $result) {
3536
throw new InvalidArgumentException(sprintf('The "%s" file is not valid.', $resource));
3637
}
3738

3839
if (isset($result['parameters']) && is_array($result['parameters'])) {
3940
foreach ($result['parameters'] as $key => $value) {
40-
$this->container->setParameter($key, $value);
41+
$this->container->setParameter($key, XmlUtils::phpize($value));
4142
}
4243
}
4344
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[parameters]
2+
true = true
3+
false = false
4+
null = null
5+
12 = 12
6+
-12 = -12
7+
0 = 0
8+
1 = 1
9+
0b0110 = 0b0110
10+
11112222333344445555 = 1111,2222,3333,4444,5555
11+
0777 = 0777
12+
255 = 0xFF
13+
100.0 = 1e2
14+
-120.0 = -1.2E2
15+
-10100.1 = -10100.1
16+
-10,100.1 = -10,100.1

src/Symfony/Component/DependencyInjection/Tests/Loader/IniFileLoaderTest.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,13 @@
1717

1818
class IniFileLoaderTest extends \PHPUnit_Framework_TestCase
1919
{
20-
protected static $fixturesPath;
21-
2220
protected $container;
2321
protected $loader;
2422

25-
public static function setUpBeforeClass()
26-
{
27-
self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
28-
}
29-
3023
protected function setUp()
3124
{
3225
$this->container = new ContainerBuilder();
33-
$this->loader = new IniFileLoader($this->container, new FileLocator(self::$fixturesPath.'/ini'));
26+
$this->loader = new IniFileLoader($this->container, new FileLocator(realpath(__DIR__.'/../Fixtures/').'/ini'));
3427
}
3528

3629
public function testIniFileCanBeLoaded()
@@ -39,6 +32,35 @@ public function testIniFileCanBeLoaded()
3932
$this->assertEquals(array('foo' => 'bar', 'bar' => '%foo%'), $this->container->getParameterBag()->all(), '->load() takes a single file name as its first argument');
4033
}
4134

35+
/**
36+
* @requires PHP 5.3
37+
*/
38+
public function testTypeConversions()
39+
{
40+
$this->loader->load('types.ini');
41+
$expected = array(
42+
'true' => true,
43+
'false' => false,
44+
'null' => null,
45+
'12' => 12,
46+
'-12' => -12.0,
47+
'1' => 1,
48+
'0' => 0,
49+
'0b0110' => 6,
50+
'11112222333344445555' => '1111,2222,3333,4444,5555',
51+
'0777' => 0777,
52+
'255' => 0xFF,
53+
'100.0' => 1e2,
54+
'-120.0' => -1.2E2,
55+
'-10100.1' => -10100.1,
56+
'-10,100.1' => '-10,100.1',
57+
);
58+
$parameters = $this->container->getParameterBag()->all();
59+
foreach ($expected as $key => $value) {
60+
$this->assertSame($value, $parameters[$key], '->load() converts values to PHP types');
61+
}
62+
}
63+
4264
/**
4365
* @expectedException \InvalidArgumentException
4466
* @expectedExceptionMessage The file "foo.ini" does not exist (in:

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public function testLoadImports()
164164
);
165165

166166
$this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
167+
$this->assertTrue($actual['imported_from_ini']);
167168

168169
// Bad import throws no exception due to ignore_errors value.
169170
$loader->load('services4_bad_import.xml');

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function testLoadImports()
115115
$actual = $container->getParameterBag()->all();
116116
$expected = array('foo' => 'bar', 'values' => array(true, false), 'bar' => '%foo%', 'escape' => '@escapeme', 'foo_bar' => new Reference('foo_bar'), 'mixedcase' => array('MixedCaseKey' => 'value'), 'imported_from_ini' => true, 'imported_from_xml' => true);
117117
$this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
118+
$this->assertTrue($actual['imported_from_ini']);
118119

119120
// Bad import throws no exception due to ignore_errors value.
120121
$loader->load('services4_bad_import.yml');

0 commit comments

Comments
 (0)