made the code more composer friendly
This commit is contained in:
parent
a14df54cd9
commit
466d34c39f
@ -6,8 +6,7 @@
|
|||||||
{
|
{
|
||||||
"name": "Nick Yeoman",
|
"name": "Nick Yeoman",
|
||||||
"email": "dev@4lt.ca",
|
"email": "dev@4lt.ca",
|
||||||
"homepage": "https://www.4lt.ca",
|
"homepage": "https://www.4lt.ca"
|
||||||
"role": "Consultant"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"autoload": {
|
"autoload": {
|
||||||
@ -16,6 +15,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
|
"php": "^8.1",
|
||||||
"twig/twig": "*",
|
"twig/twig": "*",
|
||||||
"nickyeoman/php-validation-class": "^5.0"
|
"nickyeoman/php-validation-class": "^5.0"
|
||||||
},
|
},
|
||||||
|
|||||||
@ -1,6 +1,31 @@
|
|||||||
<?php
|
<?php
|
||||||
|
/**
|
||||||
|
* Novaconium Framework Entry Point
|
||||||
|
*
|
||||||
|
* This is the main entry point for the Novaconium framework.
|
||||||
|
* It sets up the environment, loads necessary components,
|
||||||
|
* and runs the application.
|
||||||
|
*
|
||||||
|
* @package Novaconium
|
||||||
|
* @author Nick Yeoman <dev@4lt.ca>
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
// Enable error reporting for development environments
|
||||||
// error_reporting(E_ALL);
|
// error_reporting(E_ALL);
|
||||||
// ini_set('display_errors', 1);
|
// ini_set('display_errors', 1);
|
||||||
|
|
||||||
|
// Define the base path where the website is running from
|
||||||
define('BASEPATH', dirname(__DIR__, 1));
|
define('BASEPATH', dirname(__DIR__, 1));
|
||||||
require_once(BASEPATH . '/vendor/4lt/novaconium/src/novaconium.php');
|
|
||||||
?>
|
// 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();
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace Novaconium;
|
||||||
|
|
||||||
class Database {
|
class Database {
|
||||||
|
|
||||||
@ -6,7 +7,7 @@ class Database {
|
|||||||
public $lastid;
|
public $lastid;
|
||||||
|
|
||||||
public function __construct($dbinfo) {
|
public function __construct($dbinfo) {
|
||||||
$this->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) {
|
if ($this->conn->connect_error) {
|
||||||
die("Connection failed: " . $this->conn->connect_error);
|
die("Connection failed: " . $this->conn->connect_error);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace Novaconium;
|
||||||
class Logger {
|
class Logger {
|
||||||
protected string $logFile;
|
protected string $logFile;
|
||||||
protected int $logLevelThreshold;
|
protected int $logLevelThreshold;
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace Novaconium;
|
||||||
class MessageHandler {
|
class MessageHandler {
|
||||||
private $messages = [
|
private $messages = [
|
||||||
'error' => [],
|
'error' => [],
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace Novaconium;
|
||||||
class Post {
|
class Post {
|
||||||
private $data = [];
|
private $data = [];
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace Novaconium;
|
||||||
/**
|
/**
|
||||||
* Use
|
* Use
|
||||||
* $redirect->url('/login');
|
* $redirect->url('/login');
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace Novaconium;
|
||||||
class Router {
|
class Router {
|
||||||
public $routes = [];
|
public $routes = [];
|
||||||
public $query = [];
|
public $query = [];
|
||||||
@ -19,10 +19,10 @@ class Router {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function loadRoutes() {
|
private function loadRoutes() {
|
||||||
require_once(FRAMEWORKPATH . '/config/routes.php');
|
require_once( \FRAMEWORKPATH . '/config/routes.php');
|
||||||
// Check if Path exists
|
// Check if Path exists
|
||||||
if (file_exists(BASEPATH . '/App/routes.php')) {
|
if (file_exists(\BASEPATH . '/App/routes.php')) {
|
||||||
require_once( BASEPATH . '/App/routes.php');
|
require_once( \BASEPATH . '/App/routes.php');
|
||||||
}
|
}
|
||||||
$routes = array_merge((array)$routes, (array)$framework_routes);
|
$routes = array_merge((array)$routes, (array)$framework_routes);
|
||||||
|
|
||||||
@ -109,19 +109,19 @@ class Router {
|
|||||||
|
|
||||||
if (str_starts_with($this->controller, 'NOVACONIUM')) {
|
if (str_starts_with($this->controller, 'NOVACONIUM')) {
|
||||||
$trimmed = substr($this->controller, strlen('NOVACONIUM/'));
|
$trimmed = substr($this->controller, strlen('NOVACONIUM/'));
|
||||||
$cp = FRAMEWORKPATH . '/controllers/' . $trimmed . '.php';
|
$cp = \FRAMEWORKPATH . '/controllers/' . $trimmed . '.php';
|
||||||
} else {
|
} else {
|
||||||
$cp = BASEPATH . '/App/controllers/' . $this->controller . '.php';
|
$cp = \BASEPATH . '/App/controllers/' . $this->controller . '.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file_exists($cp)) {
|
if (file_exists($cp)) {
|
||||||
return $cp;
|
return $cp;
|
||||||
} else {
|
} else {
|
||||||
//Check if 404 exits
|
//Check if 404 exits
|
||||||
if (file_exists(BASEPATH . '/App/controllers/404.php')) {
|
if (file_exists( \BASEPATH . '/App/controllers/404.php')) {
|
||||||
return BASEPATH . '/App/controllers/404.php';
|
return \BASEPATH . '/App/controllers/404.php';
|
||||||
} else {
|
} else {
|
||||||
return FRAMEWORKPATH . '/controllers/404.php';
|
return \FRAMEWORKPATH . '/controllers/404.php';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
namespace Novaconium;
|
||||||
class Session {
|
class Session {
|
||||||
private $session;
|
private $session;
|
||||||
|
|
||||||
|
|||||||
@ -1,29 +1,60 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dump and Die
|
* Dump and Die (debug function)
|
||||||
|
*
|
||||||
|
* @param mixed ...$vars Any number of variables to dump and then exit the script.
|
||||||
*/
|
*/
|
||||||
function dd(...$vars) {
|
function dd(...$vars): void {
|
||||||
echo "<pre style='background:#222;color:#0f0;padding:10px;border-radius:5px;'>";
|
echo "<pre style='background:#1e1e1e;color:#00ff00;padding:12px;border-radius:6px;font-size:14px;'>";
|
||||||
foreach ($vars as $var) {
|
foreach ($vars as $var) {
|
||||||
var_dump($var);
|
var_dump($var);
|
||||||
echo "\n";
|
echo "\n";
|
||||||
}
|
}
|
||||||
echo "</pre>";
|
echo "</pre>";
|
||||||
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;
|
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());
|
$session->set('messages', $messages->getAllMessages());
|
||||||
|
|
||||||
|
// Write any buffered session data to persistent storage
|
||||||
$session->write();
|
$session->write();
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
// Perform redirect
|
||||||
|
// ------------------------------
|
||||||
|
|
||||||
|
// Execute the redirect operation
|
||||||
$redirect->execute();
|
$redirect->execute();
|
||||||
|
|
||||||
exit();
|
// Exit the script after processing is complete
|
||||||
}
|
exit;
|
||||||
|
}
|
||||||
@ -1,63 +1,50 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once(BASEPATH . '/vendor/autoload.php');
|
|
||||||
define('FRAMEWORKPATH', BASEPATH . '/vendor/4lt/novaconium');
|
|
||||||
|
|
||||||
//Check if config file exists
|
// --- Load Config ---
|
||||||
if (file_exists(BASEPATH . '/App/config.php')) {
|
if (file_exists(\BASEPATH . '/App/config.php')) {
|
||||||
require_once(BASEPATH . '/App/config.php');
|
require_once \BASEPATH . '/App/config.php';
|
||||||
} else {
|
} else {
|
||||||
require_once(FRAMEWORKPATH . '/skeleton/novaconium/App/config.php');
|
require_once \FRAMEWORKPATH . '/skeleton/novaconium/App/config.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logging
|
// --- Logging ---
|
||||||
require_once(FRAMEWORKPATH . '/src/Logger.php');
|
use Novaconium\Logger;
|
||||||
$log = new Logger(BASEPATH . $config['logfile'], $config['loglevel']);
|
$log = new Logger(\BASEPATH . $config['logfile'], $config['loglevel']);
|
||||||
|
|
||||||
// Global Functions
|
// --- Twig Data Array ---
|
||||||
require_once(FRAMEWORKPATH . '/src/functions.php');
|
$data = [];
|
||||||
|
$data['fonts'] = $config['fonts'] ?? [];
|
||||||
|
|
||||||
// Creates the view() function using twig
|
// --- Session ---
|
||||||
$data = array();
|
use Novaconium\Session;
|
||||||
require_once(FRAMEWORKPATH . '/src/twig.php');
|
|
||||||
$data['fonts'] = $config['fonts'];
|
|
||||||
|
|
||||||
// Start a Session
|
|
||||||
require_once(FRAMEWORKPATH . '/src/Session.php');
|
|
||||||
$session = new Session();
|
$session = new Session();
|
||||||
$data['token'] = $session->get('token');
|
$data['token'] = $session->get('token');
|
||||||
$data['username'] = $session->get('username');
|
$data['username'] = $session->get('username');
|
||||||
if ($config['loglevel'] == 'DEBUG') {
|
|
||||||
$data['debug'] = nl2br(print_r($session->debug(), true));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Messages
|
// --- Messages ---
|
||||||
require_once(FRAMEWORKPATH . '/src/MessageHandler.php');
|
use Novaconium\MessageHandler;
|
||||||
$messages = new MessageHandler($session->flash('messages'));
|
$messages = new MessageHandler($session->flash('messages'));
|
||||||
|
foreach (['error', 'notice'] as $key) {
|
||||||
foreach (['error','notice'] as $key){
|
$data[$key] = $messages->showMessages($key);
|
||||||
$data[$key] = $messages->showMessages($key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load Database Class
|
// --- Database ---
|
||||||
|
use Novaconium\Database;
|
||||||
if (!empty($config['database']['host'])) {
|
if (!empty($config['database']['host'])) {
|
||||||
require_once(FRAMEWORKPATH . '/src/Database.php');
|
|
||||||
$db = new Database($config['database']);
|
$db = new Database($config['database']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sanatize POST Data
|
// --- POST Wrapper ---
|
||||||
|
use Novaconium\Post;
|
||||||
if (!empty($_POST)) {
|
if (!empty($_POST)) {
|
||||||
require_once(FRAMEWORKPATH . '/src/Post.php');
|
|
||||||
$post = new Post($_POST);
|
$post = new Post($_POST);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start a Redirect
|
// --- Redirect Handler ---
|
||||||
require_once(FRAMEWORKPATH . '/src/Redirect.php');
|
use Novaconium\Redirect;
|
||||||
$redirect = new Redirect();
|
$redirect = new Redirect();
|
||||||
|
|
||||||
// Load a controller
|
// --- Router ---
|
||||||
require_once(FRAMEWORKPATH . '/src/Router.php');
|
use Novaconium\Router;
|
||||||
$router = new Router();
|
$router = new Router();
|
||||||
//$router->debug();
|
require_once \BASEPATH . $router->controllerPath;
|
||||||
require_once($router->controllerPath);
|
|
||||||
|
|
||||||
makeitso();
|
|
||||||
|
|||||||
71
src/twig.php
71
src/twig.php
@ -1,32 +1,67 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
//Twig
|
declare(strict_types=1);
|
||||||
function view($name = '', $moreData = []) {
|
|
||||||
global $config, $data; // Use the globally included $config
|
|
||||||
|
|
||||||
if (!empty($moreData)){
|
use Twig\Environment;
|
||||||
|
use Twig\Loader\FilesystemLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render a Twig view.
|
||||||
|
*
|
||||||
|
* @param string $name Template name without extension (e.g. "index")
|
||||||
|
* @param array $moreData Additional variables to merge into template context
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
function view(string $name = '', array $moreData = []): bool
|
||||||
|
{
|
||||||
|
global $config, $data;
|
||||||
|
|
||||||
|
// Ensure $data is always an array
|
||||||
|
if (!is_array($data)) {
|
||||||
|
$data = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge additional data
|
||||||
|
if (!empty($moreData)) {
|
||||||
$data = array_merge($data, $moreData);
|
$data = array_merge($data, $moreData);
|
||||||
}
|
}
|
||||||
|
|
||||||
$loader = new Twig\Loader\FilesystemLoader(BASEPATH . '/App/views/');
|
// ----------------------------------------
|
||||||
$loader->addPath(FRAMEWORKPATH . '/twig', 'novaconium');
|
// Setup Twig
|
||||||
$loader->addPath(FRAMEWORKPATH . '/views', 'novacore');
|
// ----------------------------------------
|
||||||
$loader->addPath(BASEPATH . '/App/templates', 'override');
|
|
||||||
|
|
||||||
$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);
|
$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);
|
echo $twig->render($name . '.html.twig', $data);
|
||||||
return true;
|
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;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user