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 14:17, 1 January 2013 by Woozle (talk | contribs) (version from data/mw/extensions)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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.
  • /

define('LIBMGR',1); define('LIBMGR_VER','2011-09-15');

class clsLibMgr {

   private static $vList;
   private static $vLoaded;
   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 Load($iName,$iFile,$iLine) {

if (isset(self::$vList[$iName])) { if (self::IsLoaded($iName)) { self::AddLog('Duplicate load of '.$iName.''); } else { self::AddLog('Loading '.$iName.' as ['.self::$vList[$iName].']'); try { require_once self::$vList[$iName]; } catch(Exception $e) { $err = "LibMgr could not load [$iName]; error: ".$e->getMessage().'';

throw new exception($err); } self::$vLoaded[$iName] = TRUE; } } else { echo 'UNKNOWN LIBRARY '.$iName.' REQUESTED by '.$iFile.' line '.$iLine; }

   }
   protected static function IsLoaded($iName) {

if (isset(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(); </php>