<?php
/*
* Autoload.php is copyright � 2010. EarthWalk Software.
* Licensed under the Academic Free License version 3.0.
* Refer to the file named License provided with the source.
*/
/**
* Autoload static class
*
* This package contains the __autoload implementation.
* @author Jay Wheeler
* @version 1.0
* @copyright � 2010. EarthWalk Software.
* @license refer to License file provided with the source.
* @package IGSGateway
* @subpackage Autoload
*/
/**
* Autoload static class.
*
* php5 Autoload object - an implemetation of php5 __autoload to automatically
* load files quickly only when they are needed.
* @package IGSGateway
* @subpackage Autoload
*/
class Autoload
{
/**
* @static string $IGSRoot contains the computed root directory path
*/
protected static $IGSRoot=null;
/**
* @static string $InstallRoot contains the installation root directory path
*/
protected static $InstallRoot=null;
/**
* @static string $ZendRoot contains the zend root directory path
*/
protected static $ZendRoot=null;
/**
* @static string $Separator contains the current separator
*/
protected static $Separator='';
/**
* LoadFromProject.
*
* Parses the class name to create a file name and load the required class file.
* Returns true if successful, false if not.
* @param string $class = the class name to load in Zend Framework class naming convention
* @return boolean true = successful, false = not.
*/
protected static function LoadFromProject($class)
{
return self::LoadFromRoot($class, self::$InstallRoot);
}
/**
* LoadFromZend.
*
* Parses the class name to create a file name and load the required Zend Framework class file.
* Returns true if successful, false if not.
* @param string $class = the class name to load in Zend Framework class naming convention
* @return boolean true = successful, false = not.
*/
protected static function LoadFromZend($class)
{
if (! self::$ZendRoot)
{
return false;
}
return self::LoadFromRoot($class, self::$ZendRoot);
}
/**
* LoadFromRoot.
*
* Parses the class name to create a file name relative to the install root and load the required class file.
* Returns true if successful, false if not.
* @param string $class the class name to load in Zend Framework class naming convention
* @param string $root root path to search
* @return boolean true = successful, false = not successful
*/
protected static function LoadFromRoot($class, $root)
{
$classFile = sprintf('%s%s%s.php',
$root,
self::LookupSeparator(),
str_replace('_', self::LookupSeparator(), $class));
if (file_exists($classFile))
{
require_once($classFile);
return true;
}
return false;
}
/**
* LoadFromIncludePath.
*
* Searches the include path for the required class file and loads it if found.
* Returns true if successful, false if not.
* @param string $class = the class name to load in Zend Framework class naming convention
* @return boolean
*/
protected static function LoadFromIncludePath($class)
{
$class = str_replace('_', self::LookupSeparator(), $class);
$directories = explode(PATH_SEPARATOR, get_include_path());
if ($directories[0] != '.')
{
array_unshift($directories, '.');
}
$pathDirectory = reset($directories);
do
{
$classFile = sprintf('%s%s%s.php',
$pathDirectory,
self::LookupSeparator(),
$class);
if (file_exists($classFile))
{
require_once($classFile);
return true;
}
}
while ($pathDirectory = next($directories));
return false;
}
/**
* LoadClass.
*
* Load the requested class file.
* @param string $class The name of the class and class file to load.
* @return boolean true = successful, false = failed.
*/
public static function LoadClass($class)
{
if (! self::$InstallRoot)
{
self::AutosetRoot();
}
//
// First, try to load from the project directory structure
//
if (self::LoadFromProject($class))
{
return true;
}
//
// First, try to load from the zend directory structure
//
if (self::LoadFromZend($class))
{
return true;
}
//
// Next, look for it in the include path
//
if (self::LoadFromIncludePath($class))
{
return true;
}
//
// Not found, so return false.
//
return false;
}
/**
* AutosetRoot.
*
* Use $IGSRoot to automatically set the installation root directory.
* @return string $InstallRoot is the root directory.
*/
public static function AutosetRoot()
{
self::$InstallRoot = self::IGSRoot();
return self::$InstallRoot;
}
/**
* IGSRoot.
*
* Determine the installation root.
* @return string $IGSRoot is the determined installation root. NOTE: the $InstallRoot is not set by this.
*/
public static function IGSRoot()
{
if (self::$IGSRoot)
{
return self::$IGSRoot;
}
/**
* An associative array which contains the individual components of the getcwd.
* @var array[string]string $dirs
*/
$altdirs = false;
$dirs = array();
if ((! $dirs = explode(PATH_SEPARATOR, $_SERVER['SCRIPT_FILENAME'])) ||
(count($dirs) < 2))
{
if ((! $dirs = explode('/', $_SERVER['SCRIPT_FILENAME'])) ||
(count($dirs) < 2))
{
$altdirs = true;
if (! $dirs = explode('/', dirname(__FILE__)))
{
$dirs[0] = '.';
$dirs[1] = '.';
}
}
}
array_pop($dirs);
if (! $altdirs)
{
array_pop($dirs);
}
self::$IGSRoot = implode('/', $dirs);
return self::$IGSRoot;
}
/**
* InstallRoot.
*
* Set the InstallRoot public static variable, if not null.
* Return the current setting of InstallRoot.
* @param string|null $installRoot = (optional) installation base
* @return string = current installRoot
*/
public static function InstallRoot($installRoot=null)
{
if ($installRoot != null)
{
self::$InstallRoot = $installRoot;
}
return self::$InstallRoot;
}
/**
* ZendRoot.
*
* Set the ZendRoot variable, if not null.
* Return the current setting of ZendRoot.
* @param string|null $zendRoot
* @return string = current zendRoot
*/
public static function ZendRoot($zendRoot=null)
{
if ($zendRoot != null)
{
self::$ZendRoot = $zendRoot;
}
return self::$ZendRoot;
}
/**
* LookupSeparator.
*
* Sets the separator character on the first call and returns the
* current value of the directory separator.
* @return string $Separator = current directory path separator
*/
public static function LookupSeparator()
{
if (self::$Separator == '')
{
self::$Separator = (substr(strtolower(trim(php_uname())), 0, 3) == 'win') ? '/' : DIRECTORY_SEPARATOR;
}
return self::$Separator;
}
}
/**
* __autoload.
*
* Searches for the required class file and loads if found. Emits critical error if not.
* This has to be a global function. Uses the Autoload static class to
* do the work.
* @param string $class = the class name to load
* @return none
*/
function __autoload($class)
{
if (Autoload::LoadClass($class))
{
return;
}
//
// Not found, so emit an error and cause a critical error halt
//
trigger_error(sprintf('%s was not found.', $class), E_USER_ERROR);
}