Changeset 698

Show
Ignore:
Timestamp:
05/31/07 23:57:18 (1 year ago)
Author:
mikey
Message:

added net.stubbles.reflection.stubReflectionPackage
todo: the star part in stubClassLoader::getClassNames() needs testing

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/reflection/reflection.php

    r432 r698  
    1515                      'net.stubbles.reflection.stubReflectionFunction', 
    1616                      'net.stubbles.reflection.stubReflectionMethod', 
     17                      'net.stubbles.reflection.stubReflectionObject', 
     18                      'net.stubbles.reflection.stubReflectionPackage', 
    1719                      'net.stubbles.reflection.stubReflectionParameter', 
    1820                      'net.stubbles.reflection.stubReflectionProperty' 
  • trunk/src/main/php/net/stubbles/reflection/stubBaseReflectionClass.php

    r255 r698  
    2929     */ 
    3030    public function getFullQualifiedClassName(); 
     31 
     32    /** 
     33     * returns the package where the class belongs to 
     34     * 
     35     * @return  stubReflectionPackage 
     36     */ 
     37    public function getPackage(); 
    3138} 
    3239?> 
  • trunk/src/main/php/net/stubbles/reflection/stubReflectionClass.php

    r432 r698  
    3838     * constructor 
    3939     * 
    40      * @param string  $className  name of class to reflect 
     40     * @param   string  $className  name of class to reflect 
     41     * @throws  stubClassNotFoundException 
    4142     */ 
    4243    public function __construct($className) 
     
    253254        return $stubRefExtension; 
    254255    } 
     256 
     257    /** 
     258     * returns the package where the class belongs to 
     259     * 
     260     * @return  stubReflectionPackage 
     261     */ 
     262    public function getPackage() 
     263    { 
     264        $refPackage = new stubReflectionPackage(stubClassLoader::getPackageName($this->getFullQualifiedClassName())); 
     265        return $refPackage; 
     266    } 
    255267} 
    256268?> 
  • trunk/src/main/php/net/stubbles/reflection/stubReflectionObject.php

    r432 r698  
    275275        return $stubRefExtension; 
    276276    } 
     277 
     278    /** 
     279     * returns the package where the class belongs to 
     280     * 
     281     * @return  stubReflectionPackage 
     282     */ 
     283    public function getPackage() 
     284    { 
     285        $refPackage = new stubReflectionPackage(stubClassLoader::getPackageName($this->getFullQualifiedClassName())); 
     286        return $refPackage; 
     287    } 
    277288} 
    278289?> 
  • trunk/src/main/php/net/stubbles/star/StarClassRegistry.php

    r613 r698  
    134134 
    135135    /** 
     136     * returns a list of all classes 
     137     * 
     138     * @return  string 
     139     */ 
     140    public static function getClasses() 
     141    { 
     142        return array_keys(self::$classes); 
     143    } 
     144 
     145    /** 
    136146     * initialize the class registry 
    137147     */ 
  • trunk/src/main/php/net/stubbles/stubBaseObject.php

    r685 r698  
    2727        return $refObject; 
    2828    } 
    29      
     29 
     30    /** 
     31     * returns package informations 
     32     * 
     33     * @return  stubReflectionPackage 
     34     * @XMLIgnore 
     35     */ 
     36    public function getPackage() 
     37    { 
     38         stubClassLoader::load('net.stubbles.reflection.stubReflectionPackage'); 
     39         $refPackage = new stubReflectionPackage(stubClassLoader::getPackageName($this->getClassName())); 
     40         return $refPackage; 
     41    } 
     42 
    3043    /** 
    3144     * returns the full qualified class name 
     
    3750        return stubClassLoader::getFullQualifiedClassName(get_class($this)); 
    3851    } 
    39      
     52 
    4053    /** 
    4154     * returns a unique hash code for the class 
     
    4760        return spl_object_hash($this); 
    4861    } 
    49      
     62 
    5063    /** 
    5164     * checks whether a value is equal to the class 
     
    8699        return self::getStringRepresentationOf($this, get_object_vars($this)); 
    87100    } 
    88      
     101 
    89102    /** 
    90103     * returns a string representation of the class 
     
    149162        return $string; 
    150163    } 
    151      
     164 
    152165    /** 
    153166     * determines the correct type of a value 
  • trunk/src/main/php/net/stubbles/stubClassLoader.php

    r612 r698  
    150150 
    151151    /** 
     152     * does some initializing 
     153     */ 
     154    private static function init() 
     155    { 
     156        if (stubConfig::useStar() == true && class_exists('StarClassRegistry', false) == true) { 
     157            self::$useStar = true; 
     158        } else { 
     159            self::$useStar = false; 
     160        } 
     161         
     162        self::$sourcePath = stubConfig::getSourcePath() . DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR; 
     163    } 
     164 
     165    /** 
    152166     * method to load files from source path 
    153167     * 
     
    171185        } 
    172186 
    173         if (null == self::$useStar) { 
    174             if (stubConfig::useStar() == true && class_exists('StarClassRegistry', false) == true) { 
    175                 self::$useStar = true; 
    176             } else { 
    177                 self::$useStar = false; 
    178             } 
    179              
    180             self::$sourcePath = stubConfig::getSourcePath() . DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR; 
     187        if (null === self::$useStar) { 
     188            self::init(); 
    181189        } 
    182190         
     
    189197            self::$classNames[$nqClassName] = $fqClassName; 
    190198            $foreignNamespace = self::getForeignNamespace($fqClassName); 
    191             if (null != $foreignNamespace) { 
     199            if (null !== $foreignNamespace) { 
    192200                self::$foreignClassLoaders[$foreignNamespace][0]->load($fqClassName); 
    193201                return; 
     
    195203 
    196204            $uri = null; 
    197             if (true == self::$useStar) { 
     205            if (true === self::$useStar) { 
    198206                $uri = StarClassRegistry::getUriForClass($fqClassName); 
    199207            } 
     
    213221 
    214222    /** 
     223     * returns a list of all available classnames within a package 
     224     * 
     225     * @param   string         $packageName  name of the package to retrieve class names for 
     226     * @param   bool           $recursive    optional  true if classes of subpackages should be included 
     227     * @return  array<string> 
     228     */ 
     229    public static function getClassNames($packageName, $recursive = false) 
     230    { 
     231        if (null == self::$useStar) { 
     232            self::init(); 
     233        } 
     234         
     235        $dirName = self::$sourcePath . str_replace('.', DIRECTORY_SEPARATOR, $packageName); 
     236        if (file_exists($dirName) == false) { 
     237            return array(); 
     238        } 
     239         
     240        if (false === $recursive) { 
     241            $dirIt = new DirectoryIterator($dirName); 
     242        } else { 
     243            $dirIt = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirName)); 
     244        } 
     245         
     246        $classes = array(); 
     247        foreach ($dirIt as $file) { 
     248            if ($file->isDir() == true || substr($file->getFilename(), -4) != '.php') { 
     249                continue; 
     250            } 
     251             
     252            $classes[] = str_replace(DIRECTORY_SEPARATOR, '.', str_replace('.php', '', str_replace(self::$sourcePath, '', $file->getPathname()))); 
     253        } 
     254         
     255        if (true === self::$useStar) { 
     256            $classes = StarClassRegistry::getClasses(); 
     257            foreach ($classes as $fqClassName) { 
     258                if (substr($fqClassName, 0, strlen($packageName)) !== $packageName) { 
     259                    continue; 
     260                } 
     261                 
     262                $nqClassName = self::getNonQualifiedClassName($fqClassName); 
     263                if (str_replace($nqClassName, '', $fqClassName) != $packageName && false === $recursive) { 
     264                    continue; 
     265                } 
     266                 
     267                $classes[] = $fqClassName; 
     268            } 
     269        } 
     270         
     271        sort($classes); 
     272        return $classes; 
     273    } 
     274 
     275    /** 
    215276     * returns the non qualified class name from a full qualified class name 
    216277     * 
     
    237298 
    238299        return null; 
     300    } 
     301 
     302    /** 
     303     * returns the package name of a class 
     304     * 
     305     * @param   string  $fqClassName 
     306     * @return  string 
     307     */ 
     308    public static function getPackageName($fqClassName) 
     309    { 
     310        $classNameParts = explode('.', $fqClassName); 
     311        unset($classNameParts[count($classNameParts) - 1]); 
     312        return join('.', $classNameParts); 
    239313    } 
    240314 
     
    262336            } 
    263337        } 
     338         
    264339        return null; 
    265340    } 
  • trunk/src/main/php/net/stubbles/stubException.php

    r522 r698  
    2525        return $refObject; 
    2626    } 
    27      
     27 
     28    /** 
     29     * returns package informations 
     30     * 
     31     * @return  stubReflectionPackage 
     32     * @XMLIgnore 
     33     */ 
     34    public function getPackage() 
     35    { 
     36         stubClassLoader::load('net.stubbles.reflection.stubReflectionPackage'); 
     37         $refPackage = new stubReflectionPackage(stubClassLoader::getPackageName($this->getClassName())); 
     38         return $refPackage; 
     39    } 
     40 
    2841    /** 
    2942     * returns the full qualified class name 
     
    3548        return stubClassLoader::getFullQualifiedClassName(get_class($this)); 
    3649    } 
    37      
     50 
    3851    /** 
    3952     * returns a unique hash code for the class 
     
    4558        return spl_object_hash($this); 
    4659    } 
    47      
     60 
    4861    /** 
    4962     * checks whether a value is equal to the class 
  • trunk/src/main/php/net/stubbles/stubObject.php

    r522 r698  
    2121     */ 
    2222    public function getClass(); 
     23 
     24    /** 
     25     * returns package informations 
     26     * 
     27     * @return  stubReflectionPackage 
     28     */ 
     29    public function getPackage(); 
    2330 
    2431    /** 
  • trunk/src/main/php/net/stubbles/stubSerializedObject.php

    r522 r698  
    4747     */ 
    4848    protected $data; 
    49      
     49 
    5050    /** 
    5151     * constructor 
     
    6565        $this->hashCode      = $serializable->hashCode(); 
    6666    } 
    67      
     67 
    6868    /** 
    6969     * interceptor called before object instance is serialized 
     
    100100        return $this->classInstance; 
    101101    } 
    102      
     102 
    103103    /** 
    104104     * returns the class name of the serialized class 
     
    123123        return $refObject; 
    124124    } 
    125      
     125 
    126126    /** 
    127127     * returns the full qualified class name 
     
    133133        return stubClassLoader::getFullQualifiedClassName(__CLASS__); 
    134134    } 
    135      
     135 
     136    /** 
     137     * returns package informations 
     138     * 
     139     * @return  stubReflectionPackage 
     140     * @XMLIgnore 
     141     */ 
     142    public function getPackage() 
     143    { 
     144         stubClassLoader::load('net.stubbles.reflection.stubReflectionPackage'); 
     145         $refPackage = new stubReflectionPackage(stubClassLoader::getPackageName($this->getClassName())); 
     146         return $refPackage; 
     147    } 
     148 
    136149    /** 
    137150     * returns a unique hash code for the class 
     
    143156        return spl_object_hash($this); 
    144157    } 
    145      
     158 
    146159    /** 
    147160     * checks whether a value is equal to the class 
     
    159172        return false; 
    160173    } 
    161      
     174 
    162175    /** 
    163176     * returns a serialized representation of the class 
     
    171184        throw new Exception('Can not serialize a serialized ' . $this->getClassName() . ' representation of ' . $this->fqClassName); 
    172185    } 
    173      
     186 
    174187    /** 
    175188     * returns a string representation of the class 
  • trunk/src/test/php/net/stubbles/reflection/ReflectionTestSuite.php

    r483 r698  
    4343        $this->addTestFile(dirname(__FILE__) . '/stubReflectionMethodTestCase.php'); 
    4444        $this->addTestFile(dirname(__FILE__) . '/stubReflectionObjectTestCase.php'); 
     45        $this->addTestFile(dirname(__FILE__) . '/stubReflectionPackageTestCase.php'); 
    4546        $this->addTestFile(dirname(__FILE__) . '/stubReflectionParameterTestCase.php'); 
    4647        $this->addTestFile(dirname(__FILE__) . '/stubReflectionPropertyTestCase.php'); 
  • trunk/src/test/php/net/stubbles/stubClassLoaderTestCase.php

    r579 r698  
    2121    { 
    2222        $this->assertEqual(stubClassLoader::getNonQualifiedClassName('example.foo.Bar'), 'Bar'); 
     23        $this->assertEqual(stubClassLoader::getPackageName('example.foo.Bar'), 'example.foo'); 
    2324        $this->assertNull(stubClassLoader::getFullQualifiedClassName('Bar')); 
     25        $this->assertEqual(stubClassLoader::getPackageName('Bar'), ''); 
    2426        $this->assertEqual(stubClassLoader::getNonQualifiedClassName('stubClassLoader'), 'stubClassLoader'); 
    2527    }