Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
language: php

php:
- 5.3.3
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

before_script:
- composer self-update
- composer install --dev --prefer-source

script: phpunit --coverage-text
script: vendor/bin/phpunit --coverage-text
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

* [2.0.0] - 2017-04-24

* Upgrade to compatibility with Silex2

* 1.2.2 (2014-07-06)

* Feature: Configuration prefix (@desyncr)
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
[![Build Status](https://travis-ci.org/youssman/ConfigServiceProvider.svg?branch=master)](https://travis-ci.org/youssman/ConfigServiceProvider)

> This repo is a fork of the lib [igorw/ConfigServiceProvider](https://github.com/igorw/ConfigServiceProvider) bringing support for Silex2. A pull request was submitted but not yet accepted.

# ConfigServiceProvider

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

## Installation

Put these lines to your composer.json:

"require": {
"youssman/config-service-provider": "~2.0"
},
...
"repositories": [
{
"type": "vcs",
"url": "https://github.com/youssman/ConfigServiceProvider"
}
]

## Usage

### Passing a config file
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "igorw/config-service-provider",
"description": "A config ServiceProvider for Silex with support for php, json and yaml.",
"keywords": ["silex"],
"name": "youssman/config-service-provider",
"description": "A config ServiceProvider for Silex 2 with support for php, json and yaml.",
"keywords": ["silex 2", "php"],
"license": "MIT",
"authors": [
{
Expand All @@ -14,11 +14,12 @@
}
],
"require": {
"silex/silex": "~1.0"
"silex/silex": "~2.0"
},
"require-dev": {
"symfony/yaml": "~2.1",
"jamesmoss/toml": "~0.1"
"jamesmoss/toml": "~0.1",
"phpunit/phpunit": "^5.7.19"
},
"suggest": {
"symfony/yaml": "~2.1",
Expand Down
42 changes: 22 additions & 20 deletions src/Igorw/Silex/ConfigServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@

namespace Igorw\Silex;

use Silex\Application;
use Silex\ServiceProviderInterface;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

class ConfigServiceProvider implements ServiceProviderInterface
{
private $filename;
private $replacements = array();
private $replacements = [];
private $driver;
private $prefix = null;

public function __construct($filename, array $replacements = array(), ConfigDriver $driver = null, $prefix = null)
public function __construct($filename, array $replacements = [], ConfigDriver $driver = null, $prefix = null)
{
$this->filename = $filename;
$this->prefix = $prefix;
Expand All @@ -32,40 +32,42 @@ public function __construct($filename, array $replacements = array(), ConfigDriv
}
}

$this->driver = $driver ?: new ChainConfigDriver(array(
$this->driver = $driver ?: new ChainConfigDriver([
new PhpConfigDriver(),
new YamlConfigDriver(),
new JsonConfigDriver(),
new TomlConfigDriver(),
));
]);
}

public function register(Application $app)
public function register(Container $pimple)
{
$config = $this->readConfig();

foreach ($config as $name => $value)
if ('%' === substr($name, 0, 1))
$this->replacements[$name] = (string) $value;

$this->merge($app, $config);
}
foreach ($config as $name => $value) {
if ('%' === substr($name, 0, 1)) {
$this->replacements[$name] = preg_replace_callback(
'/(%.+%)/U', function ($matches) use ($config) {
return $this->replacements[$matches[0]];
},
(string) $value);
}
}

public function boot(Application $app)
{
$this->merge($pimple, $config);
}

private function merge(Application $app, array $config)
private function merge(Container $pimple, array $config)
{
if ($this->prefix) {
$config = array($this->prefix => $config);
$config = [$this->prefix => $config];
}

foreach ($config as $name => $value) {
if (isset($app[$name]) && is_array($value)) {
$app[$name] = $this->mergeRecursively($app[$name], $value);
if (isset($pimple[$name]) && is_array($value)) {
$pimple[$name] = $this->mergeRecursively($pimple[$name], $value);
} else {
$app[$name] = $this->doReplacements($value);
$pimple[$name] = $this->doReplacements($value);
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/Igorw/Silex/JsonConfigDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ class JsonConfigDriver implements ConfigDriver
{
public function load($filename)
{
$config = $this->parseJson($filename);

if (JSON_ERROR_NONE !== json_last_error()) {
$jsonError = $this->getJsonError(json_last_error());
$json = file_get_contents($filename);
$config = $this->parseJson($json);
// determine if there was an error
// suppress PHP 7's syntax error for empty JSON strings
$errorCode = json_last_error();
if (JSON_ERROR_NONE !== $errorCode && ($errorCode !== JSON_ERROR_SYNTAX || $json !== '')) {
$jsonError = $this->getJsonError($errorCode);
throw new \RuntimeException(
sprintf('Invalid JSON provided "%s" in "%s"', $jsonError, $filename));
}
Expand All @@ -22,9 +25,8 @@ public function supports($filename)
return (bool) preg_match('#\.json(\.dist)?$#', $filename);
}

private function parseJson($filename)
private function parseJson($json)
{
$json = file_get_contents($filename);
return json_decode($json, true);
}

Expand All @@ -40,4 +42,4 @@ private function getJsonError($code)

return isset($errorMessages[$code]) ? $errorMessages[$code] : 'Unknown';
}
}
}
2 changes: 1 addition & 1 deletion src/Igorw/Silex/YamlConfigDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function load($filename)
if (!class_exists('Symfony\\Component\\Yaml\\Yaml')) {
throw new \RuntimeException('Unable to read yaml as the Symfony Yaml Component is not installed.');
}
$config = Yaml::parse($filename);
$config = Yaml::parse(file_get_contents($filename));
return $config ?: array();
}

Expand Down
Loading