User:Woozle/ModLoader/archive/libmgr.php: Difference between revisions

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
version from data/mw/extensions
 
version from /data/lib/php -- clearly the more current one
Line 13: Line 13:
     2011-09-15 Add() now has a "force" parameter to override an existing entry.
     2011-09-15 Add() now has a "force" parameter to override an existing entry.
       By default, it does not replace existing entries.
       By default, it does not replace existing entries.
    2012-04-18 class autoload registration
*/
*/


define('LIBMGR',1);
define('LIBMGR',1);
define('LIBMGR_VER','2011-09-15');
define('LIBMGR_VER','2012-04-18');
 
spl_autoload_register(__NAMESPACE__ .'\clsLibMgr::LoadClass');


class clsLibMgr {
class clsLibMgr {
     private static $vList;
     private static $vList;
     private static $vLoaded;
     private static $vLoaded;
    private static $arClasses;
     public static $Log;
     public static $Log;


Line 46: Line 50:
if ($doChg) {
if ($doChg) {
    self::$vList[$iName] = $iSpec;
    self::$vList[$iName] = $iSpec;
}
    }
    public static function AddClass($iClass, $iModule) {
self::$arClasses[$iClass] = $iModule;
    }
    public static function LoadClass($iClass) {
//echo 'CLASS ['.$iClass.'] requested.<br>';
if (ArrayHas(self::$arClasses,$iClass)) {
    $strMod = self::$arClasses[$iClass];
    self::Load($strMod,__FILE__,__LINE__);
//echo 'LOADED.<br>';
} else {
//     echo '<br>CLASS ['.$iClass.'] has not been registered.<br><pre>';
//     throw new exception('Unregistered class requested.');
}
}
     }
     }
     public static function Load($iName,$iFile,$iLine) {
     public static function Load($iName,$iFile,$iLine) {
if (isset(self::$vList[$iName])) {
if (ArrayHas(self::$vList,$iName)) {
    if (self::IsLoaded($iName)) {
    if (self::IsLoaded($iName)) {
self::AddLog('Duplicate load of </b>'.$iName.'</b>');
self::AddLog('Duplicate load of </b>'.$iName.'</b>');
    } else {
    } else {
self::AddLog('Loading </b>'.$iName.'</b> as ['.self::$vList[$iName].']');
$fsModule = self::$vList[$iName];
self::AddLog('Loading </b>'.$iName.'</b> as ['.$fsModule.']');
try {
try {
    require_once self::$vList[$iName];
    require_once $fsModule;
} catch(Exception $e) {
} catch(Exception $e) {
    $err = "LibMgr could not load [$iName]; error: <b>".$e->getMessage().'</b>';
    echo "LibMgr could not load module [$iName] from [$fsModule]; error: <b>".$e->getMessage().'</b>';


    throw new exception($err);
    throw new exception('Module file could not be loaded.');
}
}
self::$vLoaded[$iName] = TRUE;
self::$vLoaded[$iName] = TRUE;
Line 68: Line 87:
     }
     }
     protected static function IsLoaded($iName) {
     protected static function IsLoaded($iName) {
if (isset(self::$vLoaded[$iName])) {
if (ArrayHas(self::$vLoaded,$iName)) {
    return self::$vLoaded[$iName];
    return self::$vLoaded[$iName];
} else {
} else {
Line 86: Line 105:
}
}
clsLibMgr::Init();
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>
</php>

Revision as of 14:19, 1 January 2013

About

There's probably a better way of doing this -- perhaps even just a systematic way of loading libraries. I have to rethink it at some point.

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 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 {
		    require_once $fsModule;
		} 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>