Skip to content

Commit

Permalink
Merge pull request #48 from kivudesign/version_boss
Browse files Browse the repository at this point in the history
Refactoring app project to support multiple folder autoloading from buildit autoload,
  • Loading branch information
bim-g committed Jun 16, 2022
2 parents 942183f + 4899b36 commit 0ccd57a
Show file tree
Hide file tree
Showing 48 changed files with 951 additions and 572 deletions.
File renamed without changes.
23 changes: 23 additions & 0 deletions config/globals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Support for DataBase configuration resource
* Authentication information store on global variable
*/
$db_conf= $load_init_config["db_conf"];
// database configuration setup
$GLOBALS['config']=[
'mysql'=>[
'host'=> $db_conf["host"],
'username'=> $db_conf["user"],
'password'=> $db_conf["password"],
'db'=> $db_conf["database"]
],
'remender'=>[],
'session'=>[
"token_name"=>"token"
],
'controller'=>WEB_ROOT,
'middleware'=>WEB_ROOT,
'vendor'=>false,
'autoload'=>["src","controller","middleware"]
];
5 changes: 5 additions & 0 deletions config/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
header( 'HTTP/1.0 403 Forbidden', TRUE, 403 );
die( header( 'location:'.str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']) ) );
}
2 changes: 2 additions & 0 deletions config/load_init_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$load_init_config = parse_ini_file("config.ini", true);
23 changes: 23 additions & 0 deletions controller/homeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Wepesi Home Controller
*
*/
namespace Wepesi\Controller;
use Wepesi\Core\Input;
use Wepesi\Core\Redirect;
use Wepesi\Core\Session;

class homeController{
function __construct()
{
}

function home(){
Redirect::to(WEB_ROOT);
}
function changeLang(){
Session::put('lang', Input::get("lang"));
Redirect::to(WEB_ROOT);
}
}
15 changes: 0 additions & 15 deletions controller/homeCtrl.php

This file was deleted.

5 changes: 5 additions & 0 deletions controller/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
header( 'HTTP/1.0 403 Forbidden', TRUE, 403 );
die( header( 'location:'.str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']) ) );
}
26 changes: 17 additions & 9 deletions global/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@
* it speed and support event namespaces.
* but it not support for external module install via composer.
*/
spl_autoload_register(function($class){
$dirs = getSubDirectories("src");
$class_arr=explode("\\",$class);
$len=count($class_arr);
$classFile=$class_arr[($len-1)];
foreach($dirs as $dir){
$file=$dir."/". checkFileExtension($classFile);
if (is_file($file)) { // check if the file exist
require_once($file); // incluse the file request if it exist
$config = $GLOBALS["config"];
$autoload =["src"];
if(isset($config["autoload"])){
$autoload=is_string($config['autoload'])?[$config['autoload']]:$config['autoload'];
}

spl_autoload_register(function($class) use ($autoload) {
foreach ($autoload as $src){
$dirs = getSubDirectories($src);
$class_arr = explode("\\",$class);
$len = count($class_arr);
$classFile = $class_arr[($len-1)];
foreach($dirs as $dir){
$file = $dir."/". checkFileExtension($classFile);
if ( is_file($file) ) {
require_once($file);
}
}
}
});
10 changes: 4 additions & 6 deletions global/global.php → global/constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
*/
const LANG = "fr";
include("./lang/".LANG."/language.php");
// load configguration
$ini_array =(object) parse_ini_file("./config/config.ini", true);

//include database Globale configuration
include ("db.php");
// inlude language file according to your configuraiton


// include language file according to your configuration
define("LANG_VALIDATE", $validation);

//web root configaration
Expand All @@ -20,7 +18,7 @@
define('ROOT', str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']));

// default timezone
define('TIMEZONE','Africa/Kigali');
const TIMEZONE = 'Africa/Kigali';

//define default domain
$server_name=$_SERVER['SERVER_NAME'];
Expand Down
19 changes: 0 additions & 19 deletions global/db.php

This file was deleted.

5 changes: 5 additions & 0 deletions global/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
header( 'HTTP/1.0 403 Forbidden', TRUE, 403 );
die( header( 'location:'.str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']) ) );
}
46 changes: 16 additions & 30 deletions global/init.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
<?php
session_start();

require_once 'global.php';
//

/**
* @param $dir
* @return array|false
*
*/
function getSubDirectories($dir) {
$subDir = array();
$directories = array_filter(glob($dir), 'is_dir');
$subDir = array_merge($subDir, $directories);
foreach ($directories as $directory) $subDir = array_merge($subDir, getSubDirectories($directory . '/*'));
return $subDir;
}
session_start();

include "config/load_init_config.php";
include "constant.php";
include "config/globals.php";
include "helper/functions.php";
/**
* @param $fileName
* @return mixed|string
*
*/
function checkFileExtension($fileName){
$file_parts = pathinfo($fileName);
$file = isset($file_parts['extension']) ? $fileName : $fileName . ".php";
return $file;
}
require_once 'cors.php';
//activet this module for only for api app
$autoload= (object)$ini_array->autoload;
if(isset($autoload->vendor) && !$autoload->vendor){
var_dump($GLOBALS['config']);
include 'autoload.php';
}else{
die("There hould be a vendor key on autoload patern to manage with autoload to be used. by default if false");
}

$vendor = isset($GLOBALS["config"]) ? (isset($GLOBALS["config"]["vendor"]) ?$GLOBALS["config"]["vendor"]: false) : false;

if( !$vendor ){
include "autoload.php";
}else{
include "vendor/autoload.php";
}
23 changes: 23 additions & 0 deletions helper/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Scan a folder an get all directory inside
* @param $dir
* @return array
*/
function getSubDirectories($dir): array
{
$subDir = array();
$directories = array_filter(glob($dir), 'is_dir');
$subDir = array_merge($subDir, $directories);
foreach ($directories as $directory) $subDir = array_merge($subDir, getSubDirectories($directory . '/*'));
return $subDir;
}
/**
* @param $fileName
* @return mixed|string
*/
function checkFileExtension($fileName){
$file_parts = pathinfo($fileName);
return isset($file_parts['extension']) ? $fileName : $fileName . ".php";
}
5 changes: 5 additions & 0 deletions helper/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
header( 'HTTP/1.0 403 Forbidden', TRUE, 403 );
die( header( 'location:'.str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']) ) );
}
3 changes: 1 addition & 2 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?php
require_once 'global/init.php';
require_once 'route/index.php';
?>
require_once 'route/router.php';
5 changes: 5 additions & 0 deletions lang/en/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
header( 'HTTP/1.0 403 Forbidden', TRUE, 403 );
die( header( 'location:'.str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']) ) );
}
3 changes: 2 additions & 1 deletion lang/en/language.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"email"=>"invalid email address",
"url"=>"invalid url",
"file"=>"should be a file",
"unique"=>"already exist."
"unique"=>"already exist.",
"unknown"=>"unknown"
];
5 changes: 5 additions & 0 deletions lang/fr/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
header( 'HTTP/1.0 403 Forbidden', TRUE, 403 );
die( header( 'location:'.str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']) ) );
}
5 changes: 4 additions & 1 deletion lang/fr/language.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"A Simple Php MVC platform to develop quickly a php application"=>"Une plateforme Simple Php MVC pour développer rapidement une application web",
"Home"=>"Acceuill",
"contact"=>"contact",
"Translate"=>"Traduire",
"Change the language"=>"Changez de langue"
];
$validation=[
"required"=> "est obligatoire",
Expand All @@ -16,5 +18,6 @@
"email"=> "Adresse e-mail invalide",
"url"=>"invalid url",
"file"=> "devrait être un fichier",
"unique"=> "existe déjà."
"unique"=> "existe déjà.",
"unknown"=> "Inconus."
];
5 changes: 5 additions & 0 deletions lang/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
header( 'HTTP/1.0 403 Forbidden', TRUE, 403 );
die( header( 'location:'.str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']) ) );
}
5 changes: 5 additions & 0 deletions layout/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
header( 'HTTP/1.0 403 Forbidden', TRUE, 403 );
die( header( 'location:'.str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']) ) );
}
5 changes: 5 additions & 0 deletions layout/js/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
header( 'HTTP/1.0 403 Forbidden', TRUE, 403 );
die( header( 'location:'.str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']) ) );
}
5 changes: 5 additions & 0 deletions layout/style/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
header( 'HTTP/1.0 403 Forbidden', TRUE, 403 );
die( header( 'location:'.str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']) ) );
}
29 changes: 29 additions & 0 deletions middleware/Validation/HomeValidation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace wepesi\Validation;

use Wepesi\Core\Validation\Validate;

class HomeValidation
{
function changeLang(){
$valid = new Validate();
$schema = [
"token" => $valid->string("token")
->min(1)
->max(2)
->required()
->check(),
"lang" => $valid->string("lang")
->min(1)
->max(2)
->required()
->check()
];

$valid->check($_POST,$schema);
if(!$valid->passed()){
var_dump($valid->errors());
die();
}
}
}
5 changes: 5 additions & 0 deletions middleware/Validation/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
header( 'HTTP/1.0 403 Forbidden', TRUE, 403 );
die( header( 'location:'.str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']) ) );
}
5 changes: 5 additions & 0 deletions middleware/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
header( 'HTTP/1.0 403 Forbidden', TRUE, 403 );
die( header( 'location:'.str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']) ) );
}
18 changes: 4 additions & 14 deletions route/index.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
<?php

use Wepesi\Core\Routing\Router;
use Wepesi\Core\View;

$route=new Router();
// setup get started pages index
$route->get('/', function () {
new View('index');
});
$route->get('/home', "homeCtrl#home");
$route->get('/contact', [homeCtrl::class,"contact"]);

$route->run();
?>
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
header( 'HTTP/1.0 403 Forbidden', TRUE, 403 );
die( header( 'location:'.str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']) ) );
}
2 changes: 2 additions & 0 deletions route/router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
include "web.php";
Loading

0 comments on commit 0ccd57a

Please sign in to comment.