szurubooru/src/DatabaseConnection.php

38 lines
630 B
PHP
Raw Normal View History

<?php
namespace Szurubooru;
final class DatabaseConnection
{
2014-09-14 16:16:15 +02:00
private $pdo;
private $config;
public function __construct(\Szurubooru\Config $config)
{
2014-09-14 16:16:15 +02:00
$this->config = $config;
}
2014-09-14 16:16:15 +02:00
public function getPDO()
{
2014-09-14 16:16:15 +02:00
if (!$this->pdo)
{
$this->createPDO();
}
return $this->pdo;
}
2014-09-14 16:16:15 +02:00
public function close()
{
2014-09-14 16:16:15 +02:00
$this->pdo = null;
}
2014-09-14 16:16:15 +02:00
private function createPDO()
{
2014-09-14 16:16:15 +02:00
$cwd = getcwd();
if ($this->config->getDataDirectory())
chdir($this->config->getDataDirectory());
$this->pdo = new \PDO($this->config->database->dsn);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
chdir($cwd);
}
}