public
Created

Cha Loader

  • Download Gist
gistfile1.aw
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
<?php
/**
* Autoloads classes using the SPL library and the directory model given.
* Contains support for namespaces.
*
* @author Tyler(Chacha)
* @package Cha
*/
class Cha_Loader
{
/**
* Include path for the script to look in
* @var string
*/
public $include_path = "";
private $directory_model = array();
private $extension = array();
 
/**
* Constructs the Autoloader
* @param string $include_path The topmost include path for the application
* @param bool $auto_register Whether to automatically register the Autoloader
* or let the user manually register with Autoloader::register();
*/
public function __construct($directory_model, $extension = "php", $include_path="", $auto_register=true)
{
$this->directory_model = $directory_model;
 
// Remove a dot at the beginning of the file extension
if(strpos($extension, ".") === 0)
{
$this->extension = substr($extension, 0);
}
else
{
$this->extension = $extension;
}
 
if(count($directory_model) == 0)
{
throw new Exception("No Directories Defined");
}
 
if($auto_register == true)
{
$this->registerAutoloader();
}
 
if(empty($include_path))
{
$include_path = get_include_path();
}
$this->include_path = $include_path;
}
 
/**
* Register the Autoloader with the SPL Autoload Queue
*/
public function registerAutoloader()
{
spl_autoload_register(array($this, "loadClass"));
}
 
/**
* Attempts to load the class based on the directory structure given
* @param string $class_name
* @return bool True on Success, False on Failure
*/
public function loadClass($class_name)
{
 
if(strpos($class_name, "\\") === 0)
{
$class_name = substr($class_name, 1);
}
 
$include_path = "";
// First Check for Namespaces
if(strpos($class_name, "\\") !== FALSE)
{
$namespace = substr($class_name, 0, strrpos($class_name, "\\"));
$class_name = substr(substr($class_name, strrpos($class_name, "\\")), 1);
}
else
{
$namespace = "base";
}
if(array_key_exists($namespace, $this->directory_model))
{
 
// Check if the value is an array of paths
if(is_array($this->directory_model[$namespace]))
{
 
// Try to include each file
$statusCode = false;
foreach($this->directory_model[$namespace] as $path)
{
if($this->includeFile($path, $class_name) == true)
{
$statusCode = true;
break;
}
}
 
return $statusCode;
}
else
{
// Include the path via a pre-defined model
return $this->includeFile($this->directory_model[$namespace], $class_name);
}
 
}
else
{
// Include the path using the Namespace as a directory inside the include path
return $this->includeFile($this->include_path . $namespace ."/", $class_name);
}
}
 
private function includeFile($directory, $class_name)
{
$include_path = $this->buildFilePath($directory, $class_name);
 
if(file_exists($include_path))
{
include($include_path);
return true;
}
else
{
return false;
}
}
 
private function buildFilePath($directory, $class_name)
{
return $directory . $class_name . "." . $this->extension;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.