Skip to content

Commit

Permalink
Added support for TOML configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelstolt committed Apr 23, 2013
1 parent a4c2c72 commit f81e73a
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 3 deletions.
12 changes: 11 additions & 1 deletion README.md
@@ -1,7 +1,7 @@
# ConfigServiceProvider

A config ServiceProvider for [Silex](http://silex.sensiolabs.org) with support
for php, json and yaml.
for php, json, yaml, and toml.

## Usage

Expand Down Expand Up @@ -68,6 +68,16 @@ To use Yaml instead of JSON, just pass a file that ends on `.yml`:

Note, you will have to require the `~2.1` of the `symfony/yaml` package.

### Using TOML

To use [TOML](https://github.com/mojombo/toml) instead of any of the other supported formats,
just pass a file that ends on `.toml`:

$app->register(new Igorw\Silex\ConfigServiceProvider(__DIR__."/../config/services.toml"));

Note, you will have to require the `~0.1` of the `jamesmoss/toml` package and you are using
a bleeding edge configuration format, as the spec of TOML is still subject to change.

### Using plain PHP

If reading the config file on every request becomes a performance problem in
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Expand Up @@ -17,10 +17,12 @@
"silex/silex": "1.0.*@dev"
},
"require-dev": {
"symfony/yaml": "~2.1"
"symfony/yaml": "~2.1",
"jamesmoss/toml": "~0.1"
},
"suggest": {
"symfony/yaml": "~2.1"
"symfony/yaml": "~2.1",
"jamesmoss/toml": "~0.1"
},
"autoload": {
"psr-0": { "Igorw\\Silex": "src" }
Expand Down
14 changes: 14 additions & 0 deletions src/Igorw/Silex/ConfigServiceProvider.php
Expand Up @@ -14,6 +14,7 @@
use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\Yaml\Yaml;
use Toml\Parser;

class ConfigServiceProvider implements ServiceProviderInterface
{
Expand Down Expand Up @@ -62,6 +63,10 @@ public function getFileFormat()
return 'php';
}

if (preg_match('#.toml(.dist)?$#i', $filename)) {
return 'toml';
}

return pathinfo($filename, PATHINFO_EXTENSION);
}

Expand Down Expand Up @@ -154,6 +159,15 @@ private function readConfig()
return $config ?: array();
}

if ('toml' === $format) {
if (!class_exists('Toml\\Parser')) {
throw new \RuntimeException('Unable to read toml as the Toml Parser is not installed.');
}

$config = Parser::fromFile($this->filename);
return $config ?: array();
}

throw new \InvalidArgumentException(
sprintf("The config file '%s' appears has invalid format '%s'.", $this->filename, $format));
}
Expand Down
42 changes: 42 additions & 0 deletions tests/integration/ConfigServiceProviderTest.php
Expand Up @@ -75,6 +75,35 @@ public function testInFileReplacements($filename)
$this->assertSame('http://example.com', $app['%url%']);
$this->assertSame('http://example.com/images', $app['url.images']);
}
/**
* Currently not tested via testMergeConfigs as TOML seems to have problems
* to create 'db.options' keys
*/
public function testTomlMergeConfigs()
{
$app = new Application();

$filenameBase = __DIR__."/Fixtures/config_base.toml";
$filenameExtended = __DIR__."/Fixtures/config_extend.toml";

$app->register(new ConfigServiceProvider($filenameBase));
$app->register(new ConfigServiceProvider($filenameExtended));

$this->assertSame('pdo_mysql', $app['db']['driver']);
$this->assertSame('utf8', $app['db']['charset']);
$this->assertSame('127.0.0.1', $app['db']['host']);
$this->assertSame('mydatabase', $app['db']['dbname']);
$this->assertSame('root', $app['db']['user']);
$this->assertSame('', $app['db']['password']);

$this->assertSame('123', $app['myproject']['param1']);
$this->assertSame('456', $app['myproject']['param2']);
$this->assertSame('456', $app['myproject']['param3']);
$this->assertSame(array(4, 5, 6), $app['myproject']['param4']);
$this->assertSame('456', $app['myproject']['param5']);

$this->assertSame(array(1, 2, 3, 4), $app['keys']['set']);
}

/**
* @dataProvider provideMergeFilenames
Expand Down Expand Up @@ -124,12 +153,23 @@ public function invalidYamlShouldThrowException()
$app->register(new ConfigServiceProvider(__DIR__."/Fixtures/broken.yml"));
}

/**
* @test
* @expectedException \Exception
*/
public function invalidTomlShouldThrowException()
{
$app = new Application();
$app->register(new ConfigServiceProvider(__DIR__."/Fixtures/broken.toml"));
}

public function provideFilenames()
{
return array(
array(__DIR__."/Fixtures/config.php"),
array(__DIR__."/Fixtures/config.json"),
array(__DIR__."/Fixtures/config.yml"),
array(__DIR__."/Fixtures/config.toml"),
);
}

Expand All @@ -139,6 +179,7 @@ public function provideReplacementFilenames()
array(__DIR__."/Fixtures/config_replacement.php"),
array(__DIR__."/Fixtures/config_replacement.json"),
array(__DIR__."/Fixtures/config_replacement.yml"),
array(__DIR__."/Fixtures/config_replacement.toml"),
);
}

Expand All @@ -148,6 +189,7 @@ public function provideEmptyFilenames()
array(__DIR__."/Fixtures/config_empty.php"),
array(__DIR__."/Fixtures/config_empty.json"),
array(__DIR__."/Fixtures/config_empty.yml"),
array(__DIR__."/Fixtures/config_empty.toml"),
);
}

Expand Down
1 change: 1 addition & 0 deletions tests/integration/Fixtures/broken.toml
@@ -0,0 +1 @@
broken = [ 1, 2.0 ]
2 changes: 2 additions & 0 deletions tests/integration/Fixtures/config.toml
@@ -0,0 +1,2 @@
debug = true
data = "%data%"
10 changes: 10 additions & 0 deletions tests/integration/Fixtures/config_base.toml
@@ -0,0 +1,10 @@
[db]
driver = "pdo_mysql"
charset = "utf8"

[myproject]
param1 = "123"
param2 = "123"
param4 = [1, 2, 3]

[keys]
Empty file.
14 changes: 14 additions & 0 deletions tests/integration/Fixtures/config_extend.toml
@@ -0,0 +1,14 @@
[db]
host = "127.0.0.1"
dbname = "mydatabase"
user = "root"
password = ""

[myproject]
param2 = "456"
param3 = "456"
param4 = [4, 5, 6]
param5 = "456"

[keys]
set = [1, 2, 3, 4]
5 changes: 5 additions & 0 deletions tests/integration/Fixtures/config_replacement.toml
@@ -0,0 +1,5 @@
%path% = "/var/www"
path.images = "%path%/web/images"
path.upload = "%path%/upload"
%url% = "http://example.com"
url.images = "%url%/images"
2 changes: 2 additions & 0 deletions tests/unit/ConfigServiceProviderTest.php
Expand Up @@ -36,6 +36,8 @@ public function provideFilenamesForFormat()
'json.dist' => array('json', __DIR__."/Fixtures/config.json.dist"),
'php' => array('php', __DIR__."/Fixtures/config.php"),
'php.dist' => array('php', __DIR__."/Fixtures/config.php.dist"),
'toml' => array('toml', __DIR__."/Fixtures/config.toml"),
'toml.dist' => array('toml', __DIR__."/Fixtures/config.toml.dist"),
);
}
}

0 comments on commit f81e73a

Please sign in to comment.