Created
January 12, 2016 13:33
-
-
Save Pierozi/6c811706034ed9fa3119 to your computer and use it in GitHub Desktop.
php config
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
<?php | |
/** | |
* Like class is single, specify DIRECTIVE seems not be mandatory | |
*/ | |
class Single extends ObjectivePHP\Config\SingleValueDirective {} | |
$config->import(new Single('x')) | |
->import(new Single('y')); | |
$config->get(Single::class) == "y"; | |
/* ************************************************ */ | |
class DbConf extends ObjectivePHP\Config\ObjectValueDirectiveGroup | |
{ | |
const host = 'string'; | |
const port = 'int'; | |
const username = 'string'; | |
const password = 'string'; | |
const dbname = 'string'; | |
public function toDsn() | |
{ | |
return 'pgsql:host=' . $this->host | |
. ';port=' . $this->port | |
. ';dbname=' . $this->dbname | |
. ';user=' . $this->username | |
. ';password=' . $this->password | |
; | |
} | |
} | |
$config->import(new DbConf('dev', 'host', 'localhost')); | |
->import(new DbConf('dev', 'port', 5432)); | |
->import(new DbConf('dev', 'dbname', 'myDb')); | |
->import(new DbConf('dev', 'user', 'foo')); | |
->import(new DbConf('dev', 'password', 'bar')); | |
$config->get(DbConf::class . '.dev', 'host') === 'localhost'; | |
$dbConfDev = $config->subset(DbConf::class . '.dev')->toObject(); | |
$dbConfDev->host === 'localhost'; | |
$dbConfDev->port === 5432; | |
$dbConfDev->toDsn() === 'pgsql:host=localhost;port=5432;dbname=myDb;user=foo;password=bar'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment