From 937e4581bad22e14a51697f9a256ecb504363932 Mon Sep 17 00:00:00 2001 From: Nick Yeoman Date: Thu, 12 Sep 2024 16:28:33 -0700 Subject: [PATCH] added session, moved twig, updated base --- src/Session.php | 51 ++++++++++++++++++++++++++++++++++++++++++++++ src/novaconium.php | 24 ++++++++-------------- src/twig.php | 17 ++++++++++++++++ 3 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 src/Session.php create mode 100644 src/twig.php diff --git a/src/Session.php b/src/Session.php new file mode 100644 index 0000000..4df3504 --- /dev/null +++ b/src/Session.php @@ -0,0 +1,51 @@ +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(); + } + +} \ No newline at end of file diff --git a/src/novaconium.php b/src/novaconium.php index 85f87dd..f47cdf5 100644 --- a/src/novaconium.php +++ b/src/novaconium.php @@ -2,21 +2,12 @@ require_once(BASEPATH . '/vendor/autoload.php'); require_once(BASEPATH . '/App/config.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; - } -} +// Creates twig and the view() function +require_once(BASEPATH . '/vendor/4lt/novaconium/src/twig.php'); + +// Start a Session +require_once(BASEPATH . '/vendor/4lt/novaconium/src/Session.php'); +$session = new Session(); // Load Database Class 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->debug(); require_once($router->controllerPath); + +//write the session +$session->write(); \ No newline at end of file diff --git a/src/twig.php b/src/twig.php new file mode 100644 index 0000000..f0225b0 --- /dev/null +++ b/src/twig.php @@ -0,0 +1,17 @@ +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; + } +} \ No newline at end of file