From 89d245f29d4a1f92caf862633d471bdc26d60eaf Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Mon, 22 Sep 2014 15:57:24 +0800 Subject: [PATCH] Make db properly configurable. --- .gitignore | 3 ++- DataAccess/DataMapper/DataMapper.php | 5 +++-- config/DI.php | 3 ++- config/db.php.example | 7 +++++++ 4 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 config/db.php.example diff --git a/.gitignore b/.gitignore index 7f3b1d0..2026b2f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ nbproject logs composer.lock -vendor \ No newline at end of file +vendor +db.php diff --git a/DataAccess/DataMapper/DataMapper.php b/DataAccess/DataMapper/DataMapper.php index b33923c..9944116 100644 --- a/DataAccess/DataMapper/DataMapper.php +++ b/DataAccess/DataMapper/DataMapper.php @@ -14,8 +14,9 @@ class DataMapper implements IDataMapper private $_db; private $_maps; - public function __construct($maps) + public function __construct($maps, $dbCredentials) { + $credentials = include $dbCredentials; //TODO: should probably do all this through a configuration object or something $dsn = 'mysql:host=localhost;dbname=divinelegy;charset=utf8'; $username = 'root'; @@ -23,7 +24,7 @@ class DataMapper implements IDataMapper $options = array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); - $this->_db = new PDO($dsn, $username, $password, $options); + $this->_db = new PDO($dsn, $credentials['user'], $credentials['pass'], $options); $this->_maps = include $maps; } diff --git a/config/DI.php b/config/DI.php index b1dbecc..4ba7524 100644 --- a/config/DI.php +++ b/config/DI.php @@ -4,6 +4,7 @@ return [ //values 'datamapper.maps' => '../config/DataMaps.php', 'router.maps' => '../config/Routes.php', + 'db.credentials' => '../config/db.php', //entites 'Domain\Entities\StepMania\ISimfile' => DI\object('Domain\Entities\StepMania\Simfile'), @@ -17,6 +18,6 @@ return [ //DA 'DataAccess\StepMania\ISimfileRepository' => DI\object('DataAccess\StepMania\SimfileRepository'), 'DataAccess\DataMapper\IDataMapper' => DI\object('DataAccess\DataMapper\DataMapper') - ->constructor(DI\link('datamapper.maps')), + ->constructor(DI\link('datamapper.maps'), DI\link('db.credentials')), 'DataAccess\Queries\IQueryBuilderFactory' => DI\object('DataAccess\Queries\QueryBuilderFactory') ]; diff --git a/config/db.php.example b/config/db.php.example new file mode 100644 index 0000000..7a696bb --- /dev/null +++ b/config/db.php.example @@ -0,0 +1,7 @@ + 'username', + 'pass' => 'password' +]; + -- 2.11.0