[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: PatternSet.php 3076 2006-12-18 08:52:12Z fabien $ 4 * 5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 * 17 * This software consists of voluntary contributions made by many individuals 18 * and is licensed under the LGPL. For more information please see 19 * <http://phing.info>. 20 */ 21 22 include_once 'phing/system/io/FileReader.php'; 23 include_once 'phing/types/DataType.php'; 24 25 /** 26 * The patternset storage component. Carries all necessary data and methods 27 * for the patternset stuff. 28 * 29 * @author Andreas Aderhold, andi@binarycloud.com 30 * @version $Revision: 1.8 $ 31 * @package phing.types 32 */ 33 class PatternSet extends DataType { 34 35 private $includeList = array(); 36 private $excludeList = array(); 37 private $includesFileList = array(); 38 private $excludesFileList = array(); 39 40 /** 41 * Makes this instance in effect a reference to another PatternSet 42 * instance. 43 * You must not set another attribute or nest elements inside 44 * this element if you make it a reference. 45 */ 46 function setRefid(Reference $r) { 47 if (!empty($this->includeList) || !empty($this->excludeList)) { 48 throw $this->tooManyAttributes(); 49 } 50 parent::setRefid($r); 51 } 52 53 54 /** 55 * Add a name entry on the include list 56 * 57 * @returns PatternSetNameEntry Reference to object 58 * @throws BuildException 59 */ 60 function createInclude() { 61 if ($this->isReference()) { 62 throw $this->noChildrenAllowed(); 63 } 64 return $this->addPatternToList($this->includeList); 65 } 66 67 68 /** 69 * Add a name entry on the include files list 70 * 71 * @returns PatternSetNameEntry Reference to object 72 * @throws BuildException 73 */ 74 function createIncludesFile() { 75 if ($this->isReference()) { 76 throw $this->noChildrenAllowed(); 77 } 78 return $this->addPatternToList($this->includesFileList); 79 } 80 81 /** 82 * Add a name entry on the exclude list 83 * 84 * @returns PatternSetNameEntry Reference to object 85 * @throws BuildException 86 */ 87 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 101 function createExcludesFile() { 102 if ($this->isReference()) { 103 throw $this->noChildrenAllowed(); 104 return; 105 } 106 return $this->addPatternToList($this->excludesFileList); 107 } 108 109 110 /** 111 * Sets the set of include patterns. Patterns may be separated by a comma 112 * or a space. 113 * 114 * @param string the string containing the include patterns 115 * @returns void 116 * @throws BuildException 117 */ 118 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 142 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 } 154 } 155 156 /** 157 * add a name entry to the given list 158 * 159 * @param array List onto which the nameentry should be added 160 * @returns PatternSetNameEntry Reference to the created PsetNameEntry instance 161 */ 162 private function addPatternToList(&$list) { 163 $num = array_push($list, new PatternSetNameEntry()); 164 return $list[$num-1]; 165 } 166 167 /** 168 * Sets the name of the file containing the includes patterns. 169 * 170 * @param includesFile The file to fetch the include patterns from. 171 */ 172 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); 181 } 182 183 /** 184 * Sets the name of the file containing the excludes patterns. 185 * 186 * @param excludesFile The file to fetch the exclude patterns from. 187 */ 188 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); 197 } 198 199 200 /** 201 * Reads path matching patterns from a file and adds them to the 202 * includes or excludes list 203 */ 204 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(); 228 } 229 230 231 /** Adds the patterns of the other instance to this set. */ 232 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 } 252 } 253 254 /** Returns the filtered include patterns. */ 255 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 } 263 } 264 265 /** Returns the filtered exclude patterns. */ 266 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 } 274 } 275 276 /** helper for FileSet. */ 277 function hasPatterns() { 278 return (boolean) count($this->includesFileList) > 0 || count($this->excludesFileList) > 0 279 || count($this->includeList) > 0 || count($this->excludeList) > 0; 280 } 281 282 /** 283 * Performs the check for circular references and returns the 284 * referenced PatternSet. 285 */ 286 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 } 299 } 300 301 /** Convert a array of PatternSetNameEntry elements into an array of Strings. */ 302 private function makeArray(&$list, Project $p) { 303 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; 316 } 317 318 /** Read includesfile or excludesfile if not already done so. */ 319 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 } 348 } 349 350 351 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 }"; 380 } 381 } 382 383 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. 390 */ 391 392 class PatternSetNameEntry { 393 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 } 449 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |