PHP Classes

File: config/init.php

Recommend this page to a friend!
  Classes of Boss Ibrahim Mussa   Wepesi PHP Router with Parameters   config/init.php   Download  
File: config/init.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Wepesi PHP Router with Parameters
Route requests to functions or controller classes
Author: By
Last change:
Date: 1 year ago
Size: 1,165 bytes
 

Contents

Class file image Download
<?php
include "global.php";

// class autoload
spl_autoload_register(function ($class) {
   
$directories = getSubDirectories(ROOT."class");
   
$class_arr = explode("\\", $class);
   
$len = count($class_arr);
   
$classFile = $class_arr[($len - 1)];
    foreach (
$directories as $dir) {
       
$file = $dir . "/" . checkFileExtension($classFile);
        if (
is_file($file)) {
            require_once(
$file);
        }
    }
});
/**
 * @param $dir : directory name where to check
 * @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;
}
/**
 * @param $fileName : return file with valid extension
 * @return mixed|string
 */
function checkFileExtension($fileName, $extention = "php")
{
   
$file_parts = pathinfo($fileName);
   
$file = (isset($file_parts['extension']) && $file_parts['extension'] == $extention) ? $fileName : $fileName . ".$extention";
    return
$file;
}