added session, moved twig, updated base

This commit is contained in:
Nick Yeoman 2024-09-12 16:28:33 -07:00
parent f19f59d53a
commit 937e4581ba
3 changed files with 77 additions and 15 deletions

51
src/Session.php Normal file
View File

@ -0,0 +1,51 @@
<?php
class Session {
private $session;
public function __construct() {
if (!isset($_SESSION)) {
session_start();
$this->session = $_SESSION;
} else {
$this->session = $_SESSION;
}
$this->setToken();
}
public function setToken() {
if (!isset($this->session['token'])) {
$this->session['token'] = bin2hex(random_bytes(32));
}
}
public function set($key, $value) {
$this->session[$key] = $value;
}
public function get($key) {
return isset($this->session[$key]) ? $this->session[$key] : null;
}
public function flash($key) {
$return = $this->get($key);
$this->delete($key);
return $return;
}
public function debug() {
print_r($this->session);
}
public function delete($key) {
if (isset($this->session[$key])) {
unset($this->session[$key]);
}
}
public function write() {
$_SESSION = $this->session;
session_write_close();
}
}

View File

@ -2,21 +2,12 @@
require_once(BASEPATH . '/vendor/autoload.php'); require_once(BASEPATH . '/vendor/autoload.php');
require_once(BASEPATH . '/App/config.php'); require_once(BASEPATH . '/App/config.php');
//Twig // Creates twig and the view() function
function view($name = '', $data = [] ) { require_once(BASEPATH . '/vendor/4lt/novaconium/src/twig.php');
$loader = new Twig\Loader\FilesystemLoader(BASEPATH . '/App/views/');
$loader->addPath(BASEPATH . '/vendor/4lt/novaconium/twig', 'novaconium'); // Start a Session
$loader->addPath(BASEPATH . '/App/templates', 'override'); require_once(BASEPATH . '/vendor/4lt/novaconium/src/Session.php');
$twig = new Twig\Environment($loader); $session = new Session();
//check if file exists
if (file_exists(BASEPATH . '/App/views/' . $name . '.html.twig')) {
echo $twig->render("$name" . '.html.twig', $data);
return true;
} else {
echo "Error: Twig Template ($name) Not Found.";
return false;
}
}
// Load Database Class // Load Database Class
require_once(BASEPATH . '/vendor/4lt/novaconium/src/Database.php'); require_once(BASEPATH . '/vendor/4lt/novaconium/src/Database.php');
@ -27,3 +18,6 @@ require_once(BASEPATH . '/vendor/4lt/novaconium/src/Router.php');
$router = new Router(); $router = new Router();
//$router->debug(); //$router->debug();
require_once($router->controllerPath); require_once($router->controllerPath);
//write the session
$session->write();

17
src/twig.php Normal file
View File

@ -0,0 +1,17 @@
<?php
//Twig
function view($name = '', $data = [] ) {
$loader = new Twig\Loader\FilesystemLoader(BASEPATH . '/App/views/');
$loader->addPath(BASEPATH . '/vendor/4lt/novaconium/twig', 'novaconium');
$loader->addPath(BASEPATH . '/App/templates', 'override');
$twig = new Twig\Environment($loader);
//check if file exists
if (file_exists(BASEPATH . '/App/views/' . $name . '.html.twig')) {
echo $twig->render("$name" . '.html.twig', $data);
return true;
} else {
echo "Error: Twig Template ($name) Not Found.";
return false;
}
}