Skip to content

Commit

Permalink
REFACTOR ConfigManager to use apply Singleton Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Bauernfeind committed Oct 14, 2018
1 parent 9c21e99 commit b944006
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
4 changes: 1 addition & 3 deletions lib/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ class Connection
{
public $entitymanager;
private $isDevMode = true;
private $config;

public function __construct()
{
$this->config = new ConfigManager;
$driver = envar('DATABASE_DRIVER', 'pdo_mysql');
$dbType = envar('DATABASE_TYPE', 'mysql');
switch ($dbType) {
case "sqlite":
$path = dirname(__FILE__, 3) . "/" . $this->config->get("database.$dbType.database");
$path = dirname(__FILE__, 3) . "/" . ConfigManager::get("database.$dbType.database");
$conn = array(
'driver' => $driver,
'path' => $path,
Expand Down
38 changes: 31 additions & 7 deletions lib/Framework/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,40 @@
*/
class ConfigManager implements ArrayAccess
{
/** @var array */
/** @var static */
protected static $instance;

/** @var array */
private $config = [];

/**
* @return ConfigManager
*/
public static function instance()
{
if (!static::$instance) {
static::$instance = new self;
}

return static::$instance;
}

/**
* @param $property
* @return mixed|null
*/
public static function get($property)
{
return static::instance()->getProperty($property);
}

/**
* Config constructor.
*/
public function __construct()
protected function __construct()
{
// Read the config files
$configDir = dirname(__FILE__, 3) . "/Config/*.php";
$configDir = dirname(__FILE__, 3) . "/Config/*.php";

foreach (glob($configDir) as $configFile) {
$configs = include($configFile);
Expand Down Expand Up @@ -65,7 +89,7 @@ public function offsetUnset($offset)
*/
public function offsetGet($offset)
{
return isset($this->config[$offset]) ? $this->config[$offset] : null;
return isset($this->config[$offset]) ? $this->config[$offset] : null;
}

/**
Expand All @@ -74,14 +98,14 @@ public function offsetGet($offset)
* @param $property
* @return mixed|null
*/
public function get($property)
protected function getProperty($property)
{
$value = $this->config;
$path = explode('.', $property);

while(sizeof($path) > 0 && is_array($value)) {
while (sizeof($path) > 0 && is_array($value)) {
$nextKey = array_shift($path);
if (! key_exists($nextKey, $value)) {
if (!key_exists($nextKey, $value)) {
return null;
}
$value = $value[$nextKey];
Expand Down
3 changes: 1 addition & 2 deletions lib/Framework/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public function __construct()
});

$this->add('Config', function () {
$config = new ConfigManager;
return $config;
return ConfigManager::instance();
});

$this->add('Logger', function () {
Expand Down
3 changes: 1 addition & 2 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

// set session storage location and
// start the session
$config = new \Lib\Framework\ConfigManager;
ini_set('session.save_path', dirname(__FILE__, 2) . '/storage/sessions');
ini_set('session.gc_probability', $config->get('app.gcProb'));
ini_set('session.gc_probability',\Lib\Framework\ConfigManager::get('app.gcProb'));
session_start();

// set the timeout for the session
Expand Down

0 comments on commit b944006

Please sign in to comment.