Changeset 579

Show
Ignore:
Timestamp:
04/19/07 23:08:58 (1 year ago)
Author:
mikey
Message:

the stubClassLoader::load() method now throws a stubClassNotFoundException in case the class to load can not be found instead of just bailing out with a useless error message

Files:

Legend:

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

    r534 r579  
    1212 * This interface must reside here because the stubClassLoader uses it as type hint. 
    1313 * 
    14  * @author   Frank Kleine <mikey@stubbles.net> 
    15  * @author   Stephan Schmidt <schst@stubbles.net> 
    1614 * @package  stubbles 
    1715 */ 
     
    2119     * sets the namespace where this classloader is responsible for 
    2220     * 
    23      * @param  string $namespace 
     21     * @param  string $namespace 
    2422     */ 
    2523    public function setNamespace($namespace); 
     
    3634     * 
    3735     * @param   string  $fqClassName  the full qualified class name of the class to load 
    38      * @throws  Exception 
     36     * @throws  stubClassNotFoundException 
    3937     */ 
    4038    public function load($fqClassName); 
     39} 
     40/** 
     41 * Exception thrown if the class loader can not find the desired class. 
     42 *  
     43 * This exception must reside here because the stubClassLoader uses it when a class can not be loaded. 
     44 *  
     45 * @package  stubbles 
     46 */ 
     47class stubClassNotFoundException extends Exception 
     48{ 
     49    /** 
     50     * full qualified class name of the class that was not found 
     51     * 
     52     * @var  string 
     53     */ 
     54    protected $fqClassName; 
     55 
     56    /** 
     57     * constructor 
     58     * 
     59     * @param  string  $fqClassName  full qualified class name of the class that was not found 
     60     */ 
     61    public function __construct($fqClassName) 
     62    { 
     63        $this->fqClassName = $fqClassName; 
     64        $caller  = debug_backtrace(); 
     65        $message = 'The class ' . $this->fqClassName . ' loaded in ' . $caller[1]['file'] . ' on line ' . $caller[1]['line'] . ' was not found.'; 
     66        parent::__construct($message); 
     67    } 
     68 
     69    /** 
     70     * returns the full qualified class name of the class that was not found 
     71     * 
     72     * @return  string 
     73     */ 
     74    public function getNotFoundClassName() 
     75    { 
     76        return $this->fqClassName; 
     77    } 
     78 
     79    /** 
     80     * returns a string representation of the class 
     81     *  
     82     * The result is a short but informative representation about the class and 
     83     * its values. Per default, this method returns: 
     84     * <code> 
     85     * net.stubbles.stubClassNotFoundException { 
     86     *     message(string): The class example.Foo loaded in bar.php on line 6 was not found. 
     87     *     classname(string): example.Foo 
     88     *     file(string): stubClassLoader.php 
     89     *     line(integer): 179 
     90     *     code(integer): 0 
     91     * } 
     92     * </code> 
     93     * 
     94     * @return  string 
     95     */ 
     96    public function __toString() 
     97    { 
     98        $string  = "net.stubbles.stubClassNotFoundException {\n"; 
     99        $string .= '    message(string): ' . $this->getMessage() . "\n"; 
     100        $string .= '    classname(string): ' . $this->fqClassName . "\n"; 
     101        $string .= '    file(string): ' . $this->getFile() . "\n"; 
     102        $string .= '    line(integer): ' . $this->getLine() . "\n"; 
     103        $string .= '    code(integer): ' . $this->getCode() . "\n"; 
     104        $string .= "}\n"; 
     105        return $string; 
     106    } 
    41107} 
    42108/** 
     
    57123     * @var  array<string,string> 
    58124     */ 
    59     private static $classNames          = array('net.stubbles.stubClassLoader'        => 'stubClassLoader', 
    60                                                 'net.stubbles.stubForeignClassLoader' => 'stubForeignClassLoader' 
     125    private static $classNames          = array('net.stubbles.stubClassLoader'            => 'stubClassLoader', 
     126                                                'net.stubbles.stubClassNotFoundException' => 'stubClassNotFoundException', 
     127                                                'net.stubbles.stubForeignClassLoader'     => 'stubForeignClassLoader' 
    61128                                          ); 
    62129    /** 
     
    78145     * on the number of arguments. 
    79146     * 
    80      * @param  string  list of class names to load 
     147     * @param   string  list of class names to load 
     148     * @throws  stubClassNotFoundException 
    81149     */ 
    82150    public static function load() 
     
    88156        } 
    89157 
    90         $files   = array(); 
    91         $dirName = self::getSourcePath(); 
    92158        foreach ($classNames as $fqClassName) { 
    93159            $nqClassName = self::getNonQualifiedClassName($fqClassName); 
     
    108174            } 
    109175            if (null === $uri) { 
    110                 $uri = $dirName . '/' .  self::mapClassname($fqClassName); 
    111             } 
    112             require $uri; 
     176                $uri = stubConfig::getSourcePath() . DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR .  str_replace('.', DIRECTORY_SEPARATOR, $fqClassName) . '.php'; 
     177            } 
     178             
     179            if ((@include $uri) == false) { 
     180                throw new stubClassNotFoundException($fqClassName); 
     181            } 
    113182 
    114183            if (method_exists($nqClassName, '__static') == true) { 
     
    143212 
    144213        return null; 
    145     } 
    146  
    147     /** 
    148      * returns the path to the source files 
    149      * 
    150      * @return  string 
    151      */ 
    152     public static function getSourcePath() 
    153     { 
    154         if (basename(__FILE__) != __CLASS__ . '.php') { 
    155             return realpath(dirname(__FILE__) . '/../src/main/php/'); 
    156         } else { 
    157             return realpath(dirname(__FILE__) . '/../../'); 
    158         } 
    159214    } 
    160215 
     
    183238        } 
    184239        return null; 
    185     } 
    186  
    187     /** 
    188      * maps a classname to corresponding file name 
    189      * 
    190      * @param   string  $fqClassName  name of class given to load() 
    191      * @return  string  name of class required for load() 
    192      */ 
    193     private static function mapClassname($fqClassName) 
    194     { 
    195         return str_replace('.', '/', $fqClassName) . '.php'; 
    196240    } 
    197241} 
     
    206250                      'net.stubbles.stubSerializableObject', 
    207251                      'net.stubbles.stubSerializedObject' 
    208  
    209252); 
    210253?> 
  • trunk/src/test/php/net/stubbles/stubClassLoaderTestCase.php

    r158 r579  
    2424        $this->assertEqual(stubClassLoader::getNonQualifiedClassName('stubClassLoader'), 'stubClassLoader'); 
    2525    } 
    26      
     26 
    2727    /** 
    2828     * assert that __static is called, but only once 
     
    3939        $this->assertEqual(WithStatic::getCalled(), 1); 
    4040    } 
     41 
     42    /** 
     43     * assert that a stubClassNotFoundException is thrown in case a class can not be loaded 
     44     */ 
     45    public function testClassNotFound() 
     46    { 
     47        $this->expectException('stubClassNotFoundException'); 
     48        stubClassLoader::load('_test.DoesNotExist'); 
     49    } 
    4150} 
    4251?>