Changeset 1114
- Timestamp:
- 12/05/07 18:13:51 (9 months ago)
- Files:
-
- trunk/src/main/php/net/stubbles/reflection/annotations/stubAnnotationCache.php (modified) (6 diffs)
- trunk/src/main/php/net/stubbles/reflection/annotations/stubAnnotationFactory.php (modified) (2 diffs)
- trunk/src/test/php/net/stubbles/reflection/annotations/stubAnnotationFactoryTestCase.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/reflection/annotations/stubAnnotationCache.php
r501 r1114 4 4 * 5 5 * @author Stephan Schmidt <schst@stubbles.net> 6 * @author Frank Kleine <mikey@stubbles.net> 6 7 * @package stubbles 7 8 * @subpackage reflection_annotations … … 67 68 * @param stubAnnotation $annotation optional the annotation to store 68 69 */ 69 public static function put($target, $targetName, $annotationName, stubAnnotation $annotation = null) { 70 if (isset(self::$annotations[$target][$targetName]) == false) { 70 public static function put($target, $targetName, $annotationName, stubAnnotation $annotation = null) 71 { 72 if (isset(self::$annotations[$target][$targetName]) === false) { 71 73 self::$annotations[$target][$targetName] = array(); 72 74 } … … 76 78 self::$annotations[$target][$targetName][$annotationName] = $clone->getSerialized(); 77 79 } else { 78 self::$annotations[$target][$targetName][$annotationName] = null;80 self::$annotations[$target][$targetName][$annotationName] = ''; 79 81 } 82 } 83 84 /** 85 * removes an annotation from the cache 86 * 87 * @param int $target target of the annotation 88 * @param string $targetName name of the target 89 * @param string $annotationName name of the annotation 90 */ 91 public static function remove($target, $targetName, $annotationName) 92 { 93 if (isset(self::$annotations[$target][$targetName]) === false || isset(self::$annotations[$target][$targetName][$annotationName]) === false) { 94 return; 95 } 96 97 unset(self::$annotations[$target][$targetName][$annotationName]); 80 98 } 81 99 … … 88 106 * @return bool 89 107 */ 90 public static function has($target, $targetName, $annotationName) { 108 public static function has($target, $targetName, $annotationName) 109 { 110 if (isset(self::$annotations[$target][$targetName]) === false) { 111 return false; 112 } 113 114 if (isset(self::$annotations[$target][$targetName][$annotationName]) === false) { 115 return false; 116 } 117 118 return self::$annotations[$target][$targetName][$annotationName] !== ''; 119 } 120 121 /** 122 * check, whether an annotation is available in the cache 123 * 124 * @param int $target target of the annotation 125 * @param string $targetName name of the target 126 * @param string $annotationName name of the annotation 127 * @return bool 128 */ 129 public static function hasNot($target, $targetName, $annotationName) 130 { 91 131 if (isset(self::$annotations[$target][$targetName]) == false) { 92 132 return false; … … 97 137 } 98 138 99 return true;139 return self::$annotations[$target][$targetName][$annotationName] === ''; 100 140 } 101 141 … … 108 148 * @return stubAnnotation 109 149 */ 110 public static function get($target, $targetName, $annotationName) { 111 if (self::has($target, $targetName, $annotationName) == true) { 150 public static function get($target, $targetName, $annotationName) 151 { 152 if (self::has($target, $targetName, $annotationName) === true) { 112 153 return clone self::$annotations[$target][$targetName][$annotationName]->getUnserialized(); 113 154 } trunk/src/main/php/net/stubbles/reflection/annotations/stubAnnotationFactory.php
r1072 r1114 55 55 return stubAnnotationCache::get($target, $fileName . '::' . $targetName, $annotationName); 56 56 } 57 58 if (stubAnnotationCache::hasNot($target, $fileName . '::' . $targetName, $annotationName) === true) { 59 throw new ReflectionException('Can not find annotation ' . $annotationName); 60 } 57 61 58 62 $hash = md5($fileName . $comment . $targetName); … … 67 71 68 72 if (isset(self::$annotations[$hash][$annotationName]) === false) { 73 // put null into cache to save that the annotation does not exist 74 stubAnnotationCache::put($target, $fileName . '::' . $targetName, $annotationName); 69 75 throw new ReflectionException('Can not find annotation ' . $annotationName); 70 76 } trunk/src/test/php/net/stubbles/reflection/annotations/stubAnnotationFactoryTestCase.php
r1072 r1114 163 163 { 164 164 // clear the cache from both annotations first 165 stubAnnotationCache:: put(stubAnnotation::TARGET_CLASS, __FILE__ . '1' . '::' . 'MyClass', 'MyAnnotation', null);166 stubAnnotationCache:: put(stubAnnotation::TARGET_CLASS, __FILE__ . '2' . '::' . 'MyClass', 'MyAnnotation', null);165 stubAnnotationCache::remove(stubAnnotation::TARGET_CLASS, __FILE__ . '1' . '::' . 'MyClass', 'MyAnnotation'); 166 stubAnnotationCache::remove(stubAnnotation::TARGET_CLASS, __FILE__ . '2' . '::' . 'MyClass', 'MyAnnotation'); 167 167 168 168 $myAnnotation1 = stubAnnotationFactory::create($this->commentComplex, 'MyAnnotation', stubAnnotation::TARGET_CLASS, 'MyClass', __FILE__ . '1'); 169 169 $this->assertTrue(stubAnnotationCache::has(stubAnnotation::TARGET_CLASS, __FILE__ . '1' . '::' . 'MyClass', 'MyAnnotation')); 170 $this->assertFalse(stubAnnotationCache::hasNot(stubAnnotation::TARGET_CLASS, __FILE__ . '1' . '::' . 'MyClass', 'MyAnnotation')); 170 171 $this->assertFalse(stubAnnotationCache::has(stubAnnotation::TARGET_CLASS, __FILE__ . '2' . '::' . 'MyClass', 'MyAnnotation')); 172 $this->assertFalse(stubAnnotationCache::hasNot(stubAnnotation::TARGET_CLASS, __FILE__ . '2' . '::' . 'MyClass', 'MyAnnotation')); 171 173 $myAnnotation2 = stubAnnotationFactory::create($this->commentComplex, 'MyAnnotation', stubAnnotation::TARGET_CLASS, 'MyClass', __FILE__ . '2'); 172 174 $this->assertTrue(stubAnnotationCache::has(stubAnnotation::TARGET_CLASS, __FILE__ . '1' . '::' . 'MyClass', 'MyAnnotation')); 175 $this->assertFalse(stubAnnotationCache::hasNot(stubAnnotation::TARGET_CLASS, __FILE__ . '1' . '::' . 'MyClass', 'MyAnnotation')); 173 176 $this->assertTrue(stubAnnotationCache::has(stubAnnotation::TARGET_CLASS, __FILE__ . '2' . '::' . 'MyClass', 'MyAnnotation')); 177 $this->assertFalse(stubAnnotationCache::hasNot(stubAnnotation::TARGET_CLASS, __FILE__ . '2' . '::' . 'MyClass', 'MyAnnotation')); 174 178 } 175 179 … … 277 281 $this->assertEqual('cucumber', $anno3->veggie); 278 282 } 283 284 /** 285 * ensure that information about a non-existing annotation is cached as well 286 */ 287 public function testCachingOfNonExistingAnnotations() 288 { 289 // make sure that the information is really not in the cache 290 stubAnnotationCache::remove(stubAnnotation::TARGET_CLASS, __FILE__ . '::' . 'MyClass', 'NonExistingAnnotation'); 291 $this->assertFalse(stubAnnotationCache::has(stubAnnotation::TARGET_CLASS, __FILE__ . '::' . 'MyClass', 'NonExistingAnnotation')); 292 $this->assertFalse(stubAnnotationCache::hasNot(stubAnnotation::TARGET_CLASS, __FILE__ . '::' . 'MyClass', 'NonExistingAnnotation')); 293 try { 294 $myAnnotation1 = stubAnnotationFactory::create($this->commentComplex, 'NonExistingAnnotation', stubAnnotation::TARGET_CLASS, 'MyClass', __FILE__); 295 } catch (Exception $e) { 296 $this->assertFalse(stubAnnotationCache::has(stubAnnotation::TARGET_CLASS, __FILE__ . '::' . 'MyClass', 'NonExistingAnnotation')); 297 $this->assertTrue(stubAnnotationCache::hasNot(stubAnnotation::TARGET_CLASS, __FILE__ . '::' . 'MyClass', 'NonExistingAnnotation')); 298 return; 299 } 300 301 $this->fail('Found NonExistingAnnotation while this annotation should not be present.'); 302 } 279 303 } 280 304 ?>
