Permalink
Browse files

simplify YAML implementation

  • Loading branch information...
igorw committed Mar 24, 2012
1 parent 7e0814e commit 2fd96c9b19122c2d78da3949aa9b4bec60e90919
Showing with 21 additions and 34 deletions.
  1. +2 −0 .gitignore
  2. +2 −0 README.md
  3. +17 −34 src/Igorw/Silex/ConfigServiceProvider.php
View
@@ -0,0 +1,2 @@
+composer.lock
+vendor
View
@@ -57,6 +57,8 @@ To use Yaml instead of JSON, just pass a file that ends on `.yml`:
$app->register(new Igorw\Silex\ConfigServiceProvider(__DIR__."/../config/services.yml"));
+Note, you will have to require the `2.1.*` of the `symfony/yaml` package.
+
### Multiple config files
You can use multiple config files, e. g. one for a whole application and a
@@ -18,17 +18,11 @@
class ConfigServiceProvider implements ServiceProviderInterface
{
private $filename;
-
- private $format;
-
- protected $validFormats = array('json', 'yaml');
-
private $replacements = array();
public function __construct($filename, array $replacements = array())
{
$this->filename = $filename;
- $this->setFormatByFilename();
if ($replacements) {
foreach ($replacements as $key => $value) {
@@ -39,10 +33,7 @@ public function __construct($filename, array $replacements = array())
public function register(Application $app)
{
- if (null === ($config = $this->readConfig())) {
- throw new \InvalidArgumentException(
- sprintf("The config file '%s' appears to be invalid.", $this->filename));
- }
+ $config = $this->readConfig();
foreach ($config as $name => $value) {
$app[$name] = $this->doReplacements($value);
@@ -66,40 +57,32 @@ private function doReplacements($value)
return strtr($value, $this->replacements);
}
- private function setFormatByFilename($filename = null)
- {
- $filename = $filename ? $filename : $this->filename;
- $this->format = str_replace('yml', 'yaml', pathinfo($this->filename, PATHINFO_EXTENSION));
- if (!in_array($this->format, $this->validFormats)) {
- throw new \InvalidArgumentException(
- sprintf("The '%s' format is not supported, try json or yaml and ensure the config file has the right file ending.", $this->format));
- }
- }
-
private function readConfig()
{
- $config = null;
- if (!$this->filename || !$this->format) {
+ $format = pathinfo($this->filename, PATHINFO_EXTENSION);
+
+ if (!$this->filename || !$format) {
throw new \RuntimeException('A valid configuration file must be passed before reading the config.');
}
+
if (!file_exists($this->filename)) {
throw new \InvalidArgumentException(
sprintf("The config file '%s' does not exist.", $this->filename));
}
- switch ($this->format) {
- case 'yaml':
- 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($this->filename);
- break;
- case 'json':
- default:
- $config = json_decode(file_get_contents($this->filename), true);
- break;
+
+ if ('yml' === $format) {
+ if (!class_exists('Symfony\\Component\\Yaml\\Yaml')) {
+ throw new \RuntimeException('Unable to read yaml as the Symfony Yaml Component is not installed.');
+ }
+ return Yaml::parse($this->filename);
+ }
+
+ if ('json' === $format) {
+ return json_decode(file_get_contents($this->filename), true);
}
- return $config;
+ throw new \InvalidArgumentException(
+ sprintf("The config file '%s' appears has invalid format '%s'.", $this->filename, $format));
}
}

0 comments on commit 2fd96c9

Please sign in to comment.