Created
December 12, 2012 00:46
-
-
Save pablofmorales/4263841 to your computer and use it in GitHub Desktop.
Trying Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Providers; | |
use Silex\Application; | |
use Silex\ServiceProviderInterface; | |
use Symfony\Component\Yaml\Yaml; | |
class ConfigServiceProvider implements ServiceProviderInterface | |
{ | |
private $filename; | |
private $replacements = array(); | |
public function __construct($filename, array $replacements = array()) | |
{ | |
$this->filename = $filename; | |
if ($replacements) { | |
foreach ($replacements as $key => $value) { | |
$this->replacements['%'.$key.'%'] = $value; | |
} | |
} | |
} | |
public function register(Application $app) | |
{ | |
$config = $this->readConfig(); | |
foreach ($config as $name => $value) { | |
$app[$name] = $this->doReplacements($value); | |
} | |
} | |
public function boot(Application $app) | |
{ | |
} | |
private function doReplacements($value) | |
{ | |
if (!$this->replacements) { | |
return $value; | |
} | |
if (is_array($value)) { | |
foreach ($value as $k => $v) { | |
$value[$k] = $this->doReplacements($v); | |
} | |
return $value; | |
} | |
if (is_string($value)) { | |
return strtr($value, $this->replacements); | |
} | |
return $value; | |
} | |
private function readConfig() | |
{ | |
$config = Yaml::parse($this->filename); | |
return ($config) ? $config : array(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is just a touch better than pastebin :lol: