User:Woozle/ModLoader/archive/libmgr.php

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< User:Woozle‎ | ModLoader‎ | archive
Revision as of 20:35, 2 January 2013 by Woozle (talk | contribs) (a bit of error-handling code, and the beginning of a better error-handling system)
Jump to navigation Jump to search

About

There's probably a better way of doing this -- perhaps even just a systematic way of using the existing PHP code-loading functions. I have to rethink it at some point.

The basic idea is that an included file (X) which needs to include other files (Y) might have an idea of where those files are, but the callers might want to override the locations of Y before X tries to load them.

Code

<php> <?php /*

 FILE: libmgr.php
 PURPOSE: class for managing library loading
 HISTORY:
   2009-07-05 Trying to make this usable: shortened method names, added Path(), AddLog()
   2009-10-06 IsLoaded()
   2010-10-06 Log shows if update is not a change
   2011-09-15 Add() now has a "force" parameter to override an existing entry.
     By default, it does not replace existing entries.
   2012-04-18 class autoload registration
  • /

define('LIBMGR',1); define('LIBMGR_VER','2012-04-18');

spl_autoload_register(__NAMESPACE__ .'\clsLibMgr::LoadClass');

class clsLibModule {

   private $strName;
   private $fsModule;	// filespec to loadable module
   private $fsCaller;	// filespec of caller
   private $intCaller;	// line number where called (in caller)

}

class clsLibMgr {

   private static $vList;
   private static $vLoaded;
   private static $arClasses;
   public static $Log;
   public static function Init() {

self::AddLog(__FILE__);

   }
   public static function Add($iName,$iSpec,$iFile,$iLine,$iForce=FALSE) {

$doChg = TRUE; if (isset(self::$vList[$iName])) { $fsCur = self::$vList[$iName]; if ($fsCur == $iSpec) { self::AddLog('Updating '.$iName.' from ['.$fsCur.']: no change'); } else { if ($iForce) { self::AddLog('Changing '.$iName.' from ['.$fsCur.'] to ['.$iSpec.']'); } else { $doChg = FALSE; self::AddLog('Not changing '.$iName.', currently ['.$fsCur.']; discarding ['.$iSpec.']'); } } } else { self::AddLog('Adding '.$iName.' as ['.$iSpec.']'); } self::AddLog(' -- from '.$iFile); if ($doChg) { self::$vList[$iName] = $iSpec; }

   }
   public static function AddClass($iClass, $iModule) {

self::$arClasses[$iClass] = $iModule;

   }
   public static function LoadClass($iClass) {

//echo 'CLASS ['.$iClass.'] requested.
'; if (ArrayHas(self::$arClasses,$iClass)) { $strMod = self::$arClasses[$iClass]; self::Load($strMod,__FILE__,__LINE__); //echo 'LOADED.
'; } else {

// echo '
CLASS ['.$iClass.'] has not been registered.

';
//	    throw new exception('Unregistered class requested.');
	}
    }
    public static function Load($iName,$iFile,$iLine) {
	if (ArrayHas(self::$vList,$iName)) {
	    if (self::IsLoaded($iName)) {
		self::AddLog('Duplicate load of '.$iName.'');
	    } else {
		$fsModule = self::$vList[$iName];
		self::AddLog('Loading '.$iName.' as ['.$fsModule.']');
		try {
		    if (file_exists($fsModule)) {
			require_once $fsModule;
		    } else {
			echo "File $fsModule not found for module $iName.";
		    }
		} catch(Exception $e) {
		    echo "LibMgr could not load module [$iName] from [$fsModule]; error: ".$e->getMessage().'';

		    throw new exception('Module file could not be loaded.');
		}
		self::$vLoaded[$iName] = TRUE;
	    }
	} else {
	    echo 'UNKNOWN LIBRARY '.$iName.' REQUESTED by '.$iFile.' line '.$iLine;
	}
    }
    protected static function IsLoaded($iName) {
	if (ArrayHas(self::$vLoaded,$iName)) {
	    return self::$vLoaded[$iName];
	} else {
	    return FALSE;
	}
    }
    public static function Path($iName) {
	if (isset(self::$vList[$iName])) {
	    return self::$vList[$iName];
	} else {
	    return NULL;
	}
    }
    private static function AddLog($iText) {
	self::$Log .= "\n$iText
"; } } clsLibMgr::Init(); /*---- PURPOSE: like array_key_exists(), but parameters are in a sensible order, and doesn't choke if array is NULL or not set. */ if (!function_exists('ArrayHas')) { function ArrayHas(array $iArr=NULL,$iKey) { if (is_null($iArr)) { return FALSE; } else { return array_key_exists($iKey,$iArr); } } }</php>