Changeset 448

Show
Ignore:
Timestamp:
03/31/07 13:23:35 (1 year ago)
Author:
mikey
Message:

added possibility to load classes from pear or xp-framework via stubbles classloader

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/stubClassLoader.php

    r387 r448  
    55 * @author  Frank Kleine <mikey@stubbles.net> 
    66 */ 
     7/** 
     8 * Interface for class loaders that load classes from foreign namespaces. 
     9 *  
     10 * This interface must reside here because the stubClassLoader uses it as type hint. 
     11 * 
     12 * @package  stubbles 
     13 */ 
     14interface stubForeignClassLoader 
     15{ 
     16    /** 
     17     * sets the namespace where this classloader is responsible for 
     18     * 
     19     * @param  string $namespace 
     20     */ 
     21    public function setNamespace($namespace); 
     22     
     23    /** 
     24     * returns the namespace where this classloader is responsible for 
     25     * 
     26     * @return  string 
     27     */ 
     28    public function getNamespace(); 
     29     
     30    /** 
     31     * loads the given class 
     32     * 
     33     * @param   string  $fqClassName  the full qualified class name of the class to load 
     34     * @throws  Exception 
     35     */ 
     36    public function load($fqClassName); 
     37} 
    738/** 
    839 * Class loader for all stubbles classes. 
     
    2253     * @var  array<string,string> 
    2354     */ 
    24     private static $classNames = array('net.stubbles.stubClassLoader' => 'stubClassLoader'); 
     55    private static $classNames          = array('net.stubbles.stubClassLoader'        => 'stubClassLoader', 
     56                                                'net.stubbles.stubForeignClassLoader' => 'stubForeignClassLoader' 
     57                                          ); 
     58    /** 
     59     * list of foreign class loaders 
     60     * 
     61     * @var  array<stubForeignClassLoader> 
     62     */ 
     63    private static $foreignClassLoaders = array(); 
    2564     
    2665    /** 
     
    5493             
    5594            self::$classNames[$nqClassName] = $fqClassName; 
     95            $foreignNamespace = self::getForeignNamespace($fqClassName); 
     96            if (null != $foreignNamespace) { 
     97                self::$foreignClassLoaders[$foreignNamespace]->load($fqClassName); 
     98                return; 
     99            } 
     100             
    56101            $uri = null; 
    57102            if (stubConfig::useStar() == true && class_exists('StarClassRegistry', false) == true) { 
     
    111156     
    112157    /** 
     158     * registers a foreign class loader 
     159     * 
     160     * @param  stubForeignClassLoader  $foreignClassLoader 
     161     */ 
     162    public static function registerForeignClassLoader(stubForeignClassLoader $foreignClassLoader) 
     163    { 
     164        self::$foreignClassLoaders[$foreignClassLoader->getNamespace()] = $foreignClassLoader; 
     165    } 
     166     
     167    /** 
     168     * checks whether a class resides in a foreign namespace 
     169     * 
     170     * @param   string  $fqClassName  the class to check if it is in a foreign namespace 
     171     * @return  string  the foreign namespace, null if class is not in any one 
     172     */ 
     173    private static function getForeignNamespace($fqClassName) 
     174    { 
     175        foreach (self::$foreignClassLoaders as $foreignNamespace => $foreignClassLoader) { 
     176            if (substr($fqClassName, 0, strlen($foreignNamespace)) == $foreignNamespace) { 
     177                return $foreignNamespace; 
     178            } 
     179        } 
     180         
     181        return null; 
     182    } 
     183     
     184    /** 
    113185     * maps a classname to corresponding file name 
    114186     *