Skip to content

Commit

Permalink
Add comments to the code; change method access levels
Browse files Browse the repository at this point in the history
  • Loading branch information
lokhman committed Jul 29, 2015
1 parent c6467fe commit b1f526e
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions src/ConfigServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ class ConfigServiceProvider implements ServiceProviderInterface {
const ENVDEV = 'dev';
const CFGEXT = '.json';

private $env;
private $params;
private $dir;
protected $env;
protected $params;
protected $dir;

/**
* Class constructor.
*
* @param string $dir
* @param array $params
* @param string $env
*/
public function __construct($dir, array $params = [], $env = null) {
$this->env = $env ? : getenv(self::ENVVAR) ? : self::ENVDEV;
$this->dir = realpath($dir);
Expand All @@ -30,22 +37,45 @@ public function __construct($dir, array $params = [], $env = null) {
];
}

private static function load($path) {
/**
* Load file contents from the path.
*
* @param string $path
*
* @throws \RuntimeException
* @return string
*/
protected static function load($path) {
if (!is_file($path) || !is_readable($path)) {
throw new \RuntimeException('Unable to load config from ' . $path);
}
return file_get_contents($path);
}

private static function parse($str) {
/**
* Convert file contents to PHP literal.
*
* @param string $str
*
* @throws \RuntimeException
* @return mixed
*/
protected static function parse($str) {
$result = json_decode($str, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \RuntimeException('JSON format is invalid');
}
return $result;
}

private function replace($value) {
/**
* Recursive replace of tokens.
*
* @param mixed $value
*
* @return mixed
*/
protected function replace($value) {
if (!$this->params) {
return $value;
}
Expand All @@ -61,6 +91,11 @@ private function replace($value) {
return $value;
}

/**
* Registers Config service in the given app.
*
* @param \Silex\Application $app
*/
public function register(Application $app) {
$path = $this->dir . DIRECTORY_SEPARATOR . $this->env . self::CFGEXT;
foreach (self::parse(self::load($path)) as $key => $value) {
Expand All @@ -70,6 +105,11 @@ public function register(Application $app) {
}
}

/**
* Bootstraps the application.
*
* @param \Silex\Application $app
*/
public function boot(Application $app) {
/* not implemented */
}
Expand Down

0 comments on commit b1f526e

Please sign in to comment.