Changeset 111

Show
Ignore:
Timestamp:
01/18/07 17:56:16 (2 years ago)
Author:
mikey
Message:

fix path for getting contents of StarLoader?
allow more than one libpath in StarClassRegistry?

Files:

Legend:

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

    r93 r111  
    9999        $this->writer->open(); 
    100100        if (strlen($this->preface) > 0) { 
    101             $preFace  = file_get_contents('StarLoader.php'); 
     101            $preFace  = file_get_contents(dirname(__FILE__) . '/StarLoader.php'); 
    102102            $preFace .= "<?php stream_wrapper_register('star', 'StarLoader'); ?>" . trim($this->preface) . "<?php __halt_compiler();"; 
    103103            $this->writer->write($preFace); 
  • trunk/src/main/php/net/stubbles/star/StarClassRegistry.php

    r110 r111  
    1818     * @var  bool 
    1919     */ 
    20     protected static $initDone = false; 
     20    protected static $initDone = false; 
    2121    /** 
    2222     * path to star files 
     
    2424     * @var  string 
    2525     */ 
    26     protected static $libPath  = null
     26    protected static $libPathes = array()
    2727    /** 
    2828     * list of classes and the file where they are in 
     
    3030     * @var  array<string,string> 
    3131     */ 
    32     protected static $classes  = array(); 
     32    protected static $classes  = array(); 
    3333    /** 
    3434     * list of files and the classes they contain 
     
    3636     * @var  array<string,array<string>> 
    3737     */ 
    38     protected static $files    = array(); 
     38    protected static $files    = array(); 
    3939     
    4040    /** 
    4141     * set the path to the star files 
    4242     * 
    43      * @param  string  $libPath 
     43     * @param  string  $libPath    path to lib files 
     44     * @param  bool    $recursive  optional  recurse into sub directories of lib path 
    4445     */ 
    45     public static function setLibPath($libPath
     46    public static function addLibPath($libPath, $recursive = true
    4647    { 
    47         self::$libPath = $libPath
     48        self::$libPathes[$libPath] = $recursive
    4849    } 
    4950     
     
    5657    public static function getFileForClass($fqClassName) 
    5758    { 
     59        self::init(); 
    5860        if (isset(self::$classes[$fqClassName]) == true) { 
    5961            return self::$classes[$fqClassName]; 
     62        } 
     63         
     64        return null; 
     65    } 
     66     
     67    /** 
     68     * returns the uri for the given class 
     69     * 
     70     * @param   string  $fqClassName  the full qualified class name 
     71     * @return  string 
     72     */ 
     73    public static function getUriForClass($fqClassName) 
     74    { 
     75        self::init(); 
     76        if (isset(self::$classes[$fqClassName]) == true) { 
     77            return 'star://' . self::$classes[$fqClassName] . '?' . $fqClassName; 
    6078        } 
    6179         
     
    7189    public function getClassNamesFromFile($file) 
    7290    { 
     91        self::init(); 
    7392        if (isset(self::$files[$file]) == true) { 
    7493            return self::$files[$file]; 
     
    83102    protected static function init() 
    84103    { 
    85         if (true == self::$initDone) { 
     104        if (true === self::$initDone) { 
    86105            return; 
    87106        } 
    88107         
    89         if (null == self::$libPath) { 
    90             self::$libPath = getcwd()
     108        if (count(self::$libPathes) == 0) { 
     109            self::$libPathes[realpath(getcwd() . '/../lib')] = true
    91110        } 
    92111         
    93         if (file_exists(self::$libPath . '/.cache') == true) { 
    94             $cache = unserialize(file_get_contents(self::$libPath . '/.cache')); 
    95             self::$files    = $cache['files']; 
    96             self::$classes  = $cache['classes']; 
    97             self::$initDone = true; 
    98             return; 
    99         } 
    100          
    101         $dirIt = new DirectoryIterator(self::$libPath); 
    102         foreach ($dirIt as $file) { 
    103             if ($file->isFile() == false || substr($file->getPathname(), -5) != '.star') { 
     112        foreach (self::$libPathes as $libPath => $recursive) { 
     113            if (file_exists($libPath . '/.cache') == true) { 
     114                $cache = unserialize(file_get_contents($libPath . '/.cache')); 
     115                self::$files    = array_merge(self::$files, $cache['files']); 
     116                self::$classes  = array_merge(self::$classes, $cache['classes']); 
     117                self::$initDone = true; 
    104118                continue; 
    105119            } 
    106120             
    107             $archiveData = StarLoader::acquire($file->getPathname()); 
    108             if (empty($archiveData) == true) { 
    109                 continue; 
     121            if (true === $recursive) { 
     122                $dirIt = new RecursiveDirectoryIterator($libPath); 
     123            } else { 
     124                $dirIt = new DirectoryIterator($libPath); 
    110125            } 
    111126             
    112             self::$files[$file->getPath()] = array_keys($archiveData); 
     127            $cache['files']   = array(); 
     128            $cache['classes'] = array(); 
     129            foreach ($dirIt as $file) { 
     130                if ($file->isFile() == false || substr($file->getPathname(), -5) != '.star') { 
     131                    continue; 
     132                } 
     133                 
     134                $archiveData = StarLoader::acquire($file->getPathname()); 
     135                if (empty($archiveData) == true) { 
     136                    continue; 
     137                } 
     138                 
     139                $classes = array_keys($archiveData); 
     140                self::$files[$file->getPath()]    = $classes; 
     141                $cache['files'][$file->getPath()] = $classes; 
     142                 
     143                foreach ($archiveData as $fqClassName => $storeData) { 
     144                    self::$classes[$fqClassName]    = $file->getPathname(); 
     145                    $cache['classes'][$fqClassName] = $file->getPathname(); 
     146                } 
     147            } 
    113148             
    114             foreach ($archiveData as $fqClassName => $storeData) { 
    115                 self::$classes[$fqClassName] = $file->getPathname(); 
    116             } 
     149            file_put_contents($libPath . '/.cache', serialize($cache)); 
     150            self::$initDone = true; 
    117151        } 
    118          
    119         $cache = array('files' => self::$files, 'classes' => self::$classes); 
    120         file_put_contents(self::$libPath . '/.cache', serialize($cache)); 
    121         self::$initDone = true; 
    122152    } 
    123153}