classloader.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*******************************************************************************
00004  * Aliro - the modern, accessible content management system
00005  *
00006  * Aliro is open source software, free to use, and licensed under GPL.
00007  * You can find the full licence at http://www.gnu.org/copyleft/gpl.html GNU/GPL
00008  *
00009  * The author freely draws attention to the fact that Aliro derives from Mambo,
00010  * software that is controlled by the Mambo Foundation.  However, this section
00011  * of code is totally new.  If it should contain any fragments that are similar
00012  * to Mambo, please bear in mind (1) there are only so many ways to do things
00013  * and (2) the author of Aliro is also the author and copyright owner for large
00014  * parts of Mambo 4.6.
00015  *
00016  * Tribute should be paid to all the developers who took Mambo to the stage
00017  * it had reached at the time Aliro was created.  It is a feature rich system
00018  * that contains a good deal of innovation.
00019  *
00020  * Your attention is also drawn to the fact that Aliro relies on other items of
00021  * open source software, which is very much in the spirit of open source.  Aliro
00022  * wishes to give credit to those items of code.  Please refer to
00023  * http://aliro.org/credits for details.  The credits are not included within
00024  * the Aliro package simply to avoid providing a marker that allows hackers to
00025  * identify the system.
00026  *
00027  * Copyright in this code is strictly reserved by its author, Martin Brampton.
00028  * If it seems appropriate, the copyright will be vested in the Aliro Organisation
00029  * at a suitable time.
00030  *
00031  * Copyright (c) 2007 Martin Brampton
00032  *
00033  * http://aliro.org
00034  *
00035  * counterpoint@aliro.org
00036  *
00037  * This file is mainly to hold the user side smart class mapper, but it also has
00038  * the aliroDebug class.  The latter is a simple singleton class that handles
00039  * debug data.  It receives debut data from the class mapper and the database,
00040  * and could be used by other functions.
00041  *
00042  * The smartClassMapper is used to find classes.  It has written into it the
00043  * locations for permanent classes on the user side, and separately holds locations
00044  * for external classes from third parties outside the Aliro project.  These are
00045  * from other open source projects.  The third source for class information is the
00046  * database, which contains details of installed classes.  On the user side, classes
00047  * that are in a file with the same name as the class are found automatically.
00048  *
00049  */
00050 
00051 function __autoload ($classname) {
00052     if (HTMLPurifier_Bootstrap::autoload($classname)) return true;
00053     aliro::getInstance()->requireClass($classname);
00054 }
00055 
00056 // Debug Data Handler
00057 class aliroDebug {
00058     private static $instance = __CLASS__;
00059     
00060     private $debug_log = array();
00061 
00062     private function __construct () { /* Enforce singleton */ }
00063 
00064     private function __clone () { /* Enforce singleton */ }
00065 
00066     public static function getInstance () {
00067         return is_object(self::$instance) ? self::$instance : (self::$instance = new self::$instance);
00068     }
00069 
00070     public function setDebugData ($info) {
00071         $this->debug_log[] = $info;
00072     }
00073 
00074     public function getLogged () {
00075         $text = '<h4>'.count($this->debug_log).' classes loaded</h4>';
00076         foreach ($this->debug_log as $k=>$class) $text .= "\n".($k+1)."<br />".$class.'<hr />';
00077         return $text;
00078     }
00079 
00080 }
00081 
00082 class smartClassMapper extends cachedSingleton {
00083 
00084     private static $instance = __CLASS__;
00085 
00086     protected $dynamap = array();
00087     protected $debug_log = array();
00088     protected $timer = null;
00089     protected $populating = false;
00090 
00091     protected $classmap = array (
00092     'mamboCore' => 'aliroCore',
00093     'aliroAbstractDatabase' => 'aliroDatabase',
00094     'aliroDatabaseHandler' => 'aliroDatabase',
00095     'aliroCoreDatabase' => 'aliroDatabase',
00096     'database' => 'aliroDatabase',
00097     'mamboDatabase' => 'aliroDatabase',
00098     'joomlaDatabase' => 'aliroDatabase',
00099     'mosDBTable' => 'aliroDatabaseRow',
00100     'aliroDBRowFactory' => 'aliroDatabaseRow',
00101     'aliroUserTemplateBase' => 'aliroTemplateBase',
00102     'aliroMainTemplateBase' => 'aliroTemplateBase',
00103     'mosAdminMenus' => 'compatibilityClasses',
00104     'mosToolBar' => 'compatibilityClasses',
00105     'mosPathway' => 'aliroPathway',
00106     'mosUser' => 'aliroUser',
00107     'mosMailer' => 'aliroMailer',
00108     'aliroSimpleCache' => 'aliroCache',
00109     'mosCache' => 'aliroCache',
00110     'aliroFolderHandler' => 'aliroFolder',
00111     'aliroSessionFactory' => 'aliroSession',
00112     'aliroSessionData' => 'aliroSession',
00113     'aliroExtension' => 'aliroExtensionHandler',
00114     'aliroCommonExtHandler' => 'aliroExtensionHandler',
00115     'aliroUserPageNav' => 'aliroPageNav',
00116     'aliroAbstractPageNav' => 'aliroPageNav',
00117     'mosPageNav' => 'aliroPageNav',
00118     'aliroDirectory' => 'aliroFileManager',
00119     'mosController' => 'mosRenderer',
00120     'mosCommand' => 'mosRenderer',
00121     'mosView' => 'mosRenderer',
00122     'aliroMenuItem' => 'aliroMenuHandler',
00123     'mosTabs' => 'aliroTabs',
00124     'aliroComponentHandler' => 'aliroComponent',
00125     'aliroFriendlyBase' => 'aliroComponentManager',
00126     'aliroComponentUserManager' => 'aliroComponentManager',
00127     'aliroUserScreenArea' => 'aliroScreenArea',
00128     'aliroAdminScreenArea' => 'aliroScreenArea',
00129     'aliroMambotHandler' => 'aliroMambot',
00130     'mosMambotHandler' => 'aliroMambot',
00131     'aliroModuleHandler' => 'aliroModule',
00132     'aliroXMLParamsDefault' => 'aliroXMLParams',
00133     //'aliroBasicXML' => 'aliroXML',
00134     //'aliroXMLDescription' => 'aliroXML',
00135     //'aliroXMLDefaultParams' => 'aliroXML',
00136     'aliroAdminParameters' => 'aliroParameters',
00137     'mosAdminParameters' => 'aliroParameters',
00138     'aliroSpecialAdminParameters' => 'aliroParameters',
00139     'mosParameters' => 'aliroParameters',
00140     'aliroLoginDetails' => 'aliroAuthenticator',
00141     'aliroUserAuthenticator' => 'aliroAuthenticator',
00142     'aliroAdminAuthenticator' => 'aliroAuthenticator',
00143     'JApplicationHelper' => 'aliroJoomla',
00144     'JFactory' => 'aliroJoomla',
00145     'JRequest' => 'aliroJoomla',
00146     'JError' => 'aliroJoomla'
00147     );
00148 
00149     protected $extmap = array(
00150     'ArchieHTTP' => 'ArchieHTTP',
00151     'UniversalFeedCreator' => 'feedcreator.class',
00152     'htmlMimeMail5' => 'htmlMimeMail5',
00153     'Mail_MIMEPart' => 'mimePart',
00154     'Mail_RFC822' => 'RFC822',
00155     'smpt' => 'smtp',
00156     'HTMLPurifier' => 'HTMLPurifier',
00157     'HTMLPurifier_Config' => 'HTMLPurifier',
00158     'vCard' => 'vCard',
00159     'PclZip' => 'pclzip.lib',
00160     'Archive_Tar' => 'Tar',
00161     'PEAR' => 'PEAR',
00162     'HTMLPurifier_AttrTransform_ScriptRequired' => 'HTMLPurifier/HTMLPurifier_Script_Extension',
00163     'HTMLPurifier_HTMLModule_Scripting' => 'HTMLPurifier/HTMLPurifier_Script_Extension',
00164     'charsetmapping' => 'ConvertTables/charsetmapping',
00165     'PHPGettextFile' => 'phpgettext/phpgettext.file',
00166     'PHPGettextFilePOT' => 'phpgettext/phpgettext.file.pot',
00167     'PHPGettextFilePO' => 'phpgettext/phpgettext.file.po',
00168     'PHPGettextFileGLO' => 'phpgettext/phpgettext.file.glo',
00169     'PHPGettextFileMO' => 'phpgettext/phpgettext.file.mo',
00170     'PHPGettext' => 'phpgettext/phpgettext.class',
00171     'PHPGettextAdmin' => 'phpgettext/phpgettext.admin',
00172     'PHPGettext_Message' => 'phpgettext/phpgettext.message',
00173     'aliroUnaccent' => 'aliroUnaccent',
00174     'ConvertCharset' => 'ConvertCharset',
00175     'zipfile' => 'zipfile'
00176     );
00177 
00178     protected function __construct () {
00179         $this->timer = new aliroProfiler('Time so far');
00180     }
00181 
00182     public static function getInstance () {
00183         if (!is_object(self::$instance)) {
00184             self::$instance = parent::getCachedSingleton(self::$instance);
00185             self::$instance->reset();
00186         }
00187         self::$instance->checkDynamic();
00188         return self::$instance;
00189     }
00190 
00191     protected function checkDynamic () {
00192         if (aliro::getInstance()->installed AND 0 == count($this->dynamap) AND !$this->populating) {
00193             $this->populating = true;
00194             $this->populateMap();
00195             $this->populating = false;
00196             $this->cacheNow();
00197         }
00198     }
00199     
00200     public function reset () {
00201         $this->timer->reset();
00202     }
00203 
00204     public function clearCache () {
00205         $this->dynamap = array();
00206         $this->populateMap();
00207         parent::clearCache();
00208     }
00209 
00210     public function __print () {
00211         return sprintf(T_('SmartClassMapper, %s dynamic items, % logs'), count($this->dynamap), count($this->debug_log));
00212     }
00213     
00214     protected function populateMap () {
00215         $database = aliroCoreDatabase::getInstance();
00216         $database->setQuery('SELECT * FROM #__classmap WHERE side != "admin"');
00217         $maps = $database->loadObjectList();
00218         if ($maps) foreach ($maps as $map) {
00219             switch ($map->type) {
00220                 case 'component':
00221                     $path = 'components/'.$map->formalname.'/';
00222                     break;
00223                 case 'module':
00224                     $path = 'modules/'.$map->formalname.'/';
00225                     break;
00226                 case 'mambot':
00227                     $path = 'mambots/'.$map->formalname.'/';
00228                     break;
00229                 case 'template':
00230                     $path = 'templates/'.$map->formalname.'/';
00231                     break;
00232                 default: continue;
00233             }
00234             $this->saveMap($path, $map);
00235         }
00236     }
00237     
00238     public function timeSoFar () {
00239         return $this->timer->mark('seconds');
00240     }
00241 
00242     protected function getClassPath ($classname) {
00243         aliroDebug::getInstance()->setDebugData (sprintf('About to load %s, current used memory %s', $classname, (is_callable('memory_get_usage') ? memory_get_usage() : T_('not known')).$this->timeSoFar()));
00244         $base = criticalInfo::getInstance()->class_base.'/';
00245         if (isset($this->dynamap[$classname])) return $base.$this->dynamap[$classname].'.php';
00246         if (isset($this->classmap[$classname])) return $base.'classes/'.$this->classmap[$classname].'.php';
00247         if (isset($this->extmap[$classname])) return $base.'extclasses/'.$this->extmap[$classname].'.php';
00248         if (file_exists($base.'classes/'.$classname.'.php')) return $base.'classes/'.$classname.'.php';
00249         return '';
00250     }
00251 
00252     public function requireClass ($classname) {
00253         $path = $this->getClassPath($classname);
00254         if ($path AND file_exists($path)) require_once($path);
00255         else {
00256             $message = sprintf('Class %s not found, trying with path = %s', $classname, $path);
00257             trigger_error($message);
00258         }
00259     }
00260     
00261     public function classExists ($classname) {
00262         return $this->getClassPath($classname) ? true : false;
00263     }
00264 
00265     protected function saveMap ($path, $map) {
00266         $path .= $map->filename;
00267         $map->classname = trim($map->classname);
00268         if (false !== strpos($map->classname, '..')) {
00269             var_dump($map);
00270             die(T_('Class mapping includes illegal "..".'));
00271         }
00272         if (!isset($this->dynamap[$map->classname])) $this->dynamap[$map->classname] = $path;
00273         else trigger_error (sprintf('Class %s defined in %s but already defined in %s',$map->classname, $path, $this->dynamap[$map->classname]));
00274     }
00275 
00276 }

Generated on Wed May 14 13:01:56 2008 for ALIRO by  doxygen 1.5.5