Skip to content

Commit

Permalink
Merge pull request #198 from kivudesign/refactor_http_controller
Browse files Browse the repository at this point in the history
Refactor Http Controller and Middleware to handle future new feature from base controller and middleware
  • Loading branch information
bim-g authored Sep 21, 2024
2 parents 724053b + f052002 commit 68c8e4c
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 31 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
],
"require": {
"php" : ">=7.4",
"ext-mbstring": "*",
"ext-libxml": "*",
"ext-dom": "*"
"ext-mbstring": "*",
"ext-libxml": "*",
"ext-dom": "*",
"ext-json": "*"
}
}
5 changes: 3 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions config/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,16 @@ function dumper($ex)
}
}

/**
* get a formatted application route path
* @param string $path
* @return string
*/
function route_path(string $path): string{
return WEB_ROOT . ltrim($path,'/');
if (! function_exists('url')) {
/**
* get a formatted application url route path
* @param string $path
* @return string
*/
function url(string $path): string
{
return WEB_ROOT . ltrim($path, '/');
}
}

if (! function_exists('fileExists')) {
Expand Down
2 changes: 1 addition & 1 deletion controller/indexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Wepesi\Controller;


use Wepesi\Core\Controller;
use Wepesi\Core\Http\Controller;
use Wepesi\Core\Http\Input;
use Wepesi\Core\Http\Redirect;
use Wepesi\Core\Session;
Expand Down
3 changes: 1 addition & 2 deletions middleware/Validation/exampleValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Wepesi\Middleware\Validation;

use Wepesi\Core\Application;
use Wepesi\Core\MiddleWare;
use Wepesi\Core\Http\MiddleWare;

class exampleValidation extends MiddleWare
{
Expand Down
9 changes: 6 additions & 3 deletions src/Core/Controller.php → src/Core/Http/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
* Copyright (c) 2023-2024. Wepesi Dev Framework
*/

namespace Wepesi\Core;
namespace Wepesi\Core\Http;

use Wepesi\Core\Http\Providers\BaseControllerMiddleware;
use Wepesi\Core\Views\Provider\Contract\ViewsContract;
use Wepesi\Core\Views\View;

/**
*
*/
abstract class Controller
abstract class Controller extends BaseControllerMiddleware
{
/**
* @var View
Expand All @@ -21,7 +22,9 @@ abstract class Controller
/**
*
*/
public function __construct(){
public function __construct()
{
parent::__construct();
$this->view = new View();
}
}
6 changes: 4 additions & 2 deletions src/Core/MiddleWare.php → src/Core/Http/MiddleWare.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
* Copyright (c) 2023. wepesi dev framework
*/

namespace Wepesi\Core;
namespace Wepesi\Core\Http;

use Wepesi\Core\Http\Providers\BaseControllerMiddleware;
use Wepesi\Core\Validation\Rules;
use Wepesi\Core\Validation\Validate;

abstract class MiddleWare
abstract class MiddleWare extends BaseControllerMiddleware
{
protected Validate $validate;
protected Rules $rule;

public function __construct()
{
parent::__construct();
$this->rule = new Rules();
$this->validate = new Validate();
}
Expand Down
27 changes: 27 additions & 0 deletions src/Core/Http/Providers/BaseControllerMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/*
* Copyright (c) 2024. Wepesi Dev Framework
*/

namespace Wepesi\Core\Http\Providers;

use Wepesi\Core\Media;

/**
*
*/
abstract class BaseControllerMiddleware
{
/**
* @var Media
*/
protected Media $media;

/**
*
*/
public function __construct()
{
$this->media = new Media();
}
}
17 changes: 17 additions & 0 deletions src/Core/Http/Providers/Contracts/RequestContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/*
* Copyright (c) 2024. Wepesi Dev Framework
*/

namespace Wepesi\Core\Http\Providers\Contracts;

/**
*
*/
interface RequestContract
{
/**
* @return RequestContract
*/
public static function createFromGlobals(): RequestContract;
}
25 changes: 25 additions & 0 deletions src/Core/Http/Providers/Contracts/ResponseContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/*
* Copyright (c) 2024. Wepesi Dev Framework
*/

namespace Wepesi\Core\Http\Providers\Contracts;

/**
*
*/
interface ResponseContract
{
/**
* @param array|string $data
* @param int $status
* @return mixed
*/
public static function send($data, int $status);

/**
* @param int $status_code
* @return mixed
*/
public static function setStatusCode(int $status_code);
}
11 changes: 4 additions & 7 deletions src/Core/Http/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,30 @@
class Redirect
{
/**
* @param $location
* @param string $location can be an url path a file path
* @return void
*/
static function to($location = null)
static public function to(string $location = '')
{
$rule = new Rules();
$validate = new Validate();
$schema = ['link' => $rule->string()->url()];
$source = ['link' => $location];
if ($location) {
if (strlen(trim($location)) != 0) {
// check if the location is an url
$validate->check($source, $schema);
if ($validate->passed()) {
// Redirect a url
header('Location:' . $location, true, 301);
exit();
} else {
$webroot = substr(WEB_ROOT, 0, -1);
$link = Escape::addSlashes($location);
$location = $link == '' ? WEB_ROOT : $webroot . $link;
header('Location:' . $location);
exit();
}
} else {
//TODO Design a 404 pages in case file or page does not exist
header('HTTP/1.0 404 Not Found', true, 404);
die();
}
exit();
}
}
4 changes: 3 additions & 1 deletion src/Core/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

namespace Wepesi\Core\Http;

class Request
use Wepesi\Core\Http\Providers\Contracts\RequestContract;

class Request implements RequestContract
{
public string $uri;
public string $method;
Expand Down
4 changes: 3 additions & 1 deletion src/Core/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

namespace Wepesi\Core\Http;

use Wepesi\Core\Http\Providers\Contracts\ResponseContract;

/**
*
*/
class Response
class Response implements ResponseContract
{
/**
* @param $data
Expand Down
2 changes: 1 addition & 1 deletion views/404.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<body>
<div class="w3-margin w3-center">
<h1 class="w3-text-green ">404</h1>
<p><a href="<?= route_path('/') ?>">Go home</a></p>
<p><a href="<?= url('/') ?>">Go home</a></p>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion views/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<?php } ?>
<div class="w3-card w3-border w3-round-large " style="width: 300px;overflow: hidden">
<h3 class="w3-text-blue-gray w3-padding"><?= $language->translate("Change the language") ?></h3>
<form action="<?= route_path("/changelang") ?>" method="post">
<form action="<?= url("/changelang") ?>" method="post">
<input type="hidden" name="token" value="<?= Token::generate() ?>">
<select name="lang" id="lang_id" class="w3-select w3-center">
<option value="" class="w3-center w3-large" disabled selected><?= $lang ?></option>
Expand Down

0 comments on commit 68c8e4c

Please sign in to comment.