Skip to content

Commit

Permalink
ConfigFile class
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesread committed Aug 28, 2023
1 parent 5093d4a commit 3b911bc
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/main/php/libAllure/ConfigFile.php
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');
}
}
52 changes: 52 additions & 0 deletions src/test/php/ConfigFileTest.php
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'));
}
}

0 comments on commit 3b911bc

Please sign in to comment.