Skip to content
This repository was archived by the owner on Jan 26, 2023. It is now read-only.

Commit 80c91fb

Browse files
committed
Basic SQL connections
1 parent d4befec commit 80c91fb

File tree

4 files changed

+133
-4
lines changed

4 files changed

+133
-4
lines changed

bin/chat-server.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22
use Ratchet\Server\IoServer;
33
use Ratchet\Http\HttpServer;
44
use Ratchet\WebSocket\WsServer;
5-
use LBChat\ChatServer;
5+
use LBChat\Database\Database;
6+
use LBChat\Database\SQLChatServer;
7+
use LBChat\Command\CommandFactory;
68

79
require dirname(__DIR__) . "/vendor/autoload.php";
810

9-
\LBChat\Command\CommandFactory::init();
11+
CommandFactory::init();
12+
13+
$database = new Database();
14+
$database->connect();
1015

1116
$server = IoServer::factory(
1217
new HttpServer(
1318
new WsServer(
14-
new ChatServer()
19+
new SQLChatServer($database)
1520
)
1621
),
17-
28002
22+
39002
1823
);
1924

2025
$server->run();

db.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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", "");

src/LBChat/Database/Database.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace LBChat\Database;
3+
4+
class Database {
5+
protected $connection;
6+
7+
public function connect() {
8+
//Load the db config
9+
require("../db.php");
10+
11+
try {
12+
$dsn = "mysql:dbname=" . \MBDB::getDatabaseName("platinum") . ";host=" . \MBDB::getDatabaseHost("platinum");
13+
$this->connection = new \PDO($dsn, \MBDB::getDatabaseUser("platinum"), \MBDB::getDatabasePass("platinum"));
14+
} catch (\Exception $e) {
15+
//Something
16+
throw $e;
17+
}
18+
}
19+
}

src/LBChat/Database/SQLChatServer.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace LBChat\Database;
3+
4+
use LBChat\ChatServer;
5+
6+
class SQLChatServer extends ChatServer {
7+
protected $database;
8+
9+
public function __construct($database) {
10+
parent::__construct();
11+
12+
$this->database = $database;
13+
}
14+
15+
}

0 commit comments

Comments
 (0)