Skip to content

Commit

Permalink
Merge pull request #8 from jmolivas/5-project-specific-configuration
Browse files Browse the repository at this point in the history
#5 Add per-project configuration feature
  • Loading branch information
jmolivas committed Jul 22, 2015
2 parents ec3383b + 49cdb5f commit e9e1189
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ class Config
{
protected $config;

protected $custom = false;

public function __construct()
{
$this->config = [];
$this->loadFile(__DIR__.'/../'.'config.yml');
if ($this->getApplicationConfigFile()) {
$this->loadFile($this->getApplicationConfigFile());
$this->custom = true;
}
$this->loadFile($this->getBaseConfigDirectory().'messages.yml');
$this->loadFile($this->getUserHomeDirectory().'messages.yml');
}
Expand Down Expand Up @@ -57,6 +63,11 @@ public function get($key, $default = '')
return $config;
}

public function getApplicationDirectory()
{
return getcwd() . '/';
}

public function getBaseConfigDirectory()
{
return __DIR__.'/../config/';
Expand All @@ -72,8 +83,27 @@ public function getUserHomeDirectory()
return rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '/\\');
}

public function getApplicationConfigFile()
{
$configFile = $this->getApplicationDirectory() .'phpqa.yml';
if (file_exists($configFile)) {
return $configFile;
}

$configFile = $this->getApplicationDirectory() .'phpqa.yml.dist';
if (file_exists($configFile)) {
return $configFile;
}

return null;
}

public function loadProjectConfiguration($project)
{
if ($this->custom) {
return;
}

$configFile = $this->getUserConfigDirectory().$project.'/config.yml';
if (file_exists($configFile)) {
$this->loadFile($configFile);
Expand All @@ -100,12 +130,12 @@ public function getProjectAnalyzerConfigFile($project, $analyzer)
$analyserConfigOption = key($analyserConfig);
$analyserConfigFile = current($analyserConfig);

$configFile = __DIR__.'/../'.$analyserConfigFile;
$configFile = $this->getApplicationDirectory().$analyserConfigFile;
if (file_exists($configFile)) {
return '--'.$analyserConfigOption.'='.$configFile;
}

$configFile = getcwd().'/'.$analyserConfigFile;
$configFile = __DIR__.'/../'.$analyserConfigFile;
if (file_exists($configFile)) {
return '--'.$analyserConfigOption.'='.$configFile;
}
Expand All @@ -127,9 +157,4 @@ public function getProjectAnalyzerConfigFile($project, $analyzer)

return '--'.$analyserConfigOption.'='.$analyserConfigFile;
}

public function getConfigData()
{
return $this->config;
}
}

0 comments on commit e9e1189

Please sign in to comment.