From cbecf6d7344bb6e2db3ff67dd823c8c9d5916484 Mon Sep 17 00:00:00 2001 From: William Date: Fri, 3 Feb 2023 13:57:08 +0100 Subject: [PATCH] AAAHHH IMMM.. IIIIM COOOMMMIIIIIIITIING!!!!! --- app/App/Controller/ChatController.php | 51 +++++++-------------------- app/App/Model/ChatModel.php | 50 ++++++++++++++++++++++++++ routes/start.php | 7 ++++ 3 files changed, 70 insertions(+), 38 deletions(-) create mode 100644 app/App/Model/ChatModel.php diff --git a/app/App/Controller/ChatController.php b/app/App/Controller/ChatController.php index 9881982..6097ffb 100644 --- a/app/App/Controller/ChatController.php +++ b/app/App/Controller/ChatController.php @@ -2,28 +2,10 @@ namespace App\Controller; -use App\Config; - -ChatController::init(); +use App\Model\ChatModel; abstract class ChatController { - private static array $messages; - - static function init() - { - if (!file_exists(Config::get('path_to_chat_json_file'))) { - file_put_contents( - Config::get('path_to_chat_json_file'), - json_encode([]) - ); - } - self::$messages = json_decode( - file_get_contents(Config::get('path_to_chat_json_file')), - true - ); - } - static function index() { if (empty($_POST)) { @@ -36,7 +18,7 @@ abstract class ChatController $nick = filter_input(INPUT_POST, 'nick'); $text = filter_input(INPUT_POST, 'text'); - $errmsg = (function() use (&$text, &$nick): string|false { + $errmsg = (function() use (&$text, &$nick): string { if (empty(trim($nick, ' '))) { return 'You must choose a nickname.'; } @@ -55,26 +37,16 @@ abstract class ChatController return 'Message body cannot be empty.'; } - return false; + return ''; })(); - if (!$errmsg) { - if (count(self::$messages) > 100) { - array_pop(self::$messages); - } - - array_unshift(self::$messages, [ - 'nick' => $nick, - 'date' => time(), - 'text' => $text - ]); - - file_put_contents(Config::get('path_to_chat_json_file'), - json_encode( - self::$messages - ) + if (empty($errmsg)) { + ChatModel::save_message( + $nick, + $text ); } + view('pages/chat/index', [ 'nick' => $nick, 'just_sent_message' => true, @@ -84,11 +56,14 @@ abstract class ChatController static function messages() { - view('pages/chat/messages', ['messages' => self::$messages]); + view('pages/chat/messages', + ['messages' => ChatModel::get_messages()]); } static function sync() { - json_response(hash('crc32', serialize(self::$messages))); + json_response(hash('crc32', serialize( + ChatModel::get_messages() + ))); } } \ No newline at end of file diff --git a/app/App/Model/ChatModel.php b/app/App/Model/ChatModel.php new file mode 100644 index 0000000..3a13f00 --- /dev/null +++ b/app/App/Model/ChatModel.php @@ -0,0 +1,50 @@ + 100) { + array_pop($messages); + } + + array_unshift(self::$messages, [ + 'nick' => $nick, + 'date' => time(), + 'text' => $text + ]); + + file_put_contents(Config::get('path_to_chat_json_file'), + json_encode( + self::$messages + ) + ); + } +} \ No newline at end of file diff --git a/routes/start.php b/routes/start.php index 2ea0e91..519cf78 100644 --- a/routes/start.php +++ b/routes/start.php @@ -35,5 +35,12 @@ App::group('/chat', function() { ChatController::sync(...)); }); +App::group('/blog', function() { + App::get('/', + fn() => view('pages/blog/home')); + App::get('/test', + fn() => view('pages/blog/test')); +}); + http_response_code(404); view('errors/404'); \ No newline at end of file