From 466d34c39fb49009433422bc0f103f2124aabbe1 Mon Sep 17 00:00:00 2001 From: Nick Yeoman Date: Wed, 19 Nov 2025 00:03:03 -0800 Subject: [PATCH] made the code more composer friendly --- composer.json | 4 +- skeleton/novaconium/public/index.php | 29 +++++++++++- src/Database.php | 3 +- src/Logger.php | 2 +- src/MessageHandler.php | 2 +- src/Post.php | 2 +- src/Redirect.php | 1 + src/Router.php | 18 +++---- src/Session.php | 2 +- src/functions.php | 49 +++++++++++++++---- src/novaconium.php | 65 ++++++++++--------------- src/twig.php | 71 +++++++++++++++++++++------- 12 files changed, 164 insertions(+), 84 deletions(-) diff --git a/composer.json b/composer.json index 5ba9f25..3325978 100644 --- a/composer.json +++ b/composer.json @@ -6,8 +6,7 @@ { "name": "Nick Yeoman", "email": "dev@4lt.ca", - "homepage": "https://www.4lt.ca", - "role": "Consultant" + "homepage": "https://www.4lt.ca" } ], "autoload": { @@ -16,6 +15,7 @@ } }, "require": { + "php": "^8.1", "twig/twig": "*", "nickyeoman/php-validation-class": "^5.0" }, diff --git a/skeleton/novaconium/public/index.php b/skeleton/novaconium/public/index.php index ece7160..b764fe1 100644 --- a/skeleton/novaconium/public/index.php +++ b/skeleton/novaconium/public/index.php @@ -1,6 +1,31 @@ + */ + + +// Enable error reporting for development environments // error_reporting(E_ALL); // ini_set('display_errors', 1); + +// Define the base path where the website is running from define('BASEPATH', dirname(__DIR__, 1)); -require_once(BASEPATH . '/vendor/4lt/novaconium/src/novaconium.php'); -?> \ No newline at end of file + +// Load Composer's autoload file to handle class autoloading +require BASEPATH . '/vendor/autoload.php'; + +// Define the framework path +define('FRAMEWORKPATH', BASEPATH . '/vendor/4lt/novaconium'); + +// Bootstrap the Novaconium framework, which will create necessary objects like $session, $db, etc. +require FRAMEWORKPATH . '/src/novaconium.php'; + +// Run the application +makeitso(); diff --git a/src/Database.php b/src/Database.php index 3ee849e..7689080 100644 --- a/src/Database.php +++ b/src/Database.php @@ -1,4 +1,5 @@ conn = new mysqli($dbinfo['host'], $dbinfo['user'], $dbinfo['pass'], $dbinfo['name']); + $this->conn = new \mysqli($dbinfo['host'], $dbinfo['user'], $dbinfo['pass'], $dbinfo['name']); if ($this->conn->connect_error) { die("Connection failed: " . $this->conn->connect_error); } diff --git a/src/Logger.php b/src/Logger.php index c7f1635..01d4e4b 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -1,5 +1,5 @@ [], diff --git a/src/Post.php b/src/Post.php index 167448b..daf3296 100644 --- a/src/Post.php +++ b/src/Post.php @@ -1,5 +1,5 @@ url('/login'); diff --git a/src/Router.php b/src/Router.php index 48cae2a..48fb692 100644 --- a/src/Router.php +++ b/src/Router.php @@ -1,5 +1,5 @@ controller, 'NOVACONIUM')) { $trimmed = substr($this->controller, strlen('NOVACONIUM/')); - $cp = FRAMEWORKPATH . '/controllers/' . $trimmed . '.php'; + $cp = \FRAMEWORKPATH . '/controllers/' . $trimmed . '.php'; } else { - $cp = BASEPATH . '/App/controllers/' . $this->controller . '.php'; + $cp = \BASEPATH . '/App/controllers/' . $this->controller . '.php'; } if (file_exists($cp)) { return $cp; } else { //Check if 404 exits - if (file_exists(BASEPATH . '/App/controllers/404.php')) { - return BASEPATH . '/App/controllers/404.php'; + if (file_exists( \BASEPATH . '/App/controllers/404.php')) { + return \BASEPATH . '/App/controllers/404.php'; } else { - return FRAMEWORKPATH . '/controllers/404.php'; + return \FRAMEWORKPATH . '/controllers/404.php'; } } } diff --git a/src/Session.php b/src/Session.php index dfa9d7e..d3f574f 100644 --- a/src/Session.php +++ b/src/Session.php @@ -1,5 +1,5 @@ "; +function dd(...$vars): void { + echo "
";
     foreach ($vars as $var) {
         var_dump($var);
         echo "\n";
     }
     echo "
"; - die(); + exit; } -function makeitso() { +/** + * Finalize the request lifecycle. + * + * This function safely writes session data, stores flash messages, closes the database + * connection if configured, and performs a redirect. + */ +function makeitso(): void { global $session, $db, $redirect, $config, $messages, $log; - if (!empty($config['database']['host'])) { - $db->close(); + // ------------------------------ + // Close database if configured + // ------------------------------ + + // Check if database configuration exists and the $db object is set + if (!empty($config['database']['host']) && isset($db)) { + // If the $db object has a close method, call it to close the connection + if (method_exists($db, 'close')) { + $db->close(); + } } + // ------------------------------ + // Save flash messages to session + // ------------------------------ + + // Set all messages in the session $session->set('messages', $messages->getAllMessages()); + + // Write any buffered session data to persistent storage $session->write(); + // ------------------------------ + // Perform redirect + // ------------------------------ + + // Execute the redirect operation $redirect->execute(); - exit(); -} + // Exit the script after processing is complete + exit; +} \ No newline at end of file diff --git a/src/novaconium.php b/src/novaconium.php index 71ac5c9..f60861d 100644 --- a/src/novaconium.php +++ b/src/novaconium.php @@ -1,63 +1,50 @@ get('token'); +$data['token'] = $session->get('token'); $data['username'] = $session->get('username'); -if ($config['loglevel'] == 'DEBUG') { - $data['debug'] = nl2br(print_r($session->debug(), true)); -} -// Messages -require_once(FRAMEWORKPATH . '/src/MessageHandler.php'); +// --- Messages --- +use Novaconium\MessageHandler; $messages = new MessageHandler($session->flash('messages')); - -foreach (['error','notice'] as $key){ - $data[$key] = $messages->showMessages($key); +foreach (['error', 'notice'] as $key) { + $data[$key] = $messages->showMessages($key); } -// Load Database Class +// --- Database --- +use Novaconium\Database; if (!empty($config['database']['host'])) { - require_once(FRAMEWORKPATH . '/src/Database.php'); $db = new Database($config['database']); } -// Sanatize POST Data +// --- POST Wrapper --- +use Novaconium\Post; if (!empty($_POST)) { - require_once(FRAMEWORKPATH . '/src/Post.php'); $post = new Post($_POST); } -// Start a Redirect -require_once(FRAMEWORKPATH . '/src/Redirect.php'); +// --- Redirect Handler --- +use Novaconium\Redirect; $redirect = new Redirect(); -// Load a controller -require_once(FRAMEWORKPATH . '/src/Router.php'); +// --- Router --- +use Novaconium\Router; $router = new Router(); -//$router->debug(); -require_once($router->controllerPath); - -makeitso(); +require_once \BASEPATH . $router->controllerPath; diff --git a/src/twig.php b/src/twig.php index 62dd4f8..8b354ce 100644 --- a/src/twig.php +++ b/src/twig.php @@ -1,32 +1,67 @@ addPath(FRAMEWORKPATH . '/twig', 'novaconium'); - $loader->addPath(FRAMEWORKPATH . '/views', 'novacore'); - $loader->addPath(BASEPATH . '/App/templates', 'override'); + // ---------------------------------------- + // Setup Twig + // ---------------------------------------- - $twig = new Twig\Environment($loader); + $loader = new FilesystemLoader([ + BASEPATH . '/App/views/', + FRAMEWORKPATH . '/twig', + FRAMEWORKPATH . '/views', + BASEPATH . '/App/templates/override' + ]); - // Add config to Twig globally + $twig = new Environment($loader); + + // Add global config $twig->addGlobal('config', $config); - // Check if the template exists - if (file_exists(BASEPATH . '/App/views/' . $name . '.html.twig')) { + // ---------------------------------------- + // Render template + // ---------------------------------------- + + // Direct App override template + $appTemplatePath = BASEPATH . '/App/views/' . $name . '.html.twig'; + + if (file_exists($appTemplatePath)) { echo $twig->render($name . '.html.twig', $data); return true; - } elseif (str_starts_with($name, '@')) { // Check if using framework - echo $twig->render($name . '.html.twig', $data); - return true; - } else { - echo "Error: Twig Template ($name) Not Found."; - return false; } + + // Namespaced Twig templates like @novaconium/whatever + if (str_starts_with($name, '@')) { + echo $twig->render($name . '.html.twig', $data); + return true; + } + + // Fallback + echo "Error: Twig Template ($name) Not Found."; + return false; } \ No newline at end of file