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     aliro::getInstance()->requireClass($classname);
00053 }
00054 
00055 // Debug Data Handler
00056 class aliroDebug {
00057     private static $instance = __CLASS__;
00058     
00059     private $debug_log = array();
00060 
00061     private function __construct () { /* Enforce singleton */ }
00062 
00063     private function __clone () { /* Enforce singleton */ }
00064 
00065     public static function getInstance () {
00066         return is_object(self::$instance) ? self::$instance : (self::$instance = new self::$instance);
00067     }
00068 
00069     public function setDebugData ($info) {
00070         $this->debug_log[] = $info;
00071     }
00072 
00073     public function getLogged () {
00074         $text = '<h4>'.count($this->debug_log).' classes loaded</h4>';
00075         foreach ($this->debug_log as $k=>$class) $text .= "\n".($k+1)."<br />".$class.'<hr />';
00076         return $text;
00077     }
00078 
00079 }
00080 
00081 class smartClassMapper extends cachedSingleton {
00082 
00083     private static $instance = __CLASS__;
00084 
00085     protected $dynamap = array();
00086     protected $debug_log = array();
00087     protected $timer = null;
00088     protected $populating = false;
00089 
00090     protected $classmap = array (
00091     'mamboCore' => 'aliroCore',
00092     'aliroAbstractDatabase' => 'aliroDatabase',
00093     'aliroDatabaseHandler' => 'aliroDatabase',
00094     'aliroCoreDatabase' => 'aliroDatabase',
00095     'database' => 'aliroDatabase',
00096     'mamboDatabase' => 'aliroDatabase',
00097     'joomlaDatabase' => 'aliroDatabase',
00098     'mosDBTable' => 'aliroDatabaseRow',
00099     'aliroDBRowFactory' => 'aliroDatabaseRow',
00100     'aliroUserTemplateBase' => 'aliroTemplateBase',
00101     'aliroMainTemplateBase' => 'aliroTemplateBase',
00102     'mosAdminMenus' => 'compatibilityClasses',
00103     'mosToolBar' => 'compatibilityClasses',
00104     'mosPathway' => 'aliroPathway',
00105     'mosUser' => 'aliroUser',
00106     'mosMailer' => 'aliroMailer',
00107     'aliroSimpleCache' => 'aliroCache',
00108     'mosCache' => 'aliroCache',
00109     'aliroFolderHandler' => 'aliroFolder',
00110     'aliroSessionFactory' => 'aliroSession',
00111     'aliroSessionData' => 'aliroSession',
00112     'aliroExtension' => 'aliroExtensionHandler',
00113     'aliroCommonExtHandler' => 'aliroExtensionHandler',
00114     'aliroUserPageNav' => 'aliroPageNav',
00115     'aliroAbstractPageNav' => 'aliroPageNav',
00116     'mosPageNav' => 'aliroPageNav',
00117     'aliroDirectory' => 'aliroFileManager',
00118     'mosController' => 'mosRenderer',
00119     'mosCommand' => 'mosRenderer',
00120     'mosView' => 'mosRenderer',
00121     'aliroMenuItem' => 'aliroMenuHandler',
00122     'mosTabs' => 'aliroTabs',
00123     'aliroComponentHandler' => 'aliroComponent',
00124     'aliroFriendlyBase' => 'aliroComponentManager',
00125     'aliroComponentUserManager' => 'aliroComponentManager',
00126     'aliroUserScreenArea' => 'aliroScreenArea',
00127     'aliroAdminScreenArea' => 'aliroScreenArea',
00128     'aliroMambotHandler' => 'aliroMambot',
00129     'mosMambotHandler' => 'aliroMambot',
00130     'aliroModuleHandler' => 'aliroModule',
00131     'aliroXMLParamsDefault' => 'aliroXMLParams',
00132     //'aliroBasicXML' => 'aliroXML',
00133     //'aliroXMLDescription' => 'aliroXML',
00134     //'aliroXMLDefaultParams' => 'aliroXML',
00135     'aliroAdminParameters' => 'aliroParameters',
00136     'mosAdminParameters' => 'aliroParameters',
00137     'aliroSpecialAdminParameters' => 'aliroParameters',
00138     'mosParameters' => 'aliroParameters',
00139     'aliroLoginDetails' => 'aliroAuthenticator',
00140     'aliroUserAuthenticator' => 'aliroAuthenticator',
00141     'aliroAdminAuthenticator' => 'aliroAuthenticator',
00142     'JApplicationHelper' => 'aliroJoomla',
00143     'JFactory' => 'aliroJoomla',
00144     'JRequest' => 'aliroJoomla',
00145     'JError' => 'aliroJoomla'
00146     );
00147 
00148     protected $extmap = array(
00149     'ArchieHTTP' => 'ArchieHTTP',
00150     'UniversalFeedCreator' => 'feedcreator.class',
00151     'htmlMimeMail5' => 'htmlMimeMail5',
00152     'Mail_MIMEPart' => 'mimePart',
00153     'Mail_RFC822' => 'RFC822',
00154     'smpt' => 'smtp',
00155     'HTMLPurifier' => 'HTMLPurifier',
00156     'HTMLPurifier_Config' => 'HTMLPurifier',
00157     'vCard' => 'vCard',
00158     'PclZip' => 'pclzip.lib',
00159     'Archive_Tar' => 'Tar',
00160     'PEAR' => 'PEAR',
00161     'HTMLPurifier_AttrTransform_ScriptRequired' => 'HTMLPurifier/HTMLPurifier_Script_Extension',
00162     'HTMLPurifier_HTMLModule_Scripting' => 'HTMLPurifier/HTMLPurifier_Script_Extension',
00163     'charsetmapping' => 'ConvertTables/charsetmapping',
00164     'PHPGettextFile' => 'phpgettext/phpgettext.file',
00165     'PHPGettextFilePOT' => 'phpgettext/phpgettext.file.pot',
00166     'PHPGettextFilePO' => 'phpgettext/phpgettext.file.po',
00167     'PHPGettextFileGLO' => 'phpgettext/phpgettext.file.glo',
00168     'PHPGettextFileMO' => 'phpgettext/phpgettext.file.mo',
00169     'PHPGettext' => 'phpgettext/phpgettext.class',
00170     'PHPGettextAdmin' => 'phpgettext/phpgettext.admin',
00171     'PHPGettext_Message' => 'phpgettext/phpgettext.message',
00172     'aliroUnaccent' => 'aliroUnaccent',
00173     'ConvertCharset' => 'ConvertCharset',
00174     'zipfile' => 'zipfile'
00175     );
00176 
00177     protected function __construct () {
00178         $this->timer = new aliroProfiler('Time so far');
00179     }
00180 
00181     public static function getInstance () {
00182         if (!is_object(self::$instance)) {
00183             self::$instance = parent::getCachedSingleton(self::$instance);
00184             self::$instance->reset();
00185         }
00186         self::$instance->checkDynamic();
00187         return self::$instance;
00188     }
00189 
00190     protected function checkDynamic () {
00191         if (aliro::getInstance()->installed AND 0 == count($this->dynamap) AND !$this->populating) {
00192             $this->populating = true;
00193             $this->populateMap();
00194             $this->populating = false;
00195             $this->cacheNow();
00196         }
00197     }
00198     
00199     public function reset () {
00200         $this->timer->reset();
00201     }
00202 
00203     public function clearCache () {
00204         $this->dynamap = array();
00205         $this->populateMap();
00206         parent::clearCache();
00207     }
00208 
00209     public function __print () {
00210         return sprintf(T_('SmartClassMapper, %s dynamic items, % logs'), count($this->dynamap), count($this->debug_log));
00211     }
00212     
00213     protected function populateMap () {
00214         $database = aliroCoreDatabase::getInstance();
00215         $database->setQuery('SELECT * FROM #__classmap WHERE side != "admin"');
00216         $maps = $database->loadObjectList();
00217         if ($maps) foreach ($maps as $map) {
00218             switch ($map->type) {
00219                 case 'component':
00220                     $path = 'components/'.$map->formalname.'/';
00221                     break;
00222                 case 'module':
00223                     $path = 'modules/'.$map->formalname.'/';
00224                     break;
00225                 case 'mambot':
00226                     $path = 'mambots/'.$map->formalname.'/';
00227                     break;
00228                 case 'template':
00229                     $path = 'templates/'.$map->formalname.'/';
00230                     break;
00231                 default: continue;
00232             }
00233             $this->saveMap($path, $map);
00234         }
00235     }
00236     
00237     public function timeSoFar () {
00238         return $this->timer->mark('seconds');
00239     }
00240 
00241     protected function getClassPath ($classname) {
00242         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()));
00243         $base = criticalInfo::getInstance()->class_base.'/';
00244         if (isset($this->dynamap[$classname])) return $base.$this->dynamap[$classname].'.php';
00245         if (isset($this->classmap[$classname])) return $base.'classes/'.$this->classmap[$classname].'.php';
00246         if (isset($this->extmap[$classname])) return $base.'extclasses/'.$this->extmap[$classname].'.php';
00247         if (file_exists($base.'classes/'.$classname.'.php')) return $base.'classes/'.$classname.'.php';
00248         return '';
00249     }
00250 
00251     public function requireClass ($classname) {
00252         $path = $this->getClassPath($classname);
00253         if ($path AND file_exists($path)) require_once($path);
00254         else {
00255             $message = sprintf('Class %s not found, trying with path = %s', $classname, $path);
00256             trigger_error($message);
00257         }
00258     }
00259     
00260     public function classExists ($classname) {
00261         return $this->getClassPath($classname) ? true : false;
00262     }
00263 
00264     protected function saveMap ($path, $map) {
00265         $path .= $map->filename;
00266         $map->classname = trim($map->classname);
00267         if (false !== strpos($map->classname, '..')) {
00268             var_dump($map);
00269             die(T_('Class mapping includes illegal "..".'));
00270         }
00271         if (!isset($this->dynamap[$map->classname])) $this->dynamap[$map->classname] = $path;
00272         else trigger_error (sprintf('Class %s defined in %s but already defined in %s',$map->classname, $path, $this->dynamap[$map->classname]));
00273     }
00274 
00275 }

Generated on Thu Apr 17 13:03:27 2008 for ALIRO by  doxygen 1.5.5