|
| 1 | +<?php |
| 2 | + |
| 3 | +//Catch any errors |
| 4 | +if (defined("MBDBRUN")) |
| 5 | + return; |
| 6 | +define("MBDBRUN", 1); |
| 7 | + |
| 8 | +/** |
| 9 | + * Master database control class |
| 10 | + */ |
| 11 | +class MBDB { |
| 12 | + static $databases = null; |
| 13 | + |
| 14 | + /** |
| 15 | + * Get the hostname for the specified database |
| 16 | + * @param string $db The database's identifier |
| 17 | + * @return string The database's hostname |
| 18 | + */ |
| 19 | + static function getDatabaseHost($db) { |
| 20 | + return self::$databases[$db]["host"]; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Get the database name for the specified database |
| 25 | + * @param string $db The database's identifier |
| 26 | + * @return string The database's name |
| 27 | + */ |
| 28 | + static function getDatabaseName($db) { |
| 29 | + return self::$databases[$db]["data"]; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Get the username to access the specified database |
| 34 | + * @param string $db The database's identifier |
| 35 | + * @return string The username to access the database |
| 36 | + */ |
| 37 | + static function getDatabaseUser($db) { |
| 38 | + return self::$databases[$db]["user"]; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Get the password to access the specified database |
| 43 | + * @param string $db The database's identifier |
| 44 | + * @return string The password to access the database |
| 45 | + */ |
| 46 | + static function getDatabasePass($db) { |
| 47 | + return self::$databases[$db]["pass"]; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Add a database to the class's list of databases |
| 52 | + * @param string $ident The database's identifier |
| 53 | + * @param string $host The host for the database |
| 54 | + * @param string $data The database name for the database |
| 55 | + * @param string $user The username to access the database |
| 56 | + * @param string $pass The password to access the database |
| 57 | + */ |
| 58 | + static function addDatabase($ident = null, $host = null, $data = null, $user = null, $pass = null) { |
| 59 | + if ($ident == null || $host == null || $data == null || $user == null || $pass == null) |
| 60 | + return; |
| 61 | + if (self::$databases == null) { |
| 62 | + self::$databases = array(); |
| 63 | + } |
| 64 | + |
| 65 | + //%b -> resolves to getBranch() |
| 66 | + $ident = str_replace("%b", self::getBranch(), $ident); |
| 67 | + $host = str_replace("%b", self::getBranch(), $host); |
| 68 | + $data = str_replace("%b", self::getBranch(), $data); |
| 69 | + $user = str_replace("%b", self::getBranch(), $user); |
| 70 | + $pass = str_replace("%b", self::getBranch(), $pass); |
| 71 | + |
| 72 | + self::$databases[$ident] = array("host" => $host, "data" => $data, "user" => $user, "pass" => $pass); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get the current site "branch" (dev | staging | prod) |
| 77 | + * @return string The current branch |
| 78 | + */ |
| 79 | + static function getBranch() { |
| 80 | + $branch = basename(dirname(__DIR__)); |
| 81 | + return $branch; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +//Default DBs |
| 86 | +MBDB::addDatabase("joomla", "localhost", "%b_joomla", "mb-%b", ""); |
| 87 | +MBDB::addDatabase("platinum", "localhost", "%b_platinum", "mb-%b", ""); |
| 88 | +MBDB::addDatabase("platinum_old", "localhost", "%b_platinum_old", "mb-%b", ""); |
| 89 | +MBDB::addDatabase("dedicated", "localhost", "%b_dedicated", "mb-%b", ""); |
| 90 | +MBDB::addDatabase("fubar", "localhost", "%b_fubar", "mb-%b", ""); |
0 commit comments