* @copyright Copyright (c) 2008-9 TypeOneError Studios (http://www.typeoneerror.com) * @license MIT ~ http://www.opensource.org/licenses/mit-license.php * @version $Id$ * @link http://typeoneerror.com/asra * @category Asra * @package Core */ /** * Handles the auto-loading of classes and files. * * @author Ben Borowski * @copyright Copyright (c) 2008-9 TypeOneError Studios (http://www.typeoneerror.com) * @license MIT ~ http://www.opensource.org/licenses/mit-license.php * @link http://typeoneerror.com/asra * @category Asra * @package Core */ final class Asra_Core_Loader { //--------------------------------------- // PRIVATE VARIABLES //--------------------------------------- /** * Load errors. * * @param string */ private static $__error; //--------------------------------------- // CONSTRUCTOR //--------------------------------------- /** * Dont't allow clones */ // @codeCoverageIgnoreStart final private function __clone() { } /** * Don't allow construction */ final private function __construct() { } // @codeCoverageIgnoreEnd //--------------------------------------- // METHODS //--------------------------------------- /** * Performs the auto-load of classes. * * @param string $className Name of class to load file for. * @return void */ public static function autoload($className) { $autoload = false; // -- if the actual class name exists, bail if (class_exists($className, $autoload) || interface_exists($className, $autoload)) { return; } // -- convert to pear style class name $file = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; // -- include the converted file @require_once $file; // -- one more test if (!class_exists($className, $autoload) && !interface_exists($className, $autoload)) { require_once 'Asra/Core/Exception.php'; throw new Asra_Core_Exception("File \"{$file}\" does not exist or class \"{$className}\" was not found in the file"); } } /** * Registers the autoload function for use. * * @return void */ public static function init() { // @codeCoverageIgnoreStart if (!function_exists('spl_autoload_register')) { require_once 'Asra/Core/Exception.php'; throw new Asra_Core_Exception('Required autoload function `spl_autoload_register` does not exist in PHP version ' . phpversion()); } // @codeCoverageIgnoreEnd spl_autoload_register(array('Asra_Core_Loader', 'autoload')); } /** * Load a configuration file. * * @param string $file Path to config file. * @param string $type Type of config file. * @return array Parsed configuration settings. */ public static function loadConfig($file, $type) { if (!file_exists($file)) { require_once 'Asra/Core/Exception.php'; throw new Asra_Core_Exception("{$type} file was not present at specified location: {$file}"); } /** * sadly, need to hijack the error handler here because * we want to throw an error if the parsing failed */ self::$__error = null; set_error_handler(array('Asra_Core_Loader', '__loadConfigError')); $parse = parse_ini_file($file, true); restore_error_handler(); if (!is_array($parse) || is_null($parse) || !is_null(self::$__error)) { require_once 'Asra/Core/Exception.php'; throw new Asra_Core_Exception("{$type} file at {$file} appears to be malformed: " . self::$__error); } return $parse; } /** * Handle any errors while loading config. * * @param int $errno * @param string $errstr * @param string $errfile * @param int $errline * @return void */ private static function __loadConfigError($errno, $errstr, $errfile, $errline) { self::$__error = $errstr; } }