1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/kanboard_ynh.git synced 2024-09-03 19:36:17 +02:00
kanboard_ynh/sources/app/ServiceProvider/DatabaseProvider.php

105 lines
2.5 KiB
PHP
Raw Normal View History

2014-12-22 19:15:38 +01:00
<?php
namespace ServiceProvider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
2015-01-16 14:23:05 +01:00
use PicoDb\Database;
2014-12-22 19:15:38 +01:00
2015-01-16 14:23:05 +01:00
class DatabaseProvider implements ServiceProviderInterface
2014-12-22 19:15:38 +01:00
{
public function register(Container $container)
{
$container['db'] = $this->getInstance();
2015-01-16 14:23:05 +01:00
$container['db']->stopwatch = DEBUG;
$container['db']->log_queries = DEBUG;
2014-12-22 19:15:38 +01:00
}
/**
* Setup the database driver and execute schema migration
*
2015-01-16 14:23:05 +01:00
* @return \PicoDb\Database
2014-12-22 19:15:38 +01:00
*/
public function getInstance()
{
switch (DB_DRIVER) {
case 'sqlite':
$db = $this->getSqliteInstance();
break;
case 'mysql':
$db = $this->getMysqlInstance();
break;
case 'postgres':
$db = $this->getPostgresInstance();
break;
default:
die('Database driver not supported');
}
if ($db->schema()->check(\Schema\VERSION)) {
return $db;
}
else {
$errors = $db->getLogMessages();
die('Unable to migrate database schema: <br/><br/><strong>'.(isset($errors[0]) ? $errors[0] : 'Unknown error').'</strong>');
}
}
/**
* Setup the Sqlite database driver
*
2015-01-16 14:23:05 +01:00
* @return \PicoDb\Database
2014-12-22 19:15:38 +01:00
*/
function getSqliteInstance()
{
require_once __DIR__.'/../Schema/Sqlite.php';
2015-01-16 14:23:05 +01:00
return new Database(array(
2014-12-22 19:15:38 +01:00
'driver' => 'sqlite',
'filename' => DB_FILENAME
));
}
/**
* Setup the Mysql database driver
*
2015-01-16 14:23:05 +01:00
* @return \PicoDb\Database
2014-12-22 19:15:38 +01:00
*/
function getMysqlInstance()
{
require_once __DIR__.'/../Schema/Mysql.php';
2015-01-16 14:23:05 +01:00
return new Database(array(
2014-12-22 19:15:38 +01:00
'driver' => 'mysql',
'hostname' => DB_HOSTNAME,
'username' => DB_USERNAME,
'password' => DB_PASSWORD,
'database' => DB_NAME,
'charset' => 'utf8',
2015-04-21 17:56:16 +02:00
'port' => DB_PORT,
2014-12-22 19:15:38 +01:00
));
}
/**
* Setup the Postgres database driver
*
2015-01-16 14:23:05 +01:00
* @return \PicoDb\Database
2014-12-22 19:15:38 +01:00
*/
public function getPostgresInstance()
{
require_once __DIR__.'/../Schema/Postgres.php';
2015-01-16 14:23:05 +01:00
return new Database(array(
2014-12-22 19:15:38 +01:00
'driver' => 'postgres',
'hostname' => DB_HOSTNAME,
'username' => DB_USERNAME,
'password' => DB_PASSWORD,
'database' => DB_NAME,
2015-04-21 17:56:16 +02:00
'port' => DB_PORT,
2014-12-22 19:15:38 +01:00
));
}
}