Skip to content

Commit

Permalink
Added TOML file driver
Browse files Browse the repository at this point in the history
  • Loading branch information
PHLAK committed Aug 1, 2018
1 parent 90df586 commit 0475cba
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Supported file formats:
- PHP
- INI
- JSON
- [TOML](https://github.com/toml-lang/toml)
- YAML
- XML

Expand Down Expand Up @@ -138,6 +139,27 @@ valid JSON object.

```

#### TOML

A TOML configuration file must have the `.toml` file extension and be a valid
TOML file.

```toml
driver = 'mysql'

[drivers.sqlite]
database = 'database.sqlite'
prefix = ''

[drivers.mysql]
host = 'localhost'
database = 'blog'
username = 'blogger'
password = 'hunter2'
charset = 'utf8'
prefix = ''
```

#### YAML

A YAML configuration file must have the `.yaml` file extension, be a valid YAML
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
],
"require": {
"php": ">=7.0",
"symfony/yaml": "^3.0 || ^4.0"
"symfony/yaml": "^3.0 || ^4.0",
"yosymfony/toml": "^1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.3",
Expand Down
31 changes: 31 additions & 0 deletions src/Loaders/Toml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace PHLAK\Config\Loaders;

use PHLAK\Config\Exceptions\InvalidFileException;
use Yosymfony\Toml\Toml as TomlParser;
use Yosymfony\Toml\Exception\ParseException;

class Toml extends Loader
{
/**
* Retrieve the contents of a .toml file and convert it to an array of
* configuration options.
*
* @return array Array of configuration options
*/
public function getArray()
{
try {
$parsed = TomlParser::ParseFile($this->context);
} catch (ParseException $e) {
throw new InvalidFileException($e->getMessage());
}

// if (! is_array($parsed)) {
// throw new InvalidFileException('Unable to parse invalid TOML file at ' . $this->context);
// }

return $parsed;
}
}
26 changes: 26 additions & 0 deletions tests/TomlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace PHLAK\Config\Tests;

use PHLAK\Config\Config;
use PHLAK\Config\Exceptions\InvalidFileException;
use PHLAK\Config\Tests\Traits\Initializable;
use PHPUnit\Framework\TestCase;

class TomlTest extends TestCase
{
use Initializable;

public function setUp()
{
$this->validConfig = __DIR__ . '/files/toml/config.toml';
$this->invalidConfig = __DIR__ . '/files/toml/invalid.toml';
}

public function test_it_throws_an_exception_when_initializing_a_toml_file_without_an_array()
{
$this->expectException(InvalidFileException::class);

new Config(__DIR__ . '/files/toml/bad.toml');
}
}
1 change: 1 addition & 0 deletions tests/files/toml/bad.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I'm a valid YAML file but don't contain an array
21 changes: 21 additions & 0 deletions tests/files/toml/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
driver = 'mysql'

[drivers.sqlite]
database = 'database.sqlite'
prefix = ''

[drivers.mysql]
host = 'localhost'
database = 'blog'
username = 'blogger'
password = 'hunter2'
charset = 'utf8'
prefix = ''

[drivers.pgsql]
host = 'localhost'
database = 'blog'
username = 'blogger'
password = 'hunter2'
charset = 'utf8'
prefix = ''
1 change: 1 addition & 0 deletions tests/files/toml/invalid.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asdf: asdf: asdf

0 comments on commit 0475cba

Please sign in to comment.