Changeset 501

Show
Ignore:
Timestamp:
04/13/07 18:26:35 (2 years ago)
Author:
mikey
Message:

extended caching of annotations over more than one request

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/build/stubbles/build.xml

    r442 r501  
    9393 
    9494  <target name="test" description="run test suite"> 
    95     <delete file="${project.basedir}/../../lib/.cache" verbose="true"/> 
     95    <phingcall target="test-preparation" /> 
    9696    <mySimpletest testfile="&quot;${project.basedir}/../../src/test/run.php&quot;" exit="true" /> 
    9797  </target> 
    9898 
    9999  <target name="test-integration"> 
    100     <delete file="${project.basedir}/../../lib/.cache" verbose="true"/> 
     100    <phingcall target="test-preparation" /> 
    101101    <mySimpletest testfile="&quot;${project.basedir}/../../src/test/runIntegration.php&quot;" exit="true" /> 
    102102  </target> 
     103   
     104  <target name="test-preparation" description="Some preparations before running the tests."> 
     105    <delete file="${project.basedir}/../../lib/.cache" verbose="true"/> 
     106    <delete> 
     107      <fileset dir="${project.basedir}/../../cache"> 
     108        <include name="**/*.cache" /> 
     109      </fileset> 
     110    </delete> 
     111  </target> 
    103112</project> 
  • trunk/src/main/php/net/stubbles/reflection/annotations/stubAbstractAnnotation.php

    r432 r501  
    4040        return $this->annotationName; 
    4141    } 
     42     
     43    /** 
     44     * assure that a clone clones all properties og type object as well 
     45     */ 
     46    public function __clone() 
     47    { 
     48        foreach (get_object_vars($this) as $name => $value) { 
     49            if (is_object($value) == true) { 
     50                $this->$name = clone $this->$name; 
     51            } 
     52        } 
     53    } 
    4254} 
    4355?> 
  • trunk/src/main/php/net/stubbles/reflection/annotations/stubAnnotation.php

    r432 r501  
    1313 * @subpackage  reflection_annotations 
    1414 */ 
    15 interface stubAnnotation 
     15interface stubAnnotation extends stubObject 
    1616{ 
    1717    /** 
     
    5656     */ 
    5757    public function getAnnotationTarget(); 
     58     
     59    /** 
     60     * assure that a clone clones all properties of type object as well 
     61     *  
     62     * @todo  take comment away as soon as SimpleTests Mock::generate() supports __clone() 
     63     */ 
     64    #public function __clone(); 
    5865} 
    5966?> 
  • trunk/src/main/php/net/stubbles/reflection/annotations/stubAnnotationCache.php

    r432 r501  
    1919     * Property to store annotations 
    2020     * 
    21      * @var array 
     21     * @var array<string,array> 
    2222     */ 
    23     private static $annotations = array( 
    24                                     stubAnnotation::TARGET_CLASS    => array(), 
    25                                     stubAnnotation::TARGET_FUNCTION => array(), 
    26                                     stubAnnotation::TARGET_METHOD   => array(), 
    27                                     stubAnnotation::TARGET_PROPERTY => array() 
     23    private static $annotations = array(stubAnnotation::TARGET_CLASS    => array(), 
     24                                        stubAnnotation::TARGET_FUNCTION => array(), 
     25                                        stubAnnotation::TARGET_METHOD   => array(), 
     26                                        stubAnnotation::TARGET_PROPERTY => array() 
    2827                                  ); 
    2928 
    3029    /** 
    31      * Store an annotation in the cache 
    32      * 
    33      * @param int $target 
    34      * @param string $targetName 
    35      * @param string $annotationName 
    36      * @param stubAnnotation|null $annotation 
     30     * static initializer 
    3731     */ 
    38     public static function put($target, $targetName, $annotationName, stubAnnotation $annotation = null) { 
    39         if (!isset(self::$annotations[$target][$targetName])) { 
    40             self::$annotations[$target][$targetName] = array(); 
     32    public static function __static() 
     33    { 
     34        if (file_exists(stubConfig::getCachePath() . '/annotations.cache') == true) { 
     35            self::$annotations = unserialize(file_get_contents(stubConfig::getCachePath() . '/annotations.cache')); 
    4136        } 
    42         self::$annotations[$target][$targetName][$annotationName] = $annotation; 
     37         
     38        register_shutdown_function(array(__CLASS__, '__shutdown')); 
    4339    } 
    4440 
    4541    /** 
    46      * Check, whether an annotation is available in the cache 
     42     * static shutdown 
     43     */ 
     44    public static function __shutdown() 
     45    { 
     46        file_put_contents(stubConfig::getCachePath() . '/annotations.cache', serialize(self::$annotations)); 
     47    } 
     48 
     49    /** 
     50     * flushes all contents from cache 
     51     */ 
     52    public static function flush() 
     53    { 
     54        self::$annotations = array(stubAnnotation::TARGET_CLASS    => array(), 
     55                                   stubAnnotation::TARGET_FUNCTION => array(), 
     56                                   stubAnnotation::TARGET_METHOD   => array(), 
     57                                   stubAnnotation::TARGET_PROPERTY => array() 
     58                             ); 
     59    } 
     60 
     61    /** 
     62     * store an annotation in the cache 
    4763     * 
    48      * @param int $target 
    49      * @param string $targetName 
    50      * @param string $annotationName 
    51      * @return boolean 
     64     * @param  int             $target          target of the annotation 
     65     * @param  string          $targetName      name of the target 
     66     * @param  string          $annotationName  name of the annotation 
     67     * @param  stubAnnotation  $annotation      optional  the annotation to store 
     68     */ 
     69    public static function put($target, $targetName, $annotationName, stubAnnotation $annotation = null) { 
     70        if (isset(self::$annotations[$target][$targetName]) == false) { 
     71            self::$annotations[$target][$targetName] = array(); 
     72        } 
     73         
     74        if (null !== $annotation) { 
     75            $clone = clone $annotation; 
     76            self::$annotations[$target][$targetName][$annotationName] = $clone->getSerialized(); 
     77        } else { 
     78            self::$annotations[$target][$targetName][$annotationName] = null; 
     79        } 
     80    } 
     81 
     82    /** 
     83     * check, whether an annotation is available in the cache 
     84     * 
     85     * @param   int     $target          target of the annotation 
     86     * @param   string  $targetName      name of the target 
     87     * @param   string  $annotationName  name of the annotation 
     88     * @return  bool 
    5289     */ 
    5390    public static function has($target, $targetName, $annotationName) { 
    54         if (!isset(self::$annotations[$target][$targetName])) { 
     91        if (isset(self::$annotations[$target][$targetName]) == false) { 
    5592            return false; 
    5693        } 
    57         if (!isset(self::$annotations[$target][$targetName][$annotationName])) { 
     94         
     95        if (isset(self::$annotations[$target][$targetName][$annotationName]) == false) { 
    5896            return false; 
    5997        } 
     98         
    6099        return true; 
    61100    } 
    62      
     101 
    63102    /** 
    64      * Fetch an annotation from the cache 
     103     * fetch an annotation from the cache 
    65104     * 
    66      * @param int $target 
    67      * @param string $targetName 
    68      * @param string $annotationName 
    69      * @return stubAnnotation 
     105     * @param   int             $target          target of the annotation 
     106     * @param   string          $targetName      name of the target 
     107     * @param   string          $annotationName  name of the annotation 
     108     * @return stubAnnotation 
    70109     */ 
    71110    public static function get($target, $targetName, $annotationName) { 
    72         if (self::has($target, $targetName, $annotationName)) { 
    73             return self::$annotations[$target][$targetName][$annotationName]
     111        if (self::has($target, $targetName, $annotationName) == true) { 
     112            return clone self::$annotations[$target][$targetName][$annotationName]->getUnserialized()
    74113        } 
     114         
    75115        return null; 
    76116    } 
  • trunk/src/test/php/net/stubbles/reflection/annotations/stubAnnotationFactoryBuildTestCase.php

    r492 r501  
    88 */ 
    99stubClassLoader::load('net.stubbles.reflection.annotations.stubAnnotationFactory'); 
    10 class stubMethodAnnotation implements stubAnnotation 
     10class stubMethodAnnotation extends stubAbstractAnnotation implements stubAnnotation 
    1111{ 
    1212    protected $foo; 
     
    3030    public function getAnnotationTarget() {} 
    3131} 
    32 class stubPropertyAnnotation implements stubAnnotation 
     32class stubPropertyAnnotation extends stubAbstractAnnotation implements stubAnnotation 
    3333{ 
    3434    public $foo; 
  • trunk/src/test/php/net/stubbles/reflection/annotations/stubAnnotationFactoryTestCase.php

    r492 r501  
    170170        $this->assertEqual('bar', $anno2->foo); 
    171171        $this->assertEqual('true', $anno2->argh); 
    172         $this->assertEqual('tomato', $anno2->veggie); 
     172        $this->assertEqual('cucumber', $anno2->veggie); 
    173173 
    174174        // fetch with a new stubAnnotationClass instance 
     
    179179        $this->assertEqual('bar', $anno3->foo); 
    180180        $this->assertEqual('true', $anno3->argh); 
    181         $this->assertEqual('tomato', $anno3->veggie); 
     181        $this->assertEqual('cucumber', $anno3->veggie); 
    182182    } 
    183183}