PHP Classes

File: core/Common.php

Recommend this page to a friend!
  Classes of Jonathan Alexey   JFWork   core/Common.php   Download  
File: core/Common.php
Role: Auxiliary script
Content type: text/plain
Description: Configuration script
Class: JFWork
Tiny framework based on the MVC design pattern
Author: By
Last change: Update of core/Common.php
Date: 3 years ago
Size: 3,407 bytes
 

Contents

Class file image Download
<?php

if( ! function_exists('load_class') ){
    function &
load_class ( $class ){
       
        static
$_is_load;
       
       
        if(isset(
$_is_load[strtolower($class)])){
            return
$_is_load[strtolower($class)];
        }
       
       
       
       
$name = false;
       
        foreach ( array (
CORE ) as $dir ){
           
            if(
file_exists($dir.$class.".php")){
           
               
$name = $class;
           
                if(
class_exists($name) === false){
                    require(
$name.".php");
                }
                break;
           
            }
       
        }
       
        if (
$name === FALSE){
           
log_msg(' Load class not found '.$class);
        }
       
       
is_loaded($class);
       
       
$_is_load[ strtolower($class) ] = new $name();
        return
$_is_load[$class];
       
    }
}


if( !
function_exists('is_loaded')){
    function
is_loaded($name = ''){
   
        static
$_is_loaded = array();
       
        if (
$name != ''){
           
$_is_loaded[strtolower($name)] = $name;
        }
       
        return
$_is_loaded;
    }
}

if(!
function_exists('URL')){
    function
URL(){
       
         if ( ! isset(
$_SERVER['REQUEST_URI']) OR ! isset($_SERVER['SCRIPT_NAME'])){
            return
'';
        }
       
       
// Uri $_SERVER['REQUEST_URI']
       
$uri = $_SERVER['REQUEST_URI'];
       
       
        if (
strpos($uri, $_SERVER['SCRIPT_NAME']) === 0){
           
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
        }elseif (
strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0){
           
$uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
        }
           
       
// Some sort of validation
       
if ($uri == '/' || empty($uri)){
            return
'/';
        }
       
       
// parse url to nice and ease.
       
$uri = parse_url($uri, PHP_URL_PATH);
       
       
// clean the uri
       
$uri_clean = explode("/",str_replace(array('//', '../'), '/', trim($uri, '/')));
       
       
       
           
        return
$uri_clean;
    }
}

if(!
function_exists('log_msg')){
   
// log_msg to return a line and file
    // This can be convert to to write a file log
    // For now lets leave like this to development.
   
function log_msg ($msg= 'Erro'){
       
$debuger = debug_backtrace();
       
$line = $debuger[0]['line'];
       
$file = $debuger[0]['file'];
       
        return
"<h1>".$msg."</h1><br/><b>Line:</b>".$line."<br/><b>File:</b>".$file;
       
    }
}

if(!
function_exists('load_file')){
   
// Load file
    // Can be some helper functions such cal,url,math,dates and other
    // Or can be config file only.
   
function load_file( $file, $directory = 'config'){
   
       
// Keep track all file it was loaded!
       
$_load_file = array();
       
       
// If's allready exists so return true
       
if(isset($_load_file[strtolower($file)])){
           
// It's already have this file load.
            // Exit!
           
return true;
        }
       
       
// If Directory isn't a dir or don't exists return an erro!
       
if(!is_dir($directory)){
           
log_msg("Directory $directory doesn't exists");
            return
false;
        }
       
       
// Save load file
       
$_load_file[strtolower($file)] = $file;
       
       
// call file
       
require $directory."/".$file;
       
       
// Success!
       
return true;
       
    }
}


// Load views
if(!function_exists('load_view')){
    function
load_view ( $file , $data = array(), $toReturn = false){
       
// Exists file ?!
       
if(!file_exists(VIEWS.$file.".php")){
           
// Ups, no.
           
throw new Exception('Não existe :'.(VIEWS.$file.".php").'.');
        }
       
       
// Ob_start.
       
ob_start();
           
// Extract data to the file
           
extract($data);
           
           
// call the file
           
require VIEWS.$file.".php";
           
           
// my precious html.
           
$html = ob_get_contents();
       
       
// Clean my mess, please.
       
ob_end_clean();
       
       
// Is to return of controller ?!
       
if($toReturn){
           
// Oh yeah man!
           
return $html;
        }
       
       
// Let's print html to the client.
       
echo $html;
    }
}



?>