Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Dec 12, 2012

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
You can’t perform that action at this time.