From f81e73ad0833fe92915ef151f6461646cabfc514 Mon Sep 17 00:00:00 2001 From: Raphael Stolt Date: Tue, 23 Apr 2013 10:57:48 +0200 Subject: [PATCH] Added support for TOML configuration files --- README.md | 12 +++++- composer.json | 6 ++- src/Igorw/Silex/ConfigServiceProvider.php | 14 +++++++ .../integration/ConfigServiceProviderTest.php | 42 +++++++++++++++++++ tests/integration/Fixtures/broken.toml | 1 + tests/integration/Fixtures/config.toml | 2 + tests/integration/Fixtures/config_base.toml | 10 +++++ tests/integration/Fixtures/config_empty.toml | 0 tests/integration/Fixtures/config_extend.toml | 14 +++++++ .../Fixtures/config_replacement.toml | 5 +++ tests/unit/ConfigServiceProviderTest.php | 2 + 11 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 tests/integration/Fixtures/broken.toml create mode 100644 tests/integration/Fixtures/config.toml create mode 100644 tests/integration/Fixtures/config_base.toml create mode 100644 tests/integration/Fixtures/config_empty.toml create mode 100644 tests/integration/Fixtures/config_extend.toml create mode 100644 tests/integration/Fixtures/config_replacement.toml diff --git a/README.md b/README.md index cbd093c..d8ce3dc 100644 --- a/README.md +++ b/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 @@ -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 diff --git a/composer.json b/composer.json index a23a3d6..62ef73f 100644 --- a/composer.json +++ b/composer.json @@ -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" } diff --git a/src/Igorw/Silex/ConfigServiceProvider.php b/src/Igorw/Silex/ConfigServiceProvider.php index 082f398..4a85e3a 100644 --- a/src/Igorw/Silex/ConfigServiceProvider.php +++ b/src/Igorw/Silex/ConfigServiceProvider.php @@ -14,6 +14,7 @@ use Silex\Application; use Silex\ServiceProviderInterface; use Symfony\Component\Yaml\Yaml; +use Toml\Parser; class ConfigServiceProvider implements ServiceProviderInterface { @@ -62,6 +63,10 @@ public function getFileFormat() return 'php'; } + if (preg_match('#.toml(.dist)?$#i', $filename)) { + return 'toml'; + } + return pathinfo($filename, PATHINFO_EXTENSION); } @@ -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)); } diff --git a/tests/integration/ConfigServiceProviderTest.php b/tests/integration/ConfigServiceProviderTest.php index 632e35f..93cbb1b 100644 --- a/tests/integration/ConfigServiceProviderTest.php +++ b/tests/integration/ConfigServiceProviderTest.php @@ -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 @@ -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"), ); } @@ -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"), ); } @@ -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"), ); } diff --git a/tests/integration/Fixtures/broken.toml b/tests/integration/Fixtures/broken.toml new file mode 100644 index 0000000..53ba7e8 --- /dev/null +++ b/tests/integration/Fixtures/broken.toml @@ -0,0 +1 @@ +broken = [ 1, 2.0 ] \ No newline at end of file diff --git a/tests/integration/Fixtures/config.toml b/tests/integration/Fixtures/config.toml new file mode 100644 index 0000000..23ce38f --- /dev/null +++ b/tests/integration/Fixtures/config.toml @@ -0,0 +1,2 @@ +debug = true +data = "%data%" \ No newline at end of file diff --git a/tests/integration/Fixtures/config_base.toml b/tests/integration/Fixtures/config_base.toml new file mode 100644 index 0000000..c62d4f8 --- /dev/null +++ b/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] \ No newline at end of file diff --git a/tests/integration/Fixtures/config_empty.toml b/tests/integration/Fixtures/config_empty.toml new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/Fixtures/config_extend.toml b/tests/integration/Fixtures/config_extend.toml new file mode 100644 index 0000000..a3efbb2 --- /dev/null +++ b/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] \ No newline at end of file diff --git a/tests/integration/Fixtures/config_replacement.toml b/tests/integration/Fixtures/config_replacement.toml new file mode 100644 index 0000000..a8d2ac2 --- /dev/null +++ b/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" \ No newline at end of file diff --git a/tests/unit/ConfigServiceProviderTest.php b/tests/unit/ConfigServiceProviderTest.php index 28eb817..c2a12c2 100644 --- a/tests/unit/ConfigServiceProviderTest.php +++ b/tests/unit/ConfigServiceProviderTest.php @@ -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"), ); } }