PHP Classes
elePHPant
Icontem

File: tinyMVC/lib/autoload.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Alan H. Lake  >  tinyMVC  >  tinyMVC/lib/autoload.php  >  Download  
File: tinyMVC/lib/autoload.php
Role: Class source
Content type: text/plain
Description: allows automatic loading of required files
Class: tinyMVC
MVC based Web application framework
Author: By
Last change: Added path to File name
Date: 4 years ago
Size: 8,403 bytes
 

Contents

Class file image Download
<?php
spl_autoload_register
("AutoLoader::autoLoad");

class
AutoLoader {

/*
 * In a tinyMVC application, classes will be found in
 * <project>/app/controllers
 * tinyMVC/lib
 * tinyMVC/vendors/<vendor>
 *
 * Obviously, in a tinyMVC application is written so that classes appear
 * in other directories, then this class may have to be modified.
 */

   
static function autoLoad($class) {
        if(
self::tinyMVC($class)) return true;
        else if(
self::ezsql($class)) return true;
        else if(
self::tbs($class)) return true;
        else if(
self::zendStyle($class)) return true ;
        else if(
self::sufix($class)) return true ;
        else if(
self::allDir($class)) return true ;
        else if(
self::allDirExt($class)) return true ;
        else if(
self::nonSense($class)) return true ;
        else throw new
Exception("Class ".$class." not found!");
        return
false;
    }

    static function
tinyMVC($class) {
       
$paths = array();
       
$paths[] = PROJECT_ROOT_DIR . 'app' . DS . 'controllers';
       
$paths[] = PROJECT_ROOT_DIR . 'app' . DS . 'models';
       
$paths[] = PROJECT_ROOT_DIR . 'app' . DS . 'installers';
       
$paths[] = TINY_MVC_DIR . 'lib';
       
$dir = opendir(TINY_MVC_DIR.'vendor'.DS);
        while(
$file = readdir($dir)) {
           
$file_path = TINY_MVC_DIR . 'vendor' . DS . $file ;
            if(
$file == "." or $file == "..") continue ;
            if(!
is_dir($file_path)) continue;
           
$paths[] = $file_path;
           
$dir1 = opendir($file_path);
            while(
$file = readdir($dir1)) {
               
$next_path = $file_path.DS.$file;
                if(
$file == "." or $file == "..") continue ;
                if(!
is_dir($next_path)) continue;
               
$paths[] = $next_path;
            }
        }
        foreach(
$paths as $prefix){
         
$path[0] = $prefix . DS . $class . '.php';
         
$path[1] = $prefix . DS . $class . '.class.php';
         
$path[2] = $prefix . DS . 'class.' . $class . '.php';
         
$path[3] = $prefix . DS . strtolower($class) . '.php';
         
$path[4] = $prefix . DS . strtolower($class) . '.class.php';
         
$path[5] = $prefix . DS . 'class.' . strtolower($class) . '.php';
          foreach(
$path as $thisPath){
            if(
file_exists($thisPath)){

              require_once
$thisPath;
              return
true;
            }
          }
        }
        return
false;
    }

    static function
ezsql($class) {
        if(
substr($class,0,5) != 'ezSQL') return false;
       
$paths = array();
       
$class_parts = explode('_',$class);
       
$file_name = 'ez_sql_' . $class_parts[1];
       
$path = TINY_MVC_DIR.'vendor'.DS.'ez_sql'.DS;
       
$dir = opendir($path);
        while(
$file = readdir($dir)) {
            if(
$file == "." or $file == "..") continue ;
           
$file = $path . $file;
            if(!
is_dir($file)) continue;
           
$paths[] = $file;
        }
       
clearstatcache();
        foreach(
$paths as $prefix){
           
$thisPath = $prefix . DS . $file_name . '.php';
            if(
file_exists($thisPath)){
             
$core = TINY_MVC_DIR . 'vendor' . DS . 'ez_sql' . DS . 'shared' . DS . 'ez_sql_core.php';
              require_once
$core;
              require_once
$thisPath;
              return
true;
            }
        }
        return
false;
    }

    static function
tbs($class) {
       
$path = TINY_MVC_DIR.'vendor'.DS.'tbs'.DS;
        if(
$class == 'clsTinyButStrong') {
           
$file = $path . 'tbs_class_php5.php';
            if(
file_exists($file)) {
               
$file = $path . 'plugins' .DS . 'tbs_plugin_ezsql.php';
                if(
file_exists($file)) require_once $file;
               
$file = $path . 'plugins' .DS . 'tbsdb_pdo.php';
                if(
file_exists($file)) require_once $file;
               
$file = $path . 'plugins' .DS . 'tbs_plugin_html.php';
                if(
file_exists($file)) require_once $file;
               
$file = $path . 'plugins' .DS . 'tbs_plugin_bypage.php';
                if(
file_exists($file)) require_once $file;
               
$file = $path . 'plugins' .DS . 'tbs_plugin_cache.php';
                if(
file_exists($file)) require_once $file;
               
$file = $path . 'plugins' .DS . 'tbs_plugin_mergeonfly.php';
                if(
file_exists($file)) require_once $file;
               
$file = $path . 'plugins' .DS . 'tbs_plugin_navbar.php';
                if(
file_exists($file)) require_once $file;
               
$file = $path . 'plugins' .DS . 'tbsdb_csv_php5.php';
                if(
file_exists($file)) require_once $file;
               
$file = $path . 'plugins' .DS . 'opentbs' . DS . 'tbs_plugin_opentbs.php';
                if(
file_exists($file)) require_once $file;
               
$file = $path . 'tbs_class_php5.php';
                require_once
$file;
                return
true;
            }
        }
        return
false;
    }

    static function
zendStyle($class) {
       
$p = explode("_",$class);
       
$file = implode(DS,$p).".php";
        if(!
file_exists($file)) return false;
        include
$file;
        return
true ;
    }

    static function
sufix($class) {
        if (!
preg_match("#(Model|View|Controller)#i", $class, $fragment)) {
            return
false ;
        }
       
$class = substr($class, 0, strlen($class)-strlen($fragment[1]));
       
$sufix = $fragment[1];
       
$file = $sufix.DS.$class.".php";
        if(
file_exists($file)) {
            include
$file ;
            return
true ;
        }
        return
false ;
    }

    static function
allDir($class){
        if(
self::findClass($class,".")) return true ;
        else return
false;
    }

    static function
findClass($class,$dir) {
       
$ponteiro = opendir($dir);
        while(
$file = readdir($ponteiro)) {
           
$file_path = $dir.DS.$file ;
            if(
$file == "." or $file == "..") {
                    continue ;
            } else if(
is_dir($file_path)) {
                    if(
self::findClass($class,$file_path)) {
                            return
true ;
                    }
            } else if(
$file == $class.".php") {
                    include
$file_path ;
                    return
true ;
            }
        }
        return
false ;
    }

    static function
allDirExt($class) {
        if(
self::findClassExt($class,".")) return true ;
        else return
false;
    }

    static function
findClassExt($class,$dir) {
       
$ponteiro = opendir($dir);
        while(
$file = readdir($ponteiro)) {
           
$file_path = $dir.DS.$file ;
            if(
$file == "." or $file == "..") {
                    continue ;
            } else if(
is_dir($file_path)) {
                    if(
self::findClassExt($class,$file_path)) {
                            return
true ;
                    }
            } else if(
$file == $class.".class.php") {
                    include
$file_path ;
                    return
true ;
            }
        }
        return
false ;
    }

    static function
nonSense($class) {
        if(
self::findClassNonSense($class,".")) return true ;
        else return
false;
    }

    static function
findClassNonSense($class,$dir) {
       
$ponteiro = opendir($dir);
        while(
$file = readdir($ponteiro)) {
           
$file_path = $dir.DS.$file ;
            if(
$file[0] == "." or $file == "..") {
                continue ;
            } else if(
is_dir($file_path)) {
                if(
self::findClassNonSense($class,$file_path)) {
                    return
true ;
                }
            } else {
               
//$content = file_get_contents( $file_path );
                //$pattern = "/class[\s]+".$class."[\s|{]/i";
               
$file = file($file_path);
               
$pattern = "/^[\s]*class[\s]+".$class."[\s|{]/i";
                if(
count($file) >= 1){
                    foreach(
$file as $content){
                        if(
preg_match($pattern,$content)) {
                            include
$file_path;
                            return
true;
                        }
                    }
               }
            }
        }
        return
false ;
    }

}
?>