Changeset 579
- Timestamp:
- 04/19/07 23:08:58 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/stubClassLoader.php
r534 r579 12 12 * This interface must reside here because the stubClassLoader uses it as type hint. 13 13 * 14 * @author Frank Kleine <mikey@stubbles.net>15 * @author Stephan Schmidt <schst@stubbles.net>16 14 * @package stubbles 17 15 */ … … 21 19 * sets the namespace where this classloader is responsible for 22 20 * 23 * @param string $namespace21 * @param string $namespace 24 22 */ 25 23 public function setNamespace($namespace); … … 36 34 * 37 35 * @param string $fqClassName the full qualified class name of the class to load 38 * @throws Exception36 * @throws stubClassNotFoundException 39 37 */ 40 38 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 */ 47 class 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 } 41 107 } 42 108 /** … … 57 123 * @var array<string,string> 58 124 */ 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' 61 128 ); 62 129 /** … … 78 145 * on the number of arguments. 79 146 * 80 * @param string list of class names to load 147 * @param string list of class names to load 148 * @throws stubClassNotFoundException 81 149 */ 82 150 public static function load() … … 88 156 } 89 157 90 $files = array();91 $dirName = self::getSourcePath();92 158 foreach ($classNames as $fqClassName) { 93 159 $nqClassName = self::getNonQualifiedClassName($fqClassName); … … 108 174 } 109 175 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 } 113 182 114 183 if (method_exists($nqClassName, '__static') == true) { … … 143 212 144 213 return null; 145 }146 147 /**148 * returns the path to the source files149 *150 * @return string151 */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 }159 214 } 160 215 … … 183 238 } 184 239 return null; 185 }186 187 /**188 * maps a classname to corresponding file name189 *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';196 240 } 197 241 } … … 206 250 'net.stubbles.stubSerializableObject', 207 251 'net.stubbles.stubSerializedObject' 208 209 252 ); 210 253 ?> trunk/src/test/php/net/stubbles/stubClassLoaderTestCase.php
r158 r579 24 24 $this->assertEqual(stubClassLoader::getNonQualifiedClassName('stubClassLoader'), 'stubClassLoader'); 25 25 } 26 26 27 27 /** 28 28 * assert that __static is called, but only once … … 39 39 $this->assertEqual(WithStatic::getCalled(), 1); 40 40 } 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 } 41 50 } 42 51 ?>
