Changeset 257

Show
Ignore:
Timestamp:
10/21/07 00:27:07 (9 months ago)
Author:
hans
Message:

#162 - Added @pattern as alias for @name in patternset

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/2.3/classes/phing/types/PatternSet.php

    r123 r257  
    1717 * This software consists of voluntary contributions made by many individuals 
    1818 * and is licensed under the LGPL. For more information please see 
    19  * <http://phing.info>.  
     19 * <http://phing.info>. 
    2020 */ 
    2121 
     
    3333class PatternSet extends DataType { 
    3434 
    35     private $includeList = array(); 
    36     private $excludeList = array(); 
    37     private $includesFileList = array(); 
    38     private $excludesFileList = array(); 
     35       private $includeList = array(); 
     36       private $excludeList = array(); 
     37       private $includesFileList = array(); 
     38       private $excludesFileList = array(); 
    3939 
    4040    /** 
     
    4545     */ 
    4646    function setRefid(Reference $r) { 
    47         if (!empty($this->includeList) || !empty($this->excludeList)) { 
    48             throw $this->tooManyAttributes(); 
    49        
    50         parent::setRefid($r); 
     47       if (!empty($this->includeList) || !empty($this->excludeList)) { 
     48               throw $this->tooManyAttributes(); 
     49       
     50       parent::setRefid($r); 
    5151    } 
    5252 
     
    5959    */ 
    6060    function createInclude() { 
    61         if ($this->isReference()) { 
    62             throw $this->noChildrenAllowed(); 
    63        
    64         return $this->addPatternToList($this->includeList); 
     61       if ($this->isReference()) { 
     62               throw $this->noChildrenAllowed(); 
     63       
     64       return $this->addPatternToList($this->includeList); 
    6565    } 
    6666 
     
    7373    */ 
    7474    function createIncludesFile() { 
    75         if ($this->isReference()) { 
    76             throw $this->noChildrenAllowed(); 
    77        
    78         return $this->addPatternToList($this->includesFileList); 
     75       if ($this->isReference()) { 
     76               throw $this->noChildrenAllowed(); 
     77       
     78       return $this->addPatternToList($this->includesFileList); 
    7979    } 
    8080 
     
    8686    */ 
    8787    function createExclude() { 
    88         if ($this->isReference()) { 
    89             throw $this->noChildrenAllowed(); 
    90         } 
    91         return $this->addPatternToList($this->excludeList); 
    92     } 
    93  
    94     /** 
    95      * add a name entry on the exclude files list 
    96     * 
    97     * @returns PatternSetNameEntry Reference to object 
    98     * @throws  BuildException 
    99      */ 
    100  
     88        if ($this->isReference()) { 
     89                throw $this->noChildrenAllowed(); 
     90        } 
     91        return $this->addPatternToList($this->excludeList); 
     92    } 
     93 
     94    /** 
     95         * add a name entry on the exclude files list 
     96         * 
     97         * @returns PatternSetNameEntry Reference to object 
     98         * @throws  BuildException 
     99         */ 
    101100    function createExcludesFile() { 
    102         if ($this->isReference()) { 
    103             throw $this->noChildrenAllowed(); 
    104             return; 
    105        
    106         return $this->addPatternToList($this->excludesFileList); 
     101       if ($this->isReference()) { 
     102               throw $this->noChildrenAllowed(); 
     103               return; 
     104       
     105       return $this->addPatternToList($this->excludesFileList); 
    107106    } 
    108107 
     
    117116     */ 
    118117    function setIncludes($includes) { 
    119         if ($this->isReference()) { 
    120             throw $this->tooManyAttributes(); 
    121         } 
    122         if ($includes !== null && strlen($includes) > 0) { 
    123             $tok = strtok($includes, ", "); 
    124             while ($tok !== false) { 
    125                 $o = $this->createInclude(); 
    126                 $o->setName($tok); 
    127                 $tok = strtok(", "); 
    128             } 
    129         } 
    130     } 
    131  
    132  
    133     /** 
    134      * Sets the set of exclude patterns. Patterns may be separated by a comma 
    135      * or a space. 
    136      * 
    137      * @param string the string containing the exclude patterns 
    138     * @returns void 
    139     * @throws  BuildException 
    140      */ 
    141  
     118        if ($this->isReference()) { 
     119                throw $this->tooManyAttributes(); 
     120        } 
     121        if ($includes !== null && strlen($includes) > 0) { 
     122                $tok = strtok($includes, ", "); 
     123                while ($tok !== false) { 
     124                        $o = $this->createInclude(); 
     125                        $o->setName($tok); 
     126                        $tok = strtok(", "); 
     127                } 
     128        } 
     129    } 
     130 
     131 
     132        /** 
     133         * Sets the set of exclude patterns. Patterns may be separated by a comma 
     134         * or a space. 
     135         * 
     136         * @param string the string containing the exclude patterns 
     137         * @returns void 
     138     * @throws  BuildException 
     139         */ 
    142140    function setExcludes($excludes) { 
    143         if ($this->isReference()) { 
    144             throw $this->tooManyAttributes(); 
    145        
    146         if ($excludes !== null && strlen($excludes) > 0) { 
    147             $tok = strtok($excludes, ", "); 
    148             while ($tok !== false) { 
    149                 $o = $this->createExclude(); 
    150                 $o->setName($tok); 
    151                 $tok = strtok(", "); 
    152            
    153        
     141       if ($this->isReference()) { 
     142               throw $this->tooManyAttributes(); 
     143       
     144       if ($excludes !== null && strlen($excludes) > 0) { 
     145               $tok = strtok($excludes, ", "); 
     146               while ($tok !== false) { 
     147                       $o = $this->createExclude(); 
     148                       $o->setName($tok); 
     149                       $tok = strtok(", "); 
     150               
     151       
    154152    } 
    155153 
     
    161159     */ 
    162160    private function addPatternToList(&$list) { 
    163         $num = array_push($list, new PatternSetNameEntry()); 
    164         return $list[$num-1]; 
     161       $num = array_push($list, new PatternSetNameEntry()); 
     162       return $list[$num-1]; 
    165163    } 
    166164 
     
    171169     */ 
    172170    function setIncludesFile($includesFile) { 
    173         if ($this->isReference()) { 
    174             throw $this->tooManyAttributes(); 
    175        
    176         if ($includesFile instanceof File) { 
    177             $includesFile = $includesFile->getPath(); 
    178        
    179         $o = $this->createIncludesFile(); 
    180         $o->setName($includesFile); 
     171       if ($this->isReference()) { 
     172               throw $this->tooManyAttributes(); 
     173       
     174       if ($includesFile instanceof File) { 
     175               $includesFile = $includesFile->getPath(); 
     176       
     177       $o = $this->createIncludesFile(); 
     178       $o->setName($includesFile); 
    181179    } 
    182180 
     
    187185     */ 
    188186    function setExcludesFile($excludesFile) { 
    189         if ($this->isReference()) { 
    190             throw $this->tooManyAttributes(); 
    191        
    192         if ($excludesFile instanceof File) { 
    193             $excludesFile = $excludesFile->getPath(); 
    194         }         
    195         $o = $this->createExcludesFile(); 
    196         $o->setName($excludesFile); 
     187       if ($this->isReference()) { 
     188               throw $this->tooManyAttributes(); 
     189       
     190       if ($excludesFile instanceof File) { 
     191               $excludesFile = $excludesFile->getPath(); 
     192       } 
     193       $o = $this->createExcludesFile(); 
     194       $o->setName($excludesFile); 
    197195    } 
    198196 
     
    203201     */ 
    204202    private function readPatterns(PhingFile $patternfile, &$patternlist, Project $p) { 
    205         $patternReader = null; 
    206         try { 
    207             // Get a FileReader 
    208             $patternReader = new BufferedReader(new FileReader($patternfile));  
    209          
    210             // Create one NameEntry in the appropriate pattern list for each  
    211             // line in the file. 
    212             $line = $patternReader->readLine(); 
    213             while ($line !== null) { 
    214                 if (!empty($line)) { 
    215                     $line = $p->replaceProperties($line); 
    216                     $this->addPatternToList($patternlist)->setName($line); 
    217                
    218                 $line = $patternReader->readLine(); 
    219            
    220              
    221         } catch (IOException $ioe)  { 
    222             $msg = "An error occured while reading from pattern file: " . $patternfile->__toString();  
    223             if($patternReader) $patternReader->close();             
    224             throw new BuildException($msg, $ioe); 
    225         }  
    226          
    227         $patternReader->close();                 
     203       $patternReader = null; 
     204       try { 
     205               // Get a FileReader 
     206               $patternReader = new BufferedReader(new FileReader($patternfile)); 
     207 
     208               // Create one NameEntry in the appropriate pattern list for each 
     209               // line in the file. 
     210               $line = $patternReader->readLine(); 
     211               while ($line !== null) { 
     212                       if (!empty($line)) { 
     213                               $line = $p->replaceProperties($line); 
     214                               $this->addPatternToList($patternlist)->setName($line); 
     215                       
     216                       $line = $patternReader->readLine(); 
     217               
     218 
     219       } catch (IOException $ioe)  { 
     220               $msg = "An error occured while reading from pattern file: " . $patternfile->__toString(); 
     221               if($patternReader) $patternReader->close(); 
     222               throw new BuildException($msg, $ioe); 
     223       } 
     224 
     225       $patternReader->close(); 
    228226    } 
    229227 
     
    231229    /** Adds the patterns of the other instance to this set. */ 
    232230    function append($other, $p) { 
    233         if ($this->isReference()) { 
    234             throw new BuildException("Cannot append to a reference"); 
    235        
    236  
    237         $incl = $other->getIncludePatterns($p); 
    238         if ($incl !== null) { 
    239             foreach($incl as $incl_name) { 
    240                 $o = $this->createInclude(); 
    241                 $o->setName($incl_name); 
    242            
    243        
    244  
    245         $excl = $other->getExcludePatterns($p); 
    246         if ($excl !== null) { 
    247             foreach($excl as $excl_name) { 
    248                 $o = $this->createExclude(); 
    249                 $o->setName($excl_name); 
    250            
    251        
     231       if ($this->isReference()) { 
     232               throw new BuildException("Cannot append to a reference"); 
     233       
     234 
     235       $incl = $other->getIncludePatterns($p); 
     236       if ($incl !== null) { 
     237               foreach($incl as $incl_name) { 
     238                       $o = $this->createInclude(); 
     239                       $o->setName($incl_name); 
     240               
     241       
     242 
     243       $excl = $other->getExcludePatterns($p); 
     244       if ($excl !== null) { 
     245               foreach($excl as $excl_name) { 
     246                       $o = $this->createExclude(); 
     247                       $o->setName($excl_name); 
     248               
     249       
    252250    } 
    253251 
    254252    /** Returns the filtered include patterns. */ 
    255253    function getIncludePatterns(Project $p) { 
    256         if ($this->isReference()) { 
    257             $o = $this->getRef($p); 
    258             return $o->getIncludePatterns($p); 
    259         } else { 
    260             $this->readFiles($p); 
    261             return $this->makeArray($this->includeList, $p); 
    262        
     254       if ($this->isReference()) { 
     255               $o = $this->getRef($p); 
     256               return $o->getIncludePatterns($p); 
     257       } else { 
     258               $this->readFiles($p); 
     259               return $this->makeArray($this->includeList, $p); 
     260       
    263261    } 
    264262 
    265263    /** Returns the filtered exclude patterns. */ 
    266264    function getExcludePatterns(Project $p) { 
    267         if ($this->isReference()) { 
    268             $o = $this->getRef($p); 
    269             return $o->getExcludePatterns($p); 
    270         } else { 
    271             $this->readFiles($p); 
    272             return $this->makeArray($this->excludeList, $p); 
    273        
     265       if ($this->isReference()) { 
     266               $o = $this->getRef($p); 
     267               return $o->getExcludePatterns($p); 
     268       } else { 
     269               $this->readFiles($p); 
     270               return $this->makeArray($this->excludeList, $p); 
     271       
    274272    } 
    275273 
    276274    /** helper for FileSet. */ 
    277275    function hasPatterns() { 
    278         return (boolean) count($this->includesFileList) > 0 || count($this->excludesFileList) > 0 
    279                || count($this->includeList) > 0 || count($this->excludeList) > 0; 
     276       return (boolean) count($this->includesFileList) > 0 || count($this->excludesFileList) > 0 
     277       || count($this->includeList) > 0 || count($this->excludeList) > 0; 
    280278    } 
    281279 
     
    285283     */ 
    286284    function getRef(Project $p) { 
    287         if (!$this->checked) { 
    288             $stk = array(); 
    289             array_push($stk, $this); 
    290             $this->dieOnCircularReference($stk, $p); 
    291        
    292         $o = $this->ref->getReferencedObject($p); 
    293         if (!($o instanceof PatternSet)) { 
    294             $msg = $this->ref->getRefId()." doesn't denote a patternset"; 
    295             throw new BuildException($msg); 
    296         } else { 
    297             return $o; 
    298        
     285       if (!$this->checked) { 
     286               $stk = array(); 
     287               array_push($stk, $this); 
     288               $this->dieOnCircularReference($stk, $p); 
     289       
     290       $o = $this->ref->getReferencedObject($p); 
     291       if (!($o instanceof PatternSet)) { 
     292               $msg = $this->ref->getRefId()." doesn't denote a patternset"; 
     293               throw new BuildException($msg); 
     294       } else { 
     295               return $o; 
     296       
    299297    } 
    300298 
     
    302300    private function makeArray(&$list, Project $p) { 
    303301 
    304         if (count($list) === 0) { 
    305             return null; 
    306        
    307  
    308         $tmpNames = array(); 
    309         foreach($list as $ne) { 
    310             $pattern = (string) $ne->evalName($p); 
    311             if ($pattern !== null && strlen($pattern) > 0) { 
    312                 array_push($tmpNames, $pattern); 
    313            
    314        
    315         return $tmpNames; 
     302       if (count($list) === 0) { 
     303               return null; 
     304       
     305 
     306       $tmpNames = array(); 
     307       foreach($list as $ne) { 
     308               $pattern = (string) $ne->evalName($p); 
     309               if ($pattern !== null && strlen($pattern) > 0) { 
     310                       array_push($tmpNames, $pattern); 
     311               
     312       
     313       return $tmpNames; 
    316314    } 
    317315 
    318316    /** Read includesfile or excludesfile if not already done so. */ 
    319317    private function readFiles(Project $p) { 
    320         if (!empty($this->includesFileList)) { 
    321             foreach($this->includesFileList as $ne) { 
    322                 $fileName = (string) $ne->evalName($p); 
    323                 if ($fileName !== null) { 
    324                     $inclFile = $p->resolveFile($fileName); 
    325                     if (!$inclFile->exists()) { 
    326                         throw new BuildException("Includesfile ".$inclFile->getAbsolutePath()." not found."); 
    327                    
    328                     $this->readPatterns($inclFile, $this->includeList, $p); 
    329                
    330            
    331             $this->includesFileList = array(); 
    332        
    333  
    334         if (!empty($this->excludesFileList)) { 
    335             foreach($this->excludesFileList as $ne) {                
    336                 $fileName = (string) $ne->evalName($p); 
    337                 if ($fileName !== null) { 
    338                     $exclFile = $p->resolveFile($fileName); 
    339                     if (!$exclFile->exists()) { 
    340                         throw new BuildException("Excludesfile ".$exclFile->getAbsolutePath()." not found."); 
    341                         return; 
    342                    
    343                     $this->readPatterns($exclFile, $this->excludeList, $p); 
    344                
    345            
    346             $this->excludesFileList = array(); 
    347        
     318       if (!empty($this->includesFileList)) { 
     319               foreach($this->includesFileList as $ne) { 
     320                       $fileName = (string) $ne->evalName($p); 
     321                       if ($fileName !== null) { 
     322                               $inclFile = $p->resolveFile($fileName); 
     323                               if (!$inclFile->exists()) { 
     324                                       throw new BuildException("Includesfile ".$inclFile->getAbsolutePath()." not found."); 
     325                               
     326                               $this->readPatterns($inclFile, $this->includeList, $p); 
     327                       
     328               
     329               $this->includesFileList = array(); 
     330       
     331 
     332       if (!empty($this->excludesFileList)) { 
     333               foreach($this->excludesFileList as $ne) { 
     334                       $fileName = (string) $ne->evalName($p); 
     335                       if ($fileName !== null) { 
     336                               $exclFile = $p->resolveFile($fileName); 
     337                               if (!$exclFile->exists()) { 
     338                                       throw new BuildException("Excludesfile ".$exclFile->getAbsolutePath()." not found."); 
     339                                       return; 
     340                               
     341                               $this->readPatterns($exclFile, $this->excludeList, $p); 
     342                       
     343               
     344               $this->excludesFileList = array(); 
     345       
    348346    } 
    349347 
    350348 
    351349    function toString() { 
    352          
    353         // We can't compile includeList into array because, toString() does 
    354         // not know about project: 
    355         // 
    356         // $includes = $this->makeArray($this->includeList, $this->project); 
    357         // $excludes = $this->makeArray($this->excludeList, $this->project); 
    358              
    359         if (empty($this->includeList)) { 
    360             $includes = "empty"; 
    361         } else { 
    362             $includes = ""; 
    363             foreach($this->includeList as $ne) { 
    364                 $includes .= $ne->toString() . ","; 
    365            
    366             $includes = rtrim($includes, ","); 
    367        
    368          
    369         if (empty($this->excludeList)) { 
    370             $excludes = "empty"; 
    371         } else { 
    372             $excludes = ""; 
    373             foreach($this->excludeList as $ne) { 
    374                 $excludes .= $ne->toString() . ","; 
    375            
    376             $excludes = rtrim($excludes, ","); 
    377        
    378                 
    379         return "patternSet{ includes: $includes  excludes: $excludes }"; 
     350 
     351       // We can't compile includeList into array because, toString() does 
     352       // not know about project: 
     353       // 
     354       // $includes = $this->makeArray($this->includeList, $this->project); 
     355       // $excludes = $this->makeArray($this->excludeList, $this->project); 
     356 
     357       if (empty($this->includeList)) { 
     358               $includes = "empty"; 
     359       } else { 
     360               $includes = ""; 
     361               foreach($this->includeList as $ne) { 
     362                       $includes .= $ne->toString() . ","; 
     363               
     364               $includes = rtrim($includes, ","); 
     365       
     366 
     367       if (empty($this->excludeList)) { 
     368               $excludes = "empty"; 
     369       } else { 
     370               $excludes = ""; 
     371               foreach($this->excludeList as $ne) { 
     372                       $excludes .= $ne->toString() . ","; 
     373               
     374               $excludes = rtrim($excludes, ","); 
     375       
     376 
     377       return "patternSet{ includes: $includes  excludes: $excludes }"; 
    380378    } 
    381379} 
    382380 
    383381 
    384 /* 
    385  * Note, this class here should become a nested class to 
    386  * PatternSet (PatternSet:NameEntry) as it is only needed 
    387  * internally. 
    388  * This is not possible with php 4.x right now so we place 
    389  * this class (against good style) in this file. 
     382/** 
     383 * "Internal" class for holding an include/exclude pattern. 
    390384 */ 
    391  
    392385class PatternSetNameEntry { 
    393386 
    394     private $name       = null; 
    395     private $ifCond     = null; 
    396     private $unlessCond = null; 
    397  
    398     function setName($name) { 
    399         $this->name = (string) $name; 
    400     } 
    401  
    402  
    403     function setIf($cond) { 
    404         $this->ifCond = (string) $cond; 
    405     } 
    406  
    407  
    408     function setUnless($cond) { 
    409         $this->unlessCond = (string) $cond; 
    410     } 
    411  
    412  
    413     function getName() { 
    414         return $this->name; 
    415     } 
    416  
    417  
    418     function evalName($project) { 
    419         return $this->valid($project) ? $this->name : null; 
    420     } 
    421  
    422  
    423     function valid($project) { 
    424         if ($this->ifCond !== null && $project->getProperty($this->ifCond) === null) { 
    425             return false; 
    426         } else if ($this->unlessCond !== null && $project->getProperty($this->unlessCond) !== null) { 
    427             return false; 
    428         } 
    429         return true; 
    430     } 
    431  
    432  
    433     function toString() { 
    434         $buf = $this->name; 
    435         if (($this->ifCond !== null) || ($this->unlessCond !== null)) { 
    436             $buf .= ":"; 
    437             $connector = ""; 
    438  
    439             if ($this->ifCond !== null) { 
    440                 $buf .= "if->{$this->ifCond}"; 
    441                 $connector = ";"; 
    442             } 
    443             if ($this->unlessCond !== null) { 
    444                 $buf .= "$connector unless->{$this->unlessCond}"; 
    445             } 
    446         } 
    447         return $buf; 
    448     } 
     387        /** 
     388         * The pattern. 
     389         * @var string 
     390         */ 
     391        private $name; 
     392 
     393    /** 
     394     * The if-condition property for this pattern to be applied. 
     395     * @var string 
     396     */ 
     397    private $ifCond; 
     398 
     399    /** 
     400     * The unless-condition property for this pattern to be applied. 
     401     * @var string  
     402     */ 
     403    private $unlessCond; 
     404 
     405    /** 
     406     * An alias for the setName() method. 
     407     * @see setName() 
     408     * @param string $pattern 
     409     */ 
     410    public function setPattern($pattern) { 
     411        $this->setName($pattern); 
     412    } 
     413 
     414    /** 
     415     * Set the pattern text. 
     416     * @param string $name The pattern 
     417     */ 
     418    public function setName($name) { 
     419        $this->name = (string) $name; 
     420    } 
     421 
     422        /** 
     423         * Sets an if-condition property for this pattern to match. 
     424         * @param string $cond 
     425         */ 
     426        public function setIf($cond) { 
     427                $this->ifCond = (string) $cond; 
     428        } 
     429 
     430 
     431        /** 
     432         * Sets an unless-condition property for this pattern to match. 
     433         * @param string $cond 
     434         */ 
     435        public function setUnless($cond) { 
     436                $this->unlessCond = (string) $cond; 
     437        } 
     438 
     439        /** 
     440         * Get the pattern text. 
     441         * @return string The pattern. 
     442         */ 
     443        public function getName() { 
     444                return $this->name; 
     445        } 
     446 
     447        /** 
     448         * Evaluates the pattern. 
     449         * @return string The pattern or null if it is ruled out by a condition.  
     450         */ 
     451        public function evalName(Project $project) { 
     452                return $this->valid($project) ? $this->name : null; 
     453        } 
     454 
     455 
     456    /** 
     457     * Checks whether pattern should be applied based on whether the if and unless 
     458     * properties are set in project. 
     459     * @param Project $project 
     460     * @return boolean 
     461     */ 
     462    public function valid(Project $project) { 
     463        if ($this->ifCond !== null && $project->getProperty($this->ifCond) === null) { 
     464                return false; 
     465        } else if ($this->unlessCond !== null && $project->getProperty($this->unlessCond) !== null) { 
     466                return false; 
     467        } 
     468        return true; 
     469    } 
     470 
     471        /** 
     472         * Gets a string representation of this pattern. 
     473         * @return string 
     474         */ 
     475        public function toString() { 
     476                $buf = $this->name; 
     477                if (($this->ifCond !== null) || ($this->unlessCond !== null)) { 
     478                        $buf .= ":"; 
     479                        $connector = ""; 
     480 
     481                        if ($this->ifCond !== null) { 
     482                                $buf .= "if->{$this->ifCond}"; 
     483                                $connector = ";"; 
     484                        } 
     485                        if ($this->unlessCond !== null) { 
     486                                $buf .= "$connector unless->{$this->unlessCond}"; 
     487                        } 
     488                } 
     489                return $buf; 
     490        } 
    449491}