refactored Autoloader; added support for json encoded format for environment "hosts" option

This commit is contained in:
Vladimir Grigor
2014-07-28 11:53:22 +03:00
parent 6ecaf41336
commit 5ce3394b3c
5 changed files with 71 additions and 49 deletions
+33 -9
View File
@@ -24,7 +24,8 @@ use Exception;
*/
class Config
{
/**
const HOST_NAME_LENGTH = 1000;
/**
* Arguments loaded
* @var array
*/
@@ -353,13 +354,7 @@ class Config
if (is_array($envConfig['hosts'])) {
$hosts = (array) $envConfig['hosts'];
} else if (is_string($envConfig['hosts']) && file_exists($envConfig['hosts']) && is_readable($envConfig['hosts'])) {
$fileContent = fopen($envConfig['hosts'], 'r');
while (($host = fgets($fileContent)) == true) {
$host = trim($host);
if ($host != '') {
$hosts[] = $host;
}
}
$hosts = $this->getHostsFromFile($envConfig['hosts']);
}
}
@@ -565,7 +560,7 @@ class Config
* @param mixed $default
* @return mixed
*/
protected function getEnvironmentOption($option, $default = array())
public function getEnvironmentOption($option, $default = array())
{
$config = $this->getEnvironmentConfig();
if (isset($config[$option])) {
@@ -608,4 +603,33 @@ class Config
return $this->environmentConfig;
}
/**
* @param string $filePath
*
* @return array
*/
protected function getHostsFromFile($filePath)
{
$handle = fopen($filePath, 'r');
$hosts = array();
try {
$fileContent = stream_get_contents($handle);
$hosts = json_decode($fileContent);
} catch (Exception $e) {
rewind($handle);
//do it old-style: one host per line
while (($host = stream_get_line($handle, self::HOST_NAME_LENGTH)) !== false) {
$host = trim($host);
if (!empty($host)) {
$hosts[] = $host;
}
}
}
return $hosts;
}
}