Skip to content

Commit

Permalink
Add dependency injection to MySql{Database,Properties} (#924)
Browse files Browse the repository at this point in the history
Closes #922.
  • Loading branch information
XaeroDegreaz authored Dec 13, 2020
1 parent 613786e commit fc4bd93
Show file tree
Hide file tree
Showing 99 changed files with 982 additions and 639 deletions.
2 changes: 1 addition & 1 deletion admin/Default/1.6/game_create_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
forward($container);

function createNHA($gameID) {
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();

// create the Newbie Help Alliance
$db->query('REPLACE INTO alliance (alliance_id, game_id, alliance_name, alliance_description, alliance_password, leader_id, `mod`, recruiting) VALUES
Expand Down
2 changes: 1 addition & 1 deletion admin/Default/changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
$template->assign('ChangeMessage', $var['change_message'] ?? '');
$template->assign('AffectedDb', $var['affected_db'] ?? '');

$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();
$first_entry = true;
$link_set_live = true;

Expand Down
2 changes: 1 addition & 1 deletion admin/Default/comp_share.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$skipExceptions = false;

//extra db object and other vars
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();
$used = array();

//check the db and get the info we need
Expand Down
6 changes: 3 additions & 3 deletions admin/Default/game_delete_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
create_error('Deleting games is disabled!');

// additional db objects
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();

$smr_db_sql = array();
$history_db_sql = array();
Expand Down Expand Up @@ -291,9 +291,9 @@

// don't know why exactly we have to do that,
// but it seems that the db is used globally instead kept to each object
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();

}
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();
//forward em
forward(create_container('skeleton.php', 'admin_tools.php'));
2 changes: 1 addition & 1 deletion admin/Default/ip_view_results.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$variable = SmrSession::getRequestVar('variable');
$type = SmrSession::getRequestVar('type');

$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();

$container = create_container('skeleton.php', 'ip_view.php');
$template->assign('BackHREF', SmrSession::getNewHREF($container));
Expand Down
2 changes: 1 addition & 1 deletion admin/Default/log_console.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
JOIN account USING(account_id)
GROUP BY account_id');
if ($db->getNumRows()) {
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();
while ($db->nextRecord()) {
$accountID = $db->getInt('account_id');
$loggedAccounts[$accountID] = array('AccountID' => $accountID,
Expand Down
2 changes: 1 addition & 1 deletion admin/Default/newsletter_send.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
$processingContainer = create_container('newsletter_send_processing.php');

// Get the most recent newsletter text for preview
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();
$db->query('SELECT newsletter_id, newsletter_html, newsletter_text FROM newsletter ORDER BY newsletter_id DESC LIMIT 1');
if ($db->nextRecord()) {
$id = $db->getInt('newsletter_id');
Expand Down
15 changes: 13 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@
"php": "^7.4",
"phpmailer/phpmailer": "^6.1",
"vanilla/nbbc": "^2.0",
"vlucas/phpdotenv": "^v5.2.0"
"vlucas/phpdotenv": "^v5.2.0",
"php-di/php-di": "6.3.0"
},
"autoload": {
"psr-0": {
"Smr\\": "lib/"
}
},
"scripts": {
"start:dev": "docker-compose up --build -d traefik smr-dev",
"start:integration-services": [
"docker-compose --env-file test.env up -d mysql-integration-test",
"sleep 20",
Expand All @@ -24,7 +31,11 @@
"docker-compose stop",
"docker-compose rm --force"
],
"test": "docker-compose --env-file test.env run --rm smr-integration-test"
"test": "docker-compose --env-file test.env run --rm smr-integration-test",
"rebuild:dev": [
"@stop",
"@start:dev"
]
},
"autoload-dev": {
"psr-0": {
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ services:
- ./htdocs:/smr/htdocs
- ./lib:/smr/lib
- ./templates:/smr/templates
environment:
DISABLE_PHPDI_COMPILATION: "true"

smtp:
image: hemberger/postfix-relay
Expand Down
8 changes: 4 additions & 4 deletions engine/Default/alliance_message.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
$db->query($query);
$threads = array();
if ($db->getNumRows() > 0) {
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();

$container = create_container('alliance_message_delete_processing.php');
$container['alliance_id'] = $alliance->getAllianceID();
Expand All @@ -52,15 +52,15 @@
$thread_topics[$i] = $db->getField('topic');

$threads[$i]['Topic'] = $db->getField('topic');

$db2->query('SELECT time
FROM player_read_thread
FROM player_read_thread
WHERE ' . $player->getSQL() . '
AND alliance_id =' . $db2->escapeNumber($alliance->getAllianceID()) . '
AND thread_id=' . $db2->escapeNumber($threadID) . '
AND time>' . $db2->escapeNumber($db->getInt('sendtime')) . ' LIMIT 1');
$threads[$i]['Unread'] = $db2->getNumRows() == 0;

// Determine the thread author display name
$sender_id = $db->getInt('sender_id');
if ($sender_id == ACCOUNT_ID_PLANET) {
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/alliance_mod.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
$template->assign('OpTime', $db->getInt('time'));

// Has player responded yet?
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();
$db2->query('SELECT response FROM alliance_has_op_response WHERE alliance_id=' . $db2->escapeNumber($player->getAllianceID()) . ' AND ' . $player->getSQL() . ' LIMIT 1');

$response = $db2->nextRecord() ? $db2->getField('response') : null;
Expand Down
4 changes: 2 additions & 2 deletions engine/Default/bank_anon.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
AND game_id=' . $db->escapeNumber($player->getGameID()));

$ownedAnon = [];
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();
while ($db->nextRecord()) {
$anon = [];
$anon['anon_id'] = $db->getInt('anon_id');
Expand All @@ -33,7 +33,7 @@
} else {
$anon['last_transaction'] = 'No transactions';
}

$container['account_num'] = $anon['anon_id'];
$container['password'] = $anon['password'];
$anon['href'] = SmrSession::getNewHREF($container);
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/changelog_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
$template->assign('ContinueHREF', SmrSession::getNewHREF($container));
}

$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();

$db->query('SELECT *
FROM version
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/council_vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
AND race_id_1 = ' . $db->escapeNumber($player->getRaceID()));
if ($db->getNumRows() > 0) {

$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();

while ($db->nextRecord()) {
$otherRaceID = $db->getInt('race_id_2');
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/current_players.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

$allRows = array();
if ($count_last_active > 0) {
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();
while ($db->nextRecord()) {
$row = array();

Expand Down
2 changes: 1 addition & 1 deletion engine/Default/feature_request.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

$commentsContainer = $var;
$commentsContainer['body'] = 'feature_request_comments.php';
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();
$featureRequests = array();
while ($db->nextRecord()) {
$featureRequestID = $db->getInt('feature_request_id');
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/galactic_post.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
$template->assign('PageTopic', 'Galactic Post');
Menu::galactic_post();

$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();

$container = create_container('skeleton.php', 'galactic_post_view_article.php');
$template->assign('ViewArticlesHREF', SmrSession::getNewHREF($container));
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/galactic_post_delete_processing.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php declare(strict_types=1);
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();
if (isset($var['article'])) {
if (Request::get('action') == 'Yes') {
$db->query('DELETE FROM galactic_post_article WHERE game_id = ' . $db->escapeNumber($player->getGameID()) . ' AND article_id = ' . $db->escapeNumber($var['id']));
Expand Down
4 changes: 2 additions & 2 deletions engine/Default/game_play.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
// generate list of game_id that this player is joined
$game_id_list[] = $game_id;

$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();
$db2->query('SELECT count(*) as num_playing
FROM player
WHERE last_cpl_action >= ' . $db->escapeNumber(SmrSession::getTime() - 600) . '
Expand Down Expand Up @@ -191,7 +191,7 @@

$db->query('SELECT * FROM voting WHERE end > ' . $db->escapeNumber(SmrSession::getTime()) . ' ORDER BY end DESC');
if ($db->getNumRows() > 0) {
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();
$votedFor = array();
$db2->query('SELECT * FROM voting_results WHERE account_id = ' . $db->escapeNumber($account->getAccountID()));
while ($db2->nextRecord()) {
Expand Down
4 changes: 2 additions & 2 deletions engine/Default/game_stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
$template->assign('LowestAlign', $db->getInt('min_alignment'));
$template->assign('HighestKills', $db->getInt('max_kills'));
}

$db->query('SELECT count(*) num_alliance FROM alliance WHERE game_id = ' . $gameID);
if ($db->nextRecord()) {
$template->assign('TotalAlliances', $db->getInt('num_alliance'));
Expand Down Expand Up @@ -44,7 +44,7 @@
}

function allianceTopTen($gameID, $field) {
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getNewInstance();
$db->query('SELECT alliance_id, SUM(' . $field . ') amount
FROM alliance
LEFT JOIN player USING (game_id, alliance_id)
Expand Down
14 changes: 7 additions & 7 deletions engine/Default/help.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

function echo_nav($topic_id) {
// database object
$db = new SmrMySqlDatabase();
$db2 = new SmrMySqlDatabase();
$db3 = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();
$db2 = MySqlDatabase::getNewInstance();
$db3 = MySqlDatabase::getNewInstance();

// get current entry
$db->query('SELECT * FROM manual WHERE topic_id = '.$db->escapeNumber($topic_id));
Expand Down Expand Up @@ -100,7 +100,7 @@ function echo_nav($topic_id) {

function echo_content($topic_id) {
// database object
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();

// get current entry
$db->query('SELECT * FROM manual WHERE topic_id = '.$topic_id);
Expand All @@ -120,7 +120,7 @@ function echo_content($topic_id) {

function echo_subsection($topic_id) {
// database object
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();
$return='';
// check if there are subsections
$db->query('SELECT * FROM manual WHERE parent_topic_id = '.$db->escapeNumber($topic_id).' ORDER BY order_id');
Expand All @@ -139,7 +139,7 @@ function echo_subsection($topic_id) {
function echo_menu($topic_id) {
$return='';
// database object
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();

$db->query('SELECT * FROM manual WHERE parent_topic_id = '.$db->escapeNumber($topic_id).' ORDER BY order_id');
if ($db->getNumRows()) {
Expand All @@ -158,7 +158,7 @@ function echo_menu($topic_id) {
}

function get_numbering($topic_id) {
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();

$db->query('SELECT * FROM manual WHERE topic_id = '.$db->escapeNumber($topic_id));
if ($db->nextRecord()) {
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/login_check_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

$lastLogin = $account->getLastLogin();

$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();
if ($var['CheckType'] == 'Announcements') {
$db->query('SELECT 1 FROM announcement WHERE time >= ' . $db->escapeNumber($lastLogin) . ' LIMIT 1');
// do we have announcements?
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/message_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@

function displayScouts(&$messageBox, $player) {
// Generate the group messages
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();
$db->query('SELECT player.*, count( message_id ) AS number, min( send_time ) as first, max( send_time) as last, sum(msg_read=\'FALSE\') as total_unread
FROM message
JOIN player ON player.account_id = message.sender_id AND message.game_id = player.game_id
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/rankings_alliance_vs_alliance.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
$template->assign('PageTopic', 'Alliance VS Alliance Rankings');

Menu::rankings(1, 4);
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();
$container = create_container('skeleton.php', 'rankings_alliance_vs_alliance.php');
$template->assign('SubmitHREF', SmrSession::getNewHREF($container));

Expand Down
2 changes: 1 addition & 1 deletion engine/Default/vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

$db->query('SELECT * FROM voting ORDER BY end DESC');
if ($db->getNumRows() > 0) {
$db2 = new SmrMySqlDatabase();
$db2 = MySqlDatabase::getNewInstance();
$votedFor = array();
$db2->query('SELECT * FROM voting_results WHERE account_id = ' . $db2->escapeNumber($account->getAccountID()));
while ($db2->nextRecord()) {
Expand Down
2 changes: 1 addition & 1 deletion engine/Draft/alliance_pick.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Returns an array with all relevant information about draft teams,
// including their current size and if the leader can pick teammates.
function get_draft_teams($gameId) {
$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();
$db->query('SELECT account_id FROM draft_leaders WHERE game_id=' . $db->escapeNumber($gameId));

// Get team leader, alliance, and alliance size
Expand Down
2 changes: 1 addition & 1 deletion htdocs/album/album_comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function create_error_offline($msg) {
exit;
}

$db = new SmrMySqlDatabase();
$db = MySqlDatabase::getInstance();

if (!isset($_GET['comment']) || empty($_GET['comment'])) {
create_error_offline('Please enter a comment.');
Expand Down
Loading

0 comments on commit fc4bd93

Please sign in to comment.