Last active
August 23, 2019 14:48
-
-
Save jakubboucek/00de406fc3acfc537965ed6355c7840c to your computer and use it in GitHub Desktop.
Array configurator
This file contains hidden or 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
<?php | |
declare(strict_types=1); | |
namespace App; | |
// Bootstrap je třída, která se stará o zavedení aplikace. | |
class Bootstrap | |
{ | |
public static function boot(): AppLoader | |
{ | |
// Zavolá si na pomoc třídu Configurator | |
$configurator = new Configurator(); | |
// Konfurátoru přikáže, aby si načetl konfigurační soubor na uvedené cestě | |
$configurator->addFile(__DIR__ . '/Config/config.json'); | |
// A dále, pokud existuje "lokální" konfigurační soubor, tak ho načte také | |
$configurator->addFileIfExists(__DIR__ . '/../local/Config/config.json'); | |
// Funkce vrátí třítu AppLoader, která bude aplikaci poskytovat | |
return new AppLoader($configurator); | |
} | |
public function getDbConnectionForApp(string $appName): Connection | |
{ | |
$dbConfig = $this->configurator->getConfigForApp($appName)['db']; | |
return = new Connection($dbConfig); | |
} | |
} |
This file contains hidden or 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
{ | |
"apps": { | |
"__common": { | |
"db": { | |
"driver": "mysqli", | |
"host" : "db1.server.cz" | |
} | |
}, | |
"panda": { | |
"db": { | |
"user": "cst-panda", | |
"password": "ukulele", | |
"db": "wbk-root" | |
} | |
}, | |
"vyfakturuj": { | |
"db": { | |
"user": "1001-vyfakturuj", | |
"password": "haleluja20.81", | |
"db": "wbk-vyfakutuj" | |
} | |
} | |
} | |
} |
This file contains hidden or 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
{ | |
"apps": { | |
"__common": { | |
"db": { | |
"host" : "localhost" | |
} | |
}, | |
"panda": { | |
"db": { | |
"password": "", | |
} | |
}, | |
"vyfakturuj": { | |
"db": { | |
"password": "", | |
} | |
} | |
} | |
} |
This file contains hidden or 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
<?php | |
declare(strict_types=1); | |
namespace App; | |
use JsonException; | |
use RuntimeException; | |
// Configurator je třída, která se stará o načtení konfigurace z JSON souborů a jejich pohodlné servírování pro různé aplikace | |
class Configurator | |
{ | |
private const COMMON_CONFIG_KEY = '__common'; | |
private $files = []; | |
private $config; | |
public function addFile(string $filename): void | |
{ | |
$this->files[] = $filename; | |
} | |
public function addFileIfExists(string $filename):void | |
{ | |
if(file_exists($filename)){ | |
$this->addFile($filename); | |
} | |
} | |
public function getConfigForApp(string $appName): array | |
{ | |
$config = $this->loadConfig(); | |
if (isset($config['apps'][$appName]) === false) { | |
$configFiles = implode(', ', $this->files); | |
throw new LogicException("Config for application $appName not found in config. Loaded config files: $configFiles"); | |
} | |
$appConfig = $config['apps'][$appName]; | |
if(isset($config['apps'][self::COMMON_CONFIG_KEY])) { | |
$appConfig = array_replace_recursive($config['apps'][self::COMMON_CONFIG_KEY], $appConfig); | |
} | |
return $appConfig; | |
} | |
private function loadConfig(): array | |
{ | |
if ($this->config !== null) { | |
return $this->config; | |
} | |
$config = []; | |
foreach ($this->files as $file) { | |
$config = array_replace_recursive($config, $this->loadJsonFile($file)); | |
} | |
return $this->config = $config; | |
} | |
private function loadJsonFile(string $filename): array | |
{ | |
$json = @file_get_contents($filename); | |
if ($json === false) { | |
throw new \LogicException("File $filename is not readable"); | |
} | |
$value = json_decode($json, true); | |
if(json_last_error() !== JSON_ERROR_NONE) { | |
throw new JsonException(json_last_error_msg()); | |
} | |
if (is_array($value) === false) { | |
throw new \LogicException("JSON value in file $filename must be array or object"); | |
} | |
return $value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment