Izartu  0.0 Initial release
Web bookmark manager based on tags.
 All Data Structures Files Functions Variables Enumerations
Database.php
Go to the documentation of this file.
00001 <?php
00002 #  Izartu
00003 #
00004 #  Copyright © 2011-2012 Javier Beaumont <contact@javierbeaumont.org>
00005 #
00006 #  This file is part of Izartu.
00007 #
00008 #  Izartu is free software: you can redistribute it and/or modify
00009 #  it under the terms of the GNU Affero General Public License as
00010 #  published by the Free Software Foundation, either version 3 of the
00011 #  License, or (at your option) any later version.
00012 #
00013 #  Izartu is distributed in the hope that it will be useful,
00014 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016 #  GNU Affero General Public License for more details.
00017 #
00018 #  You should have received a copy of the GNU Affero General Public License
00019 #  along with Izartu. If not, see <http://www.gnu.org/licenses/>.
00020 
00033 class Database {
00034   static protected $db;
00035 
00042   final public function __construct() {
00043     if(!static::$db) {
00044       try {
00045         static::$db = new PDO($this->pdoMySQL(), DB_USER, DB_PASS);
00046       } catch (PDOException $e) {
00047         trigger_error($e->getMessage(), E_USER_ERROR);
00048       }
00049     }
00050   }
00051 
00058   final private function pdoMySQL() {
00059     $dsn = 'mysql:';
00060     // Unix Socket
00061     if (strncmp(DB_HOST, '/', 1)) {
00062       // DB Host
00063       DB_HOST? $dsn .= 'host='.DB_HOST: $dsn .= 'host=localhost';
00064       // DB Port
00065       if (DB_PORT AND is_int(DB_PORT) AND DB_PORT != 3306)
00066         $dsn .= ';port='.DB_PORT;
00067     } else {
00068       $dsn .= 'unix_socket='.DB_HOST;
00069     }
00070     // DB Name
00071     if (DB_NAME)
00072       $dsn .= ';dbname='.DB_NAME;
00073     return $dsn;
00074   }
00075 
00076 }
 All Data Structures Files Functions Variables Enumerations