-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace libAllure; | ||
|
||
class ConfigFile | ||
{ | ||
private array $keys; | ||
|
||
public $filename = '/config.ini'; | ||
|
||
public function __construct(array $additionalKeys = null, $useDefaultKeys = true) | ||
{ | ||
$this->keys = []; | ||
|
||
if ($useDefaultKeys) { | ||
$this->keys['DB_NAME'] = 'unknown'; | ||
$this->keys['DB_HOST'] = 'localhost'; | ||
$this->keys['DB_USER'] = 'user'; | ||
$this->keys['DB_PASS'] = 'password'; | ||
$this->keys['TIMEZONE'] = 'Europe/London'; | ||
} | ||
|
||
if (is_array($additionalKeys)) { | ||
$this->keys = array_merge($this->keys, $additionalKeys); | ||
} | ||
} | ||
|
||
public function tryLoad(array $possiblePaths) | ||
{ | ||
$foundAConfig = false; | ||
|
||
foreach ($possiblePaths as $path) { | ||
if (file_exists($path . $this->filename)) { | ||
$foundAConfig = true; | ||
$this->load($path . $this->filename); | ||
} | ||
} | ||
|
||
if (!$foundAConfig) { | ||
$this->createConfigFile($possiblePaths[0]); | ||
} | ||
} | ||
|
||
private function createConfigFile($path) | ||
{ | ||
if (!is_writable($path)) { | ||
throw new \Exception('Could not save a default config file as the path is not writable: ' . $path); | ||
} | ||
|
||
$content = ''; | ||
|
||
foreach ($this->keys as $key => $val) { | ||
$content .= $key . '=' . $val . "\n"; | ||
} | ||
|
||
file_put_contents($path . $this->filename, $content); | ||
} | ||
|
||
private function load($fullpath) | ||
{ | ||
$this->keys = parse_ini_file($fullpath, false); | ||
} | ||
|
||
public function getAll(): array | ||
{ | ||
return $this->keys; | ||
} | ||
|
||
public function get($k): ?string | ||
{ | ||
if (isset($this->keys[$k])) { | ||
return $this->keys[$k]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function getDsn($type = 'mysql'): string | ||
{ | ||
return $type . ':dbname=' . $this->get('DB_NAME'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
require_once 'common.php'; | ||
|
||
require_once 'libAllure/ConfigFile.php'; | ||
|
||
use \libAllure\ConfigFile; | ||
use \PHPUnit\Framework\TestCase; | ||
|
||
class ConfigFileTest extends TestCase { | ||
public function setUp(): void | ||
{ | ||
if (!file_exists('/tmp/libAllure')) { | ||
mkdir('/tmp/libAllure/'); | ||
} | ||
|
||
chmod('/tmp/libAllure', 0777); | ||
} | ||
public function testDefaultKeys() | ||
{ | ||
$cfg = new ConfigFile(); | ||
|
||
$this->assertEquals('localhost', $cfg->get('DB_HOST')); | ||
} | ||
|
||
public function testAdditionalKeys() | ||
{ | ||
$cfg = new ConfigFile([ | ||
'blat' => 'hi', | ||
]); | ||
|
||
$this->assertEquals('hi', $cfg->get('blat')); | ||
$this->assertEquals('localhost', $cfg->get('DB_HOST')); | ||
} | ||
|
||
public function testNoAdditionalKeys() | ||
{ | ||
$cfg = new ConfigFile(null, false); | ||
|
||
$this->assertSame(null, $cfg->get('DB_HOST')); | ||
} | ||
|
||
public function testLoad() | ||
{ | ||
$cfg = new ConfigFile(); | ||
$cfg->tryLoad([ | ||
'/tmp/libAllure/', | ||
]); | ||
|
||
$this->assertSame('localhost', $cfg->get('DB_HOST')); | ||
} | ||
} |