Changeset 494

Show
Ignore:
Timestamp:
04/13/07 14:09:25 (1 year ago)
Author:
mikey
Message:

corrected examples and speed test
make sure the annotation parser is loaded

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/docroot/annotationParser.php

    r456 r494  
    22require '../config/php/config.php'; 
    33require_once '../src/main/php/net/stubbles/stubClassLoader.php'; 
    4 stubClassLoader::load('net.stubbles.reflection.reflection', 
    5                       'net.stubbles.reflection.annotations.parser.stubAnnotationStateParser' 
    6 ); 
     4stubClassLoader::load('net.stubbles.reflection.reflection'); 
     5define('BAZ_CONSTANT', 'baz'); 
    76/** 
    87 * This is some text.... 
     
    1615 * @InvalidChars(foo='ba@r=,') 
    1716 * @SingleValue(42) 
     17 * @Constant(BAZ_CONSTANT) 
    1818 * @SingleStringValue('This is a string with chars like = or ,') 
    1919 * @WithEscaped(foo='This string contains \' and \\, which is possible using escaping...') 
     
    2121 *            two=2) 
    2222 * @WithTypes(true=true, false=false, integer=4562, 
    23  *            null=null, 
     23 *            null1=null, 
     24 *            null2=NULL, 
     25 *            null3=Null, 
    2426 *            negInt=-13, 
     27 *            double=2.34, 
     28 *            negDouble=-5.67, 
    2529 *            string1='true', 
    2630 *            string2='null', 
  • trunk/docroot/annotationParserTest.php

    r490 r494  
    22require '../config/php/config.php'; 
    33require_once '../src/main/php/net/stubbles/stubClassLoader.php'; 
    4 stubClassLoader::load('net.stubbles.reflection.reflection', 
    5                       'net.stubbles.reflection.annotations.parser.stubAnnotationStateParser' 
    6 ); 
     4stubClassLoader::load('net.stubbles.reflection.reflection'); 
    75class stubFooAnnotation extends stubAbstractAnnotation 
    86{ 
     
    6967 * @Bar[TomTom] 
    7068 * @TimeTest     
    71  * @MyAnnotation(foo=bar
    72  * @TwoParams(foo=bar, bar=42) 
     69 * @MyAnnotation(foo='bar'
     70 * @TwoParams(foo="bar", bar=42) 
    7371 * @SingleValue(42) 
    7472 */ 
     
    7674    // intentionally empty 
    7775} 
    78 /** 
    79  * This is some text.... 
    80  *  
    81  * @Foo 
    82  * @FooWithBrackets() 
    83  * @Bar[TomTom] 
    84  * @TimeTest     
    85  * @MyAnnotation(foo='bar') 
    86  * @TwoParams(foo="bar", bar=42) 
    87  * @SingleValue(42) 
    88  */ 
    89 class MyTestClass3 { 
    90     // intentionally empty 
    91 } 
    92 /** 
    93  * This is some text.... 
    94  *  
    95  * @Foo 
    96  * @FooWithBrackets() 
    97  * @Bar[TomTom] 
    98  * @TimeTest     
    99  * @MyAnnotation(foo=bar) 
    100  * @TwoParams(foo=bar, bar=42) 
    101  * @SingleValue(42) 
    102  */ 
    103 class MyTestClass4 { 
    104     // intentionally empty 
    105 } 
    10676 
    107 class test 
    108 { 
    109     /** 
    110      * Prefixes that can be prepended to class names 
    111      * 
    112      * @var array 
    113      */ 
    114     private static $prefixes = array('stub'); 
    11577 
    116     private static $parser      = null; 
    117     private static $annotations = array(); 
    11878 
    119     public static function create($comment, $annotationName, $target, $targetName) 
    120     { 
    121         if (stubAnnotationCache::has($target, $targetName, $annotationName)) { 
    122             return $cachedAnnotation = stubAnnotationCache::get($target, $targetName, $annotationName); 
    123         } 
    124  
    125         $hash = md5($comment); 
    126         if (isset(self::$annotations[$hash]) == false) { 
    127             if (null == self::$parser) { 
    128                 self::$parser = new stubAnnotationStateParser(); 
    129             } 
    130              
    131             self::$annotations[$hash] = self::$parser->parse($comment); 
    132         } 
    133      
    134         if (isset(self::$annotations[$hash][$annotationName]) == false) { 
    135             throw new ReflectionException('Can not find annotation ' . $annotationName); 
    136         } 
    137          
    138         $annotationClass = self::findAnnotationClass(self::$annotations[$hash][$annotationName]['type']); 
    139         $annotation      = new $annotationClass(); 
    140  
    141         if (($annotation instanceof stubAnnotation) == false) { 
    142             throw new ReflectionException('The annotation: ' . $annotationName . ' is not an instance of net.stubbles.reflection.annotations.stubAnnotation.'); 
    143         } 
    144          
    145         if (self::$annotations[$hash][$annotationName]['type'] != $annotationName) { 
    146             $annotationType = self::findAnnotationClass($annotationName, true); 
    147             if (!is_a($annotation, $annotationType)) { 
    148                 throw new ReflectionException('The annotation: ' . $annotationName . ' is not an instance of ' . $annotationType . '.'); 
    149             } 
    150         } 
    151          
    152         if (($annotation->getAnnotationTarget() & $target) == 0) { 
    153             throw new ReflectionException('The annotation: ' . $annotationName . ' is not applicable for the given type ' . $targetName); 
    154         } 
    155  
    156         $refClass = new ReflectionClass($annotation); 
    157         foreach (self::$annotations[$hash][$annotationName]['params'] as $name => $value) { 
    158             if ($refClass->hasMethod('set' . ucfirst($name)) == true) { 
    159                 $refClass->getMethod('set' . ucfirst($name))->invoke($annotation, $value); 
    160             } elseif ($refClass->hasProperty($name) == true) { 
    161                 $refClass->getProperty($name)->setValue($annotation, $value); 
    162             } else { 
    163                 throw new ReflectionException('Annotation value for "' . $name . '" can not be set: no public setter and no public property exists with this name.'); 
    164             } 
    165         } 
    166          
    167         $annotation->setAnnotationName($annotationName); 
    168         stubAnnotationCache::put($target, $targetName, $annotationName, $annotation); 
    169         return $annotation; 
    170     } 
    171  
    172     private static function findAnnotationClass($annotationClass, $allowInterface = false) { 
    173         if (class_exists($annotationClass, false) == true) { 
    174             return $annotationClass; 
    175         } 
    176          
    177         if (true == $allowInterface && interface_exists($annotationClass, false) == true) { 
    178             return $annotationClass; 
    179         } 
    180          
    181         $annotationClassname = $annotationClass  . 'Annotation'; 
    182         foreach (self::$prefixes as $prefix) { 
    183             if (class_exists($prefix . $annotationClassname) == true) { 
    184                 return $prefix . $annotationClassname; 
    185             } 
    186              
    187             if (true == $allowInterface && interface_exists($prefix . $annotationClassname, false) == true) { 
    188                 return $prefix . $annotationClassname; 
    189             } 
    190         } 
    191          
    192         throw new ReflectionException('Error parsing annotation: Class ' . $annotationClass . ' does not exist'); 
    193     } 
    194 } 
    19579 
    19680$clazz    = new ReflectionClass('MyTestClass1'); 
    19781$docBlock1 = $clazz->getDocComment(); 
    198 $clazz    = new ReflectionClass('MyTestClass3'); 
     82$clazz    = new ReflectionClass('MyTestClass2'); 
    19983$docBlock3 = $clazz->getDocComment(); 
    20084$start = microtime(true); 
    201 $foo = test::create($docBlock1, 'Foo', stubAnnotation::TARGET_CLASS, '1'); 
    202 $foo = test::create($docBlock1, 'FooWithBrackets', stubAnnotation::TARGET_CLASS, '2'); 
    203 $foo = test::create($docBlock1, 'Bar', stubAnnotation::TARGET_CLASS, '3'); 
    204 $foo = test::create($docBlock1, 'TimeTest', stubAnnotation::TARGET_CLASS, '4'); 
    205 $foo = test::create($docBlock1, 'MyAnnotation', stubAnnotation::TARGET_CLASS, '5'); 
    206 $foo = test::create($docBlock1, 'TwoParams', stubAnnotation::TARGET_CLASS, '6'); 
    207 $foo = test::create($docBlock1, 'SingleValue', stubAnnotation::TARGET_CLASS, '7'); 
    208 $foo = test::create($docBlock1, 'Foo', stubAnnotation::TARGET_CLASS, '1'); 
    209 $foo = test::create($docBlock1, 'FooWithBrackets', stubAnnotation::TARGET_CLASS, '2'); 
    210 $foo = test::create($docBlock1, 'Bar', stubAnnotation::TARGET_CLASS, '3'); 
    211 $foo = test::create($docBlock1, 'TimeTest', stubAnnotation::TARGET_CLASS, '4'); 
    212 $foo = test::create($docBlock1, 'MyAnnotation', stubAnnotation::TARGET_CLASS, '5'); 
    213 $foo = test::create($docBlock1, 'TwoParams', stubAnnotation::TARGET_CLASS, '6'); 
    214 $foo = test::create($docBlock1, 'SingleValue', stubAnnotation::TARGET_CLASS, '7'); 
    215 $foo = test::create($docBlock3, 'Foo', stubAnnotation::TARGET_CLASS, '11'); 
    216 $foo = test::create($docBlock3, 'FooWithBrackets', stubAnnotation::TARGET_CLASS, '12'); 
    217 $foo = test::create($docBlock3, 'Bar', stubAnnotation::TARGET_CLASS, '13'); 
    218 $foo = test::create($docBlock3, 'TimeTest', stubAnnotation::TARGET_CLASS, '14'); 
    219 $foo = test::create($docBlock3, 'MyAnnotation', stubAnnotation::TARGET_CLASS, '15'); 
    220 $foo = test::create($docBlock3, 'TwoParams', stubAnnotation::TARGET_CLASS, '16'); 
    221 $foo = test::create($docBlock3, 'SingleValue', stubAnnotation::TARGET_CLASS, '17'); 
    222 $foo = test::create($docBlock3, 'Foo', stubAnnotation::TARGET_CLASS, '11'); 
    223 $foo = test::create($docBlock3, 'FooWithBrackets', stubAnnotation::TARGET_CLASS, '12'); 
    224 $foo = test::create($docBlock3, 'Bar', stubAnnotation::TARGET_CLASS, '13'); 
    225 $foo = test::create($docBlock3, 'TimeTest', stubAnnotation::TARGET_CLASS, '14'); 
    226 $foo = test::create($docBlock3, 'MyAnnotation', stubAnnotation::TARGET_CLASS, '15'); 
    227 $foo = test::create($docBlock3, 'TwoParams', stubAnnotation::TARGET_CLASS, '16'); 
    228 $foo = test::create($docBlock3, 'SingleValue', stubAnnotation::TARGET_CLASS, '17'); 
     85$foo = stubAnnotationFactory::create($docBlock1, 'Foo', stubAnnotation::TARGET_CLASS, '1'); 
     86$foo = stubAnnotationFactory::create($docBlock1, 'FooWithBrackets', stubAnnotation::TARGET_CLASS, '2'); 
     87$foo = stubAnnotationFactory::create($docBlock1, 'Bar', stubAnnotation::TARGET_CLASS, '3'); 
     88$foo = stubAnnotationFactory::create($docBlock1, 'TimeTest', stubAnnotation::TARGET_CLASS, '4'); 
     89$foo = stubAnnotationFactory::create($docBlock1, 'MyAnnotation', stubAnnotation::TARGET_CLASS, '5'); 
     90$foo = stubAnnotationFactory::create($docBlock1, 'TwoParams', stubAnnotation::TARGET_CLASS, '6'); 
     91$foo = stubAnnotationFactory::create($docBlock1, 'SingleValue', stubAnnotation::TARGET_CLASS, '7'); 
     92$foo = stubAnnotationFactory::create($docBlock1, 'Foo', stubAnnotation::TARGET_CLASS, '1'); 
     93$foo = stubAnnotationFactory::create($docBlock1, 'FooWithBrackets', stubAnnotation::TARGET_CLASS, '2'); 
     94$foo = stubAnnotationFactory::create($docBlock1, 'Bar', stubAnnotation::TARGET_CLASS, '3'); 
     95$foo = stubAnnotationFactory::create($docBlock1, 'TimeTest', stubAnnotation::TARGET_CLASS, '4'); 
     96$foo = stubAnnotationFactory::create($docBlock1, 'MyAnnotation', stubAnnotation::TARGET_CLASS, '5'); 
     97$foo = stubAnnotationFactory::create($docBlock1, 'TwoParams', stubAnnotation::TARGET_CLASS, '6'); 
     98$foo = stubAnnotationFactory::create($docBlock1, 'SingleValue', stubAnnotation::TARGET_CLASS, '7'); 
     99$foo = stubAnnotationFactory::create($docBlock3, 'Foo', stubAnnotation::TARGET_CLASS, '11'); 
     100$foo = stubAnnotationFactory::create($docBlock3, 'FooWithBrackets', stubAnnotation::TARGET_CLASS, '12'); 
     101$foo = stubAnnotationFactory::create($docBlock3, 'Bar', stubAnnotation::TARGET_CLASS, '13'); 
     102$foo = stubAnnotationFactory::create($docBlock3, 'TimeTest', stubAnnotation::TARGET_CLASS, '14'); 
     103$foo = stubAnnotationFactory::create($docBlock3, 'MyAnnotation', stubAnnotation::TARGET_CLASS, '15'); 
     104$foo = stubAnnotationFactory::create($docBlock3, 'TwoParams', stubAnnotation::TARGET_CLASS, '16'); 
     105$foo = stubAnnotationFactory::create($docBlock3, 'SingleValue', stubAnnotation::TARGET_CLASS, '17'); 
     106$foo = stubAnnotationFactory::create($docBlock3, 'Foo', stubAnnotation::TARGET_CLASS, '11'); 
     107$foo = stubAnnotationFactory::create($docBlock3, 'FooWithBrackets', stubAnnotation::TARGET_CLASS, '12'); 
     108$foo = stubAnnotationFactory::create($docBlock3, 'Bar', stubAnnotation::TARGET_CLASS, '13'); 
     109$foo = stubAnnotationFactory::create($docBlock3, 'TimeTest', stubAnnotation::TARGET_CLASS, '14'); 
     110$foo = stubAnnotationFactory::create($docBlock3, 'MyAnnotation', stubAnnotation::TARGET_CLASS, '15'); 
     111$foo = stubAnnotationFactory::create($docBlock3, 'TwoParams', stubAnnotation::TARGET_CLASS, '16'); 
     112$foo = stubAnnotationFactory::create($docBlock3, 'SingleValue', stubAnnotation::TARGET_CLASS, '17'); 
    229113$end = microtime(true); 
    230114echo 'new parser took ' . ($end - $start) . ' seconds<br>'; 
    231  
    232 $clazz     = new ReflectionClass('MyTestClass2'); 
    233 $docBlock2 = $clazz->getDocComment(); 
    234 $clazz     = new ReflectionClass('MyTestClass4'); 
    235 $docBlock4 = $clazz->getDocComment(); 
    236 $start = microtime(true); 
    237 $foo = stubAnnotationFactory::create($docBlock2, 'Foo', stubAnnotation::TARGET_CLASS, '21'); 
    238 $foo = stubAnnotationFactory::create($docBlock2, 'FooWithBrackets', stubAnnotation::TARGET_CLASS, '22'); 
    239 $foo = stubAnnotationFactory::create($docBlock2, 'Bar', stubAnnotation::TARGET_CLASS, '23'); 
    240 $foo = stubAnnotationFactory::create($docBlock2, 'TimeTest', stubAnnotation::TARGET_CLASS, '24'); 
    241 $foo = stubAnnotationFactory::create($docBlock2, 'MyAnnotation', stubAnnotation::TARGET_CLASS, '25'); 
    242 $foo = stubAnnotationFactory::create($docBlock2, 'TwoParams', stubAnnotation::TARGET_CLASS, '26'); 
    243 $foo = stubAnnotationFactory::create($docBlock2, 'SingleValue', stubAnnotation::TARGET_CLASS, '27'); 
    244 $foo = stubAnnotationFactory::create($docBlock2, 'Foo', stubAnnotation::TARGET_CLASS, '21'); 
    245 $foo = stubAnnotationFactory::create($docBlock2, 'FooWithBrackets', stubAnnotation::TARGET_CLASS, '22'); 
    246 $foo = stubAnnotationFactory::create($docBlock2, 'Bar', stubAnnotation::TARGET_CLASS, '23'); 
    247 $foo = stubAnnotationFactory::create($docBlock2, 'TimeTest', stubAnnotation::TARGET_CLASS, '24'); 
    248 $foo = stubAnnotationFactory::create($docBlock2, 'MyAnnotation', stubAnnotation::TARGET_CLASS, '25'); 
    249 $foo = stubAnnotationFactory::create($docBlock2, 'TwoParams', stubAnnotation::TARGET_CLASS, '26'); 
    250 $foo = stubAnnotationFactory::create($docBlock2, 'SingleValue', stubAnnotation::TARGET_CLASS, '27'); 
    251 $foo = stubAnnotationFactory::create($docBlock4, 'Foo', stubAnnotation::TARGET_CLASS, '31'); 
    252 $foo = stubAnnotationFactory::create($docBlock4, 'FooWithBrackets', stubAnnotation::TARGET_CLASS, '32'); 
    253 $foo = stubAnnotationFactory::create($docBlock4, 'Bar', stubAnnotation::TARGET_CLASS, '33'); 
    254 $foo = stubAnnotationFactory::create($docBlock4, 'TimeTest', stubAnnotation::TARGET_CLASS, '34'); 
    255 $foo = stubAnnotationFactory::create($docBlock4, 'MyAnnotation', stubAnnotation::TARGET_CLASS, '35'); 
    256 $foo = stubAnnotationFactory::create($docBlock4, 'TwoParams', stubAnnotation::TARGET_CLASS, '36'); 
    257 $foo = stubAnnotationFactory::create($docBlock4, 'SingleValue', stubAnnotation::TARGET_CLASS, '37'); 
    258 $foo = stubAnnotationFactory::create($docBlock4, 'Foo', stubAnnotation::TARGET_CLASS, '31'); 
    259 $foo = stubAnnotationFactory::create($docBlock4, 'FooWithBrackets', stubAnnotation::TARGET_CLASS, '32'); 
    260 $foo = stubAnnotationFactory::create($docBlock4, 'Bar', stubAnnotation::TARGET_CLASS, '33'); 
    261 $foo = stubAnnotationFactory::create($docBlock4, 'TimeTest', stubAnnotation::TARGET_CLASS, '34'); 
    262 $foo = stubAnnotationFactory::create($docBlock4, 'MyAnnotation', stubAnnotation::TARGET_CLASS, '35'); 
    263 $foo = stubAnnotationFactory::create($docBlock4, 'TwoParams', stubAnnotation::TARGET_CLASS, '36'); 
    264 $foo = stubAnnotationFactory::create($docBlock4, 'SingleValue', stubAnnotation::TARGET_CLASS, '37'); 
    265 $end = microtime(true); 
    266 echo 'old parser took ' . ($end - $start) . ' seconds<br>'; 
    267115?> 
  • trunk/src/main/php/net/stubbles/reflection/annotations/stubAnnotationFactory.php

    r492 r494  
    99 */ 
    1010stubClassLoader::load('net.stubbles.reflection.annotations.stubAnnotation', 
    11                       'net.stubbles.reflection.annotations.stubAnnotationCache' 
     11                      'net.stubbles.reflection.annotations.stubAnnotationCache', 
     12                      'net.stubbles.reflection.annotations.parser.stubAnnotationStateParser' 
    1213); 
    1314/**