Skip to content

Instantly share code, notes, and snippets.

@tylerhcarter
Created June 10, 2012 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tylerhcarter/2903165 to your computer and use it in GitHub Desktop.
Save tylerhcarter/2903165 to your computer and use it in GitHub Desktop.
Cha Loader
<?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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment