Skip to content

Instantly share code, notes, and snippets.

@pablofmorales
Created December 12, 2012 00:46
Show Gist options
  • Save pablofmorales/4263841 to your computer and use it in GitHub Desktop.
Save pablofmorales/4263841 to your computer and use it in GitHub Desktop.
Trying Gist
<?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();
}
}
@pablofmorales
Copy link
Author

is just a touch better than pastebin :lol:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment