Changeset 701

Show
Ignore:
Timestamp:
06/02/07 12:00:12 (1 year ago)
Author:
schst
Message:

Removed all files from docroot,
added example for annotations

Files:

Legend:

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

    r693 r701  
    7373        </ul> 
    7474      </li> 
     75      <li> 
     76        Reflection 
     77        <ul> 
     78          <li> 
     79            <a href="reflection/annotations.php">Defining annotations</a> (<a href="showsource.php?group=reflection&example=annotations">show PHP source</a>) 
     80          </li> 
     81        </ul> 
     82      </li> 
    7583      <li>Event handling<ul><li><a href="events/">Simple example</a></li><li><a href="events/queue.php">Queued events</a></li></ul></li> 
    7684      <li> 
  • trunk/examples/docroot/reflection/annotations.php

    r494 r701  
    11<?php 
    2 require '../config/php/config.php'; 
    3 require_once '../src/main/php/net/stubbles/stubClassLoader.php'; 
     2/** 
     3 * Example that shows how to define annotations 
     4 * 
     5 * @author Stephan Schmidt <schst@stubbles.net> 
     6 */ 
     7 
     8/** 
     9 * Load stubbles 
     10 */ 
     11require '../bootstrap-stubbles.php'; 
     12 
     13// load the complete reflection package 
    414stubClassLoader::load('net.stubbles.reflection.reflection'); 
    5 class stubFooAnnotation extends stubAbstractAnnotation 
    6 
    7     public function getAnnotationTarget() { return stubAnnotation::TARGET_CLASS; } 
    8 
    9 class stubFooWithBracketsAnnotation extends stubFooAnnotation 
    10 
    11     protected $value; 
    12      
    13     public function setValue($value) 
    14     { 
    15         $this->value = $value; 
     15 
     16/** 
     17 * Simple annotation with two properties 
     18 * 
     19 */ 
     20class stubFooAnnotation extends stubAbstractAnnotation implements stubAnnotation { 
     21    public $bar; 
     22    public $baz; 
     23 
     24    /** 
     25     * Set the bar property 
     26     * 
     27     * @param mixed $bar 
     28     */ 
     29    public function setBar($bar) { 
     30        $this->bar = $bar; 
     31    } 
     32 
     33    /** 
     34     * Set the baz property 
     35     * 
     36     * @param mixed $bar 
     37     */ 
     38    public function setBaz($baz) { 
     39        $this->baz = $baz; 
     40    } 
     41 
     42    /** 
     43     * Get the bar property 
     44     * 
     45     * @return mixed 
     46     */ 
     47    public function getBar() { 
     48        return $this->bar; 
     49    } 
     50 
     51    /** 
     52     * Get the baz property 
     53     * 
     54     * @return mixed 
     55     */ 
     56    public function getBaz() { 
     57        return $this->baz; 
     58    } 
     59 
     60    /** 
     61     * Define the possible targets of this annotation 
     62     * 
     63     * @return int 
     64     */ 
     65    public function getAnnotationTarget() { 
     66        return stubAnnotation::TARGET_CLASS | stubAnnotation::TARGET_PROPERTY; 
    1667    } 
    1768} 
    18 interface Bar { } 
    19 class stubTomTomAnnotation extends stubFooAnnotation implements Bar { } 
    20 class stubTimeTestAnnotation extends stubFooAnnotation { } 
    21 class stubMyAnnotationAnnotation extends stubFooAnnotation 
    22 
    23     protected $foo; 
    24      
    25     public function setFoo($foo) 
    26     { 
    27         $this->foo = $foo; 
    28     } 
    29 
    30 class stubTwoParamsAnnotation extends stubMyAnnotationAnnotation 
    31 
    32     protected $bar; 
    33      
    34     public function setBar($bar) 
    35     { 
    36         $this->bar = $bar; 
    37     } 
    38 
    39 class stubSingleValueAnnotation extends stubFooAnnotation 
    40 
    41     protected $value; 
    42      
    43     public function setValue($value) 
    44     { 
    45         $this->value = $value; 
    46     } 
    47 
     69 
    4870/** 
    49  * This is some text.... 
    50  *  
    51  * @Foo 
    52  * @FooWithBrackets() 
    53  * @Bar[TomTom] 
    54  * @TimeTest     
    55  * @MyAnnotation(foo='bar') 
    56  * @TwoParams(foo="bar", bar=42) 
    57  * @SingleValue(42) 
     71 * Class without the annotation 
    5872 */ 
    59 class MyTestClass1 { 
    60     // intentionally empty 
    61 
    62 /** 
    63  * This is some text.... 
    64  *  
    65  * @Foo 
    66  * @FooWithBrackets() 
    67  * @Bar[TomTom] 
    68  * @TimeTest     
    69  * @MyAnnotation(foo='bar') 
    70  * @TwoParams(foo="bar", bar=42) 
    71  * @SingleValue(42) 
    72  */ 
    73 class MyTestClass2 { 
    74     // intentionally empty 
     73class NoAnnotation { 
    7574} 
    7675 
     76/** 
     77 * Class that makes use of the Foo annotation. 
     78 * 
     79 * @Foo(bar='This is bar', baz=42) 
     80 */ 
     81class MyClass { 
     82} 
    7783 
     84define('MY_CONSTANT', 'This is a constant.'); 
     85/** 
     86 * Class that makes use of the Foo annotation and a constant. 
     87 * 
     88 * @Foo(bar=MY_CONSTANT, baz=false) 
     89 */ 
     90class ConstantAnnotation { 
     91} 
    7892 
     93/** 
     94 * Class that makes use of the Foo annotation and a constant. 
     95 * 
     96 * @Foo(bar=MyClass.class, baz=true) 
     97 */ 
     98class MyClass2 { 
     99} 
    79100 
    80 $clazz    = new ReflectionClass('MyTestClass1'); 
    81 $docBlock1 = $clazz->getDocComment(); 
    82 $clazz    = new ReflectionClass('MyTestClass2'); 
    83 $docBlock3 = $clazz->getDocComment(); 
    84 $start = microtime(true); 
    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'); 
    113 $end = microtime(true); 
    114 echo 'new parser took ' . ($end - $start) . ' seconds<br>'; 
     101print "<pre>"; 
     102checkForFoo('NoAnnotation'); 
     103checkForFoo('MyClass'); 
     104checkForFoo('ConstantAnnotation'); 
     105//checkForFoo('MyClass2'); 
     106print "</pre>"; 
     107 
     108function checkForFoo($className) { 
     109    echo "Introspecting {$className}\n"; 
     110    // extract the annotation 
     111    $clazz = new stubReflectionClass($className); 
     112    if ($clazz->hasAnnotation('Foo')) { 
     113        $foo = $clazz->getAnnotation('Foo'); 
     114 
     115        print "Annotation foo is present:\n"; 
     116        printf("bar : %s\n", var_export($foo->getBar(), true)); 
     117        printf("baz : %s\n", var_export($foo->getBaz(), true)); 
     118    } else { 
     119        print "Annotation foo is *not* present:\n"; 
     120    } 
     121    print "\n"; 
     122
     123 
    115124?>