Skip to content

Commit

Permalink
Refactoring: Global context fix + added Renderer class
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchikikomori committed Oct 16, 2023
1 parent 8cf8b76 commit 95d1cd1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 25 deletions.
30 changes: 13 additions & 17 deletions classes/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PDO; // from PHP
use libraries\Session as Session;
use libraries\Helper as Helper;
use libraries\Renderer as Renderer;
use Medoo\Medoo;

/**
Expand Down Expand Up @@ -52,7 +53,7 @@ class App
*/
public $db_connection;
/**
* @var Whoops object
* @var \Whoops object
*/
public $whoops = null;
/**
Expand Down Expand Up @@ -328,30 +329,16 @@ public function render($part, $data = array(), $class = null)
$data["switch_user_requested"] = $this->switch_user_requested;
// Extract array keys into variables
extract($data);
// If layout was activated (default)
if ($this->isLayouts()) {
// include $this->header_path;
// include $this->footer_path;
// If layout was activated (default)
include $this->layout_file;
} else {
// Extract without layout
$this->render_partial($part);
Renderer::render_partial($part);
}
}
}

/**
* Render partial file wihout checking layout switch
* Note: Extracting arrays into variables are contained each view
*
* @param string $part = Partial view
*/
public function render_partial($part, $data = array())
{
extract($data);
include $this->views_path . $part . '.php';
}

/**
* Custom message params
* Title - $data['error_title'] - String
Expand Down Expand Up @@ -486,6 +473,15 @@ public function getControllersDir()
return $this->controllers_path;
}

/**
* Summary of getViewsPath
* @return string
*/
public function getViewsPath()
{
return $this->views_path;
}

/**
* Extract with additional helpers to identify the following
* - If user is logged in
Expand Down
2 changes: 1 addition & 1 deletion classes/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public function verifyResetCode($email, $reset_code)
$result = $query->fetch(); // overwrite for now
$datetime = new DateTime($result['created']);
$timestamp_from_query = $datetime->format("U");
var_dump($timestamp_from_query);
// \libraries\Debugger::dump($timestamp_from_query);
if (($result['code'] == $reset_code) && ($timestamp_from_query > $timestamp_one_hour_ago)) {
$this->messages[] = "Verified. Please reset your password now.";
$this->status = "success";
Expand Down
3 changes: 3 additions & 0 deletions classes/handlers/URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function __construct($app)
// Reuse the application
$this->app = $app;

// Establishing up global context
$GLOBALS["context"] = $app;

// Get the current URI without query parameters
$request_uri = strtok($_SERVER['REQUEST_URI'], '?');
// Tentative condition
Expand Down
8 changes: 2 additions & 6 deletions libraries/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public static function json_decode($json)

/**
* Display Messages
* TODO: Expose Global Variables for layouts
* TODO: Use app context instead
* @return void
*/
public static function getFeedback()
{
Expand All @@ -56,10 +55,7 @@ public static function getFeedback()
if (!empty($obj['messages'])) {
$data = array("_notification_messages" => $obj['messages']);
$file = "templates/partials/notification";
// TODO: Report this error if context doesn't exists
if (isset($GLOBALS["context"])) {
$GLOBALS["context"]->render_partial($file, $data);
}
Renderer::render_partial($file, $data);
}
}
Session::destroy('response');
Expand Down
36 changes: 36 additions & 0 deletions libraries/Renderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace libraries;

use Exception;

/**
* Renderer
*
* PHP Version 7.2
*
* @category Renderer
* @package hello-php
* @author John Cyrill Corsanes <[email protected]>
* @license http://opensource.org/licenses/MIT MIT License
* @version v0.7.1-alpha
* @link https://github.com/jcchikikomori/hello-php
*/
class Renderer
{
/**
* Render partial file wihout checking layout switch
* Note: Extracting arrays into variables are contained each view
*
* @param string $part = Partial view
*/
public static function render_partial($part, $data = array())
{
extract($data);
if (isset($GLOBALS["context"])) {
include $GLOBALS['context']->getViewsPath() . $part . '.php';
} else {
throw new Exception("Unable to locate the context.");
}
}
}
2 changes: 1 addition & 1 deletion views/templates/partials/logged_in.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<?php
// show potential errors / feedback (from session)
// libraries\Helper::getFeedback();
libraries\Helper::getFeedback();
?>
<?php
// Using Session library
Expand Down

0 comments on commit 95d1cd1

Please sign in to comment.