Index: trunk/phase3/includes/libs/sfFinder.php |
— | — | @@ -0,0 +1,824 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/* |
| 5 | + * This file is part of the symfony package. |
| 6 | + * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * |
| 15 | + * Allow to build rules to find files and directories. |
| 16 | + * |
| 17 | + * All rules may be invoked several times, except for ->in() method. |
| 18 | + * Some rules are cumulative (->name() for example) whereas others are destructive |
| 19 | + * (most recent value is used, ->maxdepth() method for example). |
| 20 | + * |
| 21 | + * All methods return the current sfFinder object to allow easy chaining: |
| 22 | + * |
| 23 | + * $files = sfFinder::type('file')->name('*.php')->in(.); |
| 24 | + * |
| 25 | + * Interface loosely based on perl File::Find::Rule module. |
| 26 | + * |
| 27 | + * @license http://www.opensource.org/licenses/mit-license.php MIT License |
| 28 | + * @author Fabien Potencier <fabien.potencier@symfony-project.com> |
| 29 | + * @version SVN: $Id$ |
| 30 | + */ |
| 31 | +class sfFinder |
| 32 | +{ |
| 33 | + protected $type = 'file'; |
| 34 | + protected $names = array(); |
| 35 | + protected $prunes = array(); |
| 36 | + protected $discards = array(); |
| 37 | + protected $execs = array(); |
| 38 | + protected $mindepth = 0; |
| 39 | + protected $sizes = array(); |
| 40 | + protected $maxdepth = 1000000; |
| 41 | + protected $relative = false; |
| 42 | + protected $follow_link = false; |
| 43 | + protected $sort = false; |
| 44 | + protected $ignore_version_control = true; |
| 45 | + |
| 46 | + /** |
| 47 | + * Sets maximum directory depth. |
| 48 | + * |
| 49 | + * Finder will descend at most $level levels of directories below the starting point. |
| 50 | + * |
| 51 | + * @param int $level |
| 52 | + * @return sfFinder current sfFinder object |
| 53 | + */ |
| 54 | + public function maxdepth($level) |
| 55 | + { |
| 56 | + $this->maxdepth = $level; |
| 57 | + |
| 58 | + return $this; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Sets minimum directory depth. |
| 63 | + * |
| 64 | + * Finder will start applying tests at level $level. |
| 65 | + * |
| 66 | + * @param int $level |
| 67 | + * @return sfFinder current sfFinder object |
| 68 | + */ |
| 69 | + public function mindepth($level) |
| 70 | + { |
| 71 | + $this->mindepth = $level; |
| 72 | + |
| 73 | + return $this; |
| 74 | + } |
| 75 | + |
| 76 | + public function get_type() |
| 77 | + { |
| 78 | + return $this->type; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Sets the type of elements to returns. |
| 83 | + * |
| 84 | + * @param string $name directory or file or any (for both file and directory) |
| 85 | + * @return sfFinder new sfFinder object |
| 86 | + */ |
| 87 | + public static function type($name) |
| 88 | + { |
| 89 | + $finder = new self(); |
| 90 | + return $finder->setType($name); |
| 91 | + } |
| 92 | + /** |
| 93 | + * Sets the type of elements to returns. |
| 94 | + * |
| 95 | + * @param string $name directory or file or any (for both file and directory) |
| 96 | + * @return sfFinder Current object |
| 97 | + */ |
| 98 | + public function setType($name) |
| 99 | + { |
| 100 | + $name = strtolower($name); |
| 101 | + |
| 102 | + if (substr($name, 0, 3) === 'dir') |
| 103 | + { |
| 104 | + $this->type = 'directory'; |
| 105 | + |
| 106 | + return $this; |
| 107 | + } |
| 108 | + if ($name === 'any') |
| 109 | + { |
| 110 | + $this->type = 'any'; |
| 111 | + |
| 112 | + return $this; |
| 113 | + } |
| 114 | + |
| 115 | + $this->type = 'file'; |
| 116 | + |
| 117 | + return $this; |
| 118 | + } |
| 119 | + |
| 120 | + /* |
| 121 | + * glob, patterns (must be //) or strings |
| 122 | + */ |
| 123 | + protected function to_regex($str) |
| 124 | + { |
| 125 | + if (preg_match('/^(!)?([^a-zA-Z0-9\\\\]).+?\\2[ims]?$/', $str)) |
| 126 | + { |
| 127 | + return $str; |
| 128 | + } |
| 129 | + |
| 130 | + return sfGlobToRegex::glob_to_regex($str); |
| 131 | + } |
| 132 | + |
| 133 | + protected function args_to_array($arg_list, $not = false) |
| 134 | + { |
| 135 | + $list = array(); |
| 136 | + $nbArgList = count($arg_list); |
| 137 | + for ($i = 0; $i < $nbArgList; $i++) |
| 138 | + { |
| 139 | + if (is_array($arg_list[$i])) |
| 140 | + { |
| 141 | + foreach ($arg_list[$i] as $arg) |
| 142 | + { |
| 143 | + $list[] = array($not, $this->to_regex($arg)); |
| 144 | + } |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + $list[] = array($not, $this->to_regex($arg_list[$i])); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return $list; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Adds rules that files must match. |
| 157 | + * |
| 158 | + * You can use patterns (delimited with / sign), globs or simple strings. |
| 159 | + * |
| 160 | + * $finder->name('*.php') |
| 161 | + * $finder->name('/\.php$/') // same as above |
| 162 | + * $finder->name('test.php') |
| 163 | + * |
| 164 | + * @param list a list of patterns, globs or strings |
| 165 | + * @return sfFinder Current object |
| 166 | + */ |
| 167 | + public function name() |
| 168 | + { |
| 169 | + $args = func_get_args(); |
| 170 | + $this->names = array_merge($this->names, $this->args_to_array($args)); |
| 171 | + |
| 172 | + return $this; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Adds rules that files must not match. |
| 177 | + * |
| 178 | + * @see ->name() |
| 179 | + * @param list a list of patterns, globs or strings |
| 180 | + * @return sfFinder Current object |
| 181 | + */ |
| 182 | + public function not_name() |
| 183 | + { |
| 184 | + $args = func_get_args(); |
| 185 | + $this->names = array_merge($this->names, $this->args_to_array($args, true)); |
| 186 | + |
| 187 | + return $this; |
| 188 | + } |
| 189 | + |
| 190 | + public function notName() { |
| 191 | + $args = func_get_args(); |
| 192 | + return call_user_func_array( array( $this, 'not_name' ), $args ); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Adds tests for file sizes. |
| 197 | + * |
| 198 | + * $finder->size('> 10K'); |
| 199 | + * $finder->size('<= 1Ki'); |
| 200 | + * $finder->size(4); |
| 201 | + * |
| 202 | + * @param list a list of comparison strings |
| 203 | + * @return sfFinder Current object |
| 204 | + */ |
| 205 | + public function size() |
| 206 | + { |
| 207 | + $args = func_get_args(); |
| 208 | + $numargs = count($args); |
| 209 | + for ($i = 0; $i < $numargs; $i++) |
| 210 | + { |
| 211 | + $this->sizes[] = new sfNumberCompare($args[$i]); |
| 212 | + } |
| 213 | + |
| 214 | + return $this; |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Traverses no further. |
| 219 | + * |
| 220 | + * @param list a list of patterns, globs to match |
| 221 | + * @return sfFinder Current object |
| 222 | + */ |
| 223 | + public function prune() |
| 224 | + { |
| 225 | + $args = func_get_args(); |
| 226 | + $this->prunes = array_merge($this->prunes, $this->args_to_array($args)); |
| 227 | + |
| 228 | + return $this; |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Discards elements that matches. |
| 233 | + * |
| 234 | + * @param list a list of patterns, globs to match |
| 235 | + * @return sfFinder Current object |
| 236 | + */ |
| 237 | + public function discard() |
| 238 | + { |
| 239 | + $args = func_get_args(); |
| 240 | + $this->discards = array_merge($this->discards, $this->args_to_array($args)); |
| 241 | + |
| 242 | + return $this; |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * Ignores version control directories. |
| 247 | + * |
| 248 | + * Currently supports Subversion, CVS, DARCS, Gnu Arch, Monotone, Bazaar-NG, GIT, Mercurial |
| 249 | + * |
| 250 | + * @param bool $ignore falase when version control directories shall be included (default is true) |
| 251 | + * |
| 252 | + * @return sfFinder Current object |
| 253 | + */ |
| 254 | + public function ignore_version_control($ignore = true) |
| 255 | + { |
| 256 | + $this->ignore_version_control = $ignore; |
| 257 | + |
| 258 | + return $this; |
| 259 | + } |
| 260 | + |
| 261 | + public function ignoreVersionControl($ignore = true) |
| 262 | + { |
| 263 | + return $this->ignore_version_control($ignore); |
| 264 | + } |
| 265 | + |
| 266 | + public function ignoreVC($ignore = true) |
| 267 | + { |
| 268 | + return $this->ignore_version_control($ignore); |
| 269 | + } |
| 270 | + |
| 271 | + /** |
| 272 | + * Returns files and directories ordered by name |
| 273 | + * |
| 274 | + * @return sfFinder Current object |
| 275 | + */ |
| 276 | + public function sort_by_name() |
| 277 | + { |
| 278 | + $this->sort = 'name'; |
| 279 | + |
| 280 | + return $this; |
| 281 | + } |
| 282 | + |
| 283 | + public function sortByName() |
| 284 | + { |
| 285 | + return $this->sort_by_name(); |
| 286 | + } |
| 287 | + |
| 288 | + /** |
| 289 | + * Returns files and directories ordered by type (directories before files), then by name |
| 290 | + * |
| 291 | + * @return sfFinder Current object |
| 292 | + */ |
| 293 | + public function sort_by_type() |
| 294 | + { |
| 295 | + $this->sort = 'type'; |
| 296 | + |
| 297 | + return $this; |
| 298 | + } |
| 299 | + |
| 300 | + public function sortByType() |
| 301 | + { |
| 302 | + return $this->sort_by_type(); |
| 303 | + } |
| 304 | + |
| 305 | + /** |
| 306 | + * Executes function or method for each element. |
| 307 | + * |
| 308 | + * Element match if functino or method returns true. |
| 309 | + * |
| 310 | + * $finder->exec('myfunction'); |
| 311 | + * $finder->exec(array($object, 'mymethod')); |
| 312 | + * |
| 313 | + * @param mixed function or method to call |
| 314 | + * @return sfFinder Current object |
| 315 | + */ |
| 316 | + public function exec() |
| 317 | + { |
| 318 | + $args = func_get_args(); |
| 319 | + $numargs = count($args); |
| 320 | + for ($i = 0; $i < $numargs; $i++) |
| 321 | + { |
| 322 | + if (is_array($args[$i]) && !method_exists($args[$i][0], $args[$i][1])) |
| 323 | + { |
| 324 | + throw new MWException(sprintf('method "%s" does not exist for object "%s".', $args[$i][1], $args[$i][0])); |
| 325 | + } |
| 326 | + if (!is_array($args[$i]) && !function_exists($args[$i])) |
| 327 | + { |
| 328 | + throw new MWException(sprintf('function "%s" does not exist.', $args[$i])); |
| 329 | + } |
| 330 | + |
| 331 | + $this->execs[] = $args[$i]; |
| 332 | + } |
| 333 | + |
| 334 | + return $this; |
| 335 | + } |
| 336 | + |
| 337 | + /** |
| 338 | + * Returns relative paths for all files and directories. |
| 339 | + * |
| 340 | + * @return sfFinder Current object |
| 341 | + */ |
| 342 | + public function relative() |
| 343 | + { |
| 344 | + $this->relative = true; |
| 345 | + |
| 346 | + return $this; |
| 347 | + } |
| 348 | + |
| 349 | + /** |
| 350 | + * Symlink following. |
| 351 | + * |
| 352 | + * @return sfFinder Current object |
| 353 | + */ |
| 354 | + public function follow_link() |
| 355 | + { |
| 356 | + $this->follow_link = true; |
| 357 | + |
| 358 | + return $this; |
| 359 | + } |
| 360 | + |
| 361 | + public function followLink() { |
| 362 | + return $this->follow_link(); |
| 363 | + } |
| 364 | + |
| 365 | + /** |
| 366 | + * Searches files and directories which match defined rules. |
| 367 | + * |
| 368 | + * @return array list of files and directories |
| 369 | + */ |
| 370 | + public function in() |
| 371 | + { |
| 372 | + $files = array(); |
| 373 | + $here_dir = getcwd(); |
| 374 | + |
| 375 | + $finder = clone $this; |
| 376 | + |
| 377 | + if ($this->ignore_version_control) |
| 378 | + { |
| 379 | + $ignores = array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg'); |
| 380 | + |
| 381 | + $finder->discard($ignores)->prune($ignores); |
| 382 | + } |
| 383 | + |
| 384 | + // first argument is an array? |
| 385 | + $numargs = func_num_args(); |
| 386 | + $arg_list = func_get_args(); |
| 387 | + if ($numargs === 1 && is_array($arg_list[0])) |
| 388 | + { |
| 389 | + $arg_list = $arg_list[0]; |
| 390 | + $numargs = count($arg_list); |
| 391 | + } |
| 392 | + |
| 393 | + for ($i = 0; $i < $numargs; $i++) |
| 394 | + { |
| 395 | + $dir = realpath($arg_list[$i]); |
| 396 | + |
| 397 | + if (!is_dir($dir)) |
| 398 | + { |
| 399 | + continue; |
| 400 | + } |
| 401 | + |
| 402 | + $dir = str_replace('\\', '/', $dir); |
| 403 | + |
| 404 | + // absolute path? |
| 405 | + if (!self::isPathAbsolute($dir)) |
| 406 | + { |
| 407 | + $dir = $here_dir.'/'.$dir; |
| 408 | + } |
| 409 | + |
| 410 | + $new_files = str_replace('\\', '/', $finder->search_in($dir)); |
| 411 | + |
| 412 | + if ($this->relative) |
| 413 | + { |
| 414 | + $new_files = str_replace(rtrim($dir, '/').'/', '', $new_files); |
| 415 | + } |
| 416 | + |
| 417 | + $files = array_merge($files, $new_files); |
| 418 | + } |
| 419 | + |
| 420 | + if ($this->sort === 'name') |
| 421 | + { |
| 422 | + sort($files); |
| 423 | + } |
| 424 | + |
| 425 | + return array_unique($files); |
| 426 | + } |
| 427 | + |
| 428 | + protected function search_in($dir, $depth = 0) |
| 429 | + { |
| 430 | + if ($depth > $this->maxdepth) |
| 431 | + { |
| 432 | + return array(); |
| 433 | + } |
| 434 | + |
| 435 | + $dir = realpath($dir); |
| 436 | + |
| 437 | + if ((!$this->follow_link) && is_link($dir)) |
| 438 | + { |
| 439 | + return array(); |
| 440 | + } |
| 441 | + |
| 442 | + $files = array(); |
| 443 | + $temp_files = array(); |
| 444 | + $temp_folders = array(); |
| 445 | + if (is_dir($dir) && is_readable($dir)) |
| 446 | + { |
| 447 | + $current_dir = opendir($dir); |
| 448 | + while (false !== $entryname = readdir($current_dir)) |
| 449 | + { |
| 450 | + if ($entryname == '.' || $entryname == '..') continue; |
| 451 | + |
| 452 | + $current_entry = $dir.DIRECTORY_SEPARATOR.$entryname; |
| 453 | + if ((!$this->follow_link) && is_link($current_entry)) |
| 454 | + { |
| 455 | + continue; |
| 456 | + } |
| 457 | + |
| 458 | + if (is_dir($current_entry)) |
| 459 | + { |
| 460 | + if ($this->sort === 'type') |
| 461 | + { |
| 462 | + $temp_folders[$entryname] = $current_entry; |
| 463 | + } |
| 464 | + else |
| 465 | + { |
| 466 | + if (($this->type === 'directory' || $this->type === 'any') && ($depth >= $this->mindepth) && !$this->is_discarded($dir, $entryname) && $this->match_names($dir, $entryname) && $this->exec_ok($dir, $entryname)) |
| 467 | + { |
| 468 | + $files[] = $current_entry; |
| 469 | + } |
| 470 | + |
| 471 | + if (!$this->is_pruned($dir, $entryname)) |
| 472 | + { |
| 473 | + $files = array_merge($files, $this->search_in($current_entry, $depth + 1)); |
| 474 | + } |
| 475 | + } |
| 476 | + } |
| 477 | + else |
| 478 | + { |
| 479 | + if (($this->type !== 'directory' || $this->type === 'any') && ($depth >= $this->mindepth) && !$this->is_discarded($dir, $entryname) && $this->match_names($dir, $entryname) && $this->size_ok($dir, $entryname) && $this->exec_ok($dir, $entryname)) |
| 480 | + { |
| 481 | + if ($this->sort === 'type') |
| 482 | + { |
| 483 | + $temp_files[] = $current_entry; |
| 484 | + } |
| 485 | + else |
| 486 | + { |
| 487 | + $files[] = $current_entry; |
| 488 | + } |
| 489 | + } |
| 490 | + } |
| 491 | + } |
| 492 | + |
| 493 | + if ($this->sort === 'type') |
| 494 | + { |
| 495 | + ksort($temp_folders); |
| 496 | + foreach($temp_folders as $entryname => $current_entry) |
| 497 | + { |
| 498 | + if (($this->type === 'directory' || $this->type === 'any') && ($depth >= $this->mindepth) && !$this->is_discarded($dir, $entryname) && $this->match_names($dir, $entryname) && $this->exec_ok($dir, $entryname)) |
| 499 | + { |
| 500 | + $files[] = $current_entry; |
| 501 | + } |
| 502 | + |
| 503 | + if (!$this->is_pruned($dir, $entryname)) |
| 504 | + { |
| 505 | + $files = array_merge($files, $this->search_in($current_entry, $depth + 1)); |
| 506 | + } |
| 507 | + } |
| 508 | + |
| 509 | + sort($temp_files); |
| 510 | + $files = array_merge($files, $temp_files); |
| 511 | + } |
| 512 | + |
| 513 | + closedir($current_dir); |
| 514 | + } |
| 515 | + |
| 516 | + return $files; |
| 517 | + } |
| 518 | + |
| 519 | + protected function match_names($dir, $entry) |
| 520 | + { |
| 521 | + if (!count($this->names)) return true; |
| 522 | + |
| 523 | + // Flags indicating that there was attempts to match |
| 524 | + // at least one "not_name" or "name" rule respectively |
| 525 | + // to following variables: |
| 526 | + $one_not_name_rule = false; |
| 527 | + $one_name_rule = false; |
| 528 | + |
| 529 | + foreach ($this->names as $args) |
| 530 | + { |
| 531 | + list($not, $regex) = $args; |
| 532 | + $not ? $one_not_name_rule = true : $one_name_rule = true; |
| 533 | + if (preg_match($regex, $entry)) |
| 534 | + { |
| 535 | + // We must match ONLY ONE "not_name" or "name" rule: |
| 536 | + // if "not_name" rule matched then we return "false" |
| 537 | + // if "name" rule matched then we return "true" |
| 538 | + return $not ? false : true; |
| 539 | + } |
| 540 | + } |
| 541 | + |
| 542 | + if ($one_not_name_rule && $one_name_rule) |
| 543 | + { |
| 544 | + return false; |
| 545 | + } |
| 546 | + else if ($one_not_name_rule) |
| 547 | + { |
| 548 | + return true; |
| 549 | + } |
| 550 | + else if ($one_name_rule) |
| 551 | + { |
| 552 | + return false; |
| 553 | + } |
| 554 | + return true; |
| 555 | + } |
| 556 | + |
| 557 | + protected function size_ok($dir, $entry) |
| 558 | + { |
| 559 | + if (0 === count($this->sizes)) return true; |
| 560 | + |
| 561 | + if (!is_file($dir.DIRECTORY_SEPARATOR.$entry)) return true; |
| 562 | + |
| 563 | + $filesize = filesize($dir.DIRECTORY_SEPARATOR.$entry); |
| 564 | + foreach ($this->sizes as $number_compare) |
| 565 | + { |
| 566 | + if (!$number_compare->test($filesize)) return false; |
| 567 | + } |
| 568 | + |
| 569 | + return true; |
| 570 | + } |
| 571 | + |
| 572 | + protected function is_pruned($dir, $entry) |
| 573 | + { |
| 574 | + if (0 === count($this->prunes)) return false; |
| 575 | + |
| 576 | + foreach ($this->prunes as $args) |
| 577 | + { |
| 578 | + $regex = $args[1]; |
| 579 | + if (preg_match($regex, $entry)) return true; |
| 580 | + } |
| 581 | + |
| 582 | + return false; |
| 583 | + } |
| 584 | + |
| 585 | + protected function is_discarded($dir, $entry) |
| 586 | + { |
| 587 | + if (0 === count($this->discards)) return false; |
| 588 | + |
| 589 | + foreach ($this->discards as $args) |
| 590 | + { |
| 591 | + $regex = $args[1]; |
| 592 | + if (preg_match($regex, $entry)) return true; |
| 593 | + } |
| 594 | + |
| 595 | + return false; |
| 596 | + } |
| 597 | + |
| 598 | + protected function exec_ok($dir, $entry) |
| 599 | + { |
| 600 | + if (0 === count($this->execs)) return true; |
| 601 | + |
| 602 | + foreach ($this->execs as $exec) |
| 603 | + { |
| 604 | + if (!call_user_func_array($exec, array($dir, $entry))) return false; |
| 605 | + } |
| 606 | + |
| 607 | + return true; |
| 608 | + } |
| 609 | + |
| 610 | + public static function isPathAbsolute($path) |
| 611 | + { |
| 612 | + if ($path{0} === '/' || $path{0} === '\\' || |
| 613 | + (strlen($path) > 3 && ctype_alpha($path{0}) && |
| 614 | + $path{1} === ':' && |
| 615 | + ($path{2} === '\\' || $path{2} === '/') |
| 616 | + ) |
| 617 | + ) |
| 618 | + { |
| 619 | + return true; |
| 620 | + } |
| 621 | + |
| 622 | + return false; |
| 623 | + } |
| 624 | +} |
| 625 | + |
| 626 | +/** |
| 627 | + * Match globbing patterns against text. |
| 628 | + * |
| 629 | + * if match_glob("foo.*", "foo.bar") echo "matched\n"; |
| 630 | + * |
| 631 | + * // prints foo.bar and foo.baz |
| 632 | + * $regex = glob_to_regex("foo.*"); |
| 633 | + * for (array('foo.bar', 'foo.baz', 'foo', 'bar') as $t) |
| 634 | + * { |
| 635 | + * if (/$regex/) echo "matched: $car\n"; |
| 636 | + * } |
| 637 | + * |
| 638 | + * sfGlobToRegex implements glob(3) style matching that can be used to match |
| 639 | + * against text, rather than fetching names from a filesystem. |
| 640 | + * |
| 641 | + * based on perl Text::Glob module. |
| 642 | + * |
| 643 | + * @package symfony |
| 644 | + * @subpackage util |
| 645 | + * @author Fabien Potencier <fabien.potencier@gmail.com> php port |
| 646 | + * @author Richard Clamp <richardc@unixbeard.net> perl version |
| 647 | + * @copyright 2004-2005 Fabien Potencier <fabien.potencier@gmail.com> |
| 648 | + * @copyright 2002 Richard Clamp <richardc@unixbeard.net> |
| 649 | + * @version SVN: $Id$ |
| 650 | + */ |
| 651 | +class sfGlobToRegex |
| 652 | +{ |
| 653 | + protected static $strict_leading_dot = true; |
| 654 | + protected static $strict_wildcard_slash = true; |
| 655 | + |
| 656 | + public static function setStrictLeadingDot($boolean) |
| 657 | + { |
| 658 | + self::$strict_leading_dot = $boolean; |
| 659 | + } |
| 660 | + |
| 661 | + public static function setStrictWildcardSlash($boolean) |
| 662 | + { |
| 663 | + self::$strict_wildcard_slash = $boolean; |
| 664 | + } |
| 665 | + |
| 666 | + /** |
| 667 | + * Returns a compiled regex which is the equiavlent of the globbing pattern. |
| 668 | + * |
| 669 | + * @param string $glob pattern |
| 670 | + * @return string regex |
| 671 | + */ |
| 672 | + public static function glob_to_regex($glob) |
| 673 | + { |
| 674 | + $first_byte = true; |
| 675 | + $escaping = false; |
| 676 | + $in_curlies = 0; |
| 677 | + $regex = ''; |
| 678 | + $sizeGlob = strlen($glob); |
| 679 | + for ($i = 0; $i < $sizeGlob; $i++) |
| 680 | + { |
| 681 | + $car = $glob[$i]; |
| 682 | + if ($first_byte) |
| 683 | + { |
| 684 | + if (self::$strict_leading_dot && $car !== '.') |
| 685 | + { |
| 686 | + $regex .= '(?=[^\.])'; |
| 687 | + } |
| 688 | + |
| 689 | + $first_byte = false; |
| 690 | + } |
| 691 | + |
| 692 | + if ($car === '/') |
| 693 | + { |
| 694 | + $first_byte = true; |
| 695 | + } |
| 696 | + |
| 697 | + if ($car === '.' || $car === '(' || $car === ')' || $car === '|' || $car === '+' || $car === '^' || $car === '$') |
| 698 | + { |
| 699 | + $regex .= "\\$car"; |
| 700 | + } |
| 701 | + elseif ($car === '*') |
| 702 | + { |
| 703 | + $regex .= ($escaping ? '\\*' : (self::$strict_wildcard_slash ? '[^/]*' : '.*')); |
| 704 | + } |
| 705 | + elseif ($car === '?') |
| 706 | + { |
| 707 | + $regex .= ($escaping ? '\\?' : (self::$strict_wildcard_slash ? '[^/]' : '.')); |
| 708 | + } |
| 709 | + elseif ($car === '{') |
| 710 | + { |
| 711 | + $regex .= ($escaping ? '\\{' : '('); |
| 712 | + if (!$escaping) ++$in_curlies; |
| 713 | + } |
| 714 | + elseif ($car === '}' && $in_curlies) |
| 715 | + { |
| 716 | + $regex .= ($escaping ? '}' : ')'); |
| 717 | + if (!$escaping) --$in_curlies; |
| 718 | + } |
| 719 | + elseif ($car === ',' && $in_curlies) |
| 720 | + { |
| 721 | + $regex .= ($escaping ? ',' : '|'); |
| 722 | + } |
| 723 | + elseif ($car === '\\') |
| 724 | + { |
| 725 | + if ($escaping) |
| 726 | + { |
| 727 | + $regex .= '\\\\'; |
| 728 | + $escaping = false; |
| 729 | + } |
| 730 | + else |
| 731 | + { |
| 732 | + $escaping = true; |
| 733 | + } |
| 734 | + |
| 735 | + continue; |
| 736 | + } |
| 737 | + else |
| 738 | + { |
| 739 | + $regex .= $car; |
| 740 | + } |
| 741 | + $escaping = false; |
| 742 | + } |
| 743 | + |
| 744 | + return '#^'.$regex.'$#'; |
| 745 | + } |
| 746 | + |
| 747 | + public static function globToRegex($glob) { |
| 748 | + return self::glob_to_regex($glob); |
| 749 | + } |
| 750 | + |
| 751 | +} |
| 752 | + |
| 753 | +/** |
| 754 | + * Numeric comparisons. |
| 755 | + * |
| 756 | + * sfNumberCompare compiles a simple comparison to an anonymous |
| 757 | + * subroutine, which you can call with a value to be tested again. |
| 758 | + |
| 759 | + * Now this would be very pointless, if sfNumberCompare didn't understand |
| 760 | + * magnitudes. |
| 761 | + |
| 762 | + * The target value may use magnitudes of kilobytes (k, ki), |
| 763 | + * megabytes (m, mi), or gigabytes (g, gi). Those suffixed |
| 764 | + * with an i use the appropriate 2**n version in accordance with the |
| 765 | + * IEC standard: http://physics.nist.gov/cuu/Units/binary.html |
| 766 | + * |
| 767 | + * based on perl Number::Compare module. |
| 768 | + * |
| 769 | + * @author Fabien Potencier <fabien.potencier@gmail.com> php port |
| 770 | + * @author Richard Clamp <richardc@unixbeard.net> perl version |
| 771 | + * @copyright 2004-2005 Fabien Potencier <fabien.potencier@gmail.com> |
| 772 | + * @copyright 2002 Richard Clamp <richardc@unixbeard.net> |
| 773 | + * @see http://physics.nist.gov/cuu/Units/binary.html |
| 774 | + * @version SVN: $Id$ |
| 775 | + */ |
| 776 | +class sfNumberCompare |
| 777 | +{ |
| 778 | + protected $test = ''; |
| 779 | + |
| 780 | + public function __construct($test) |
| 781 | + { |
| 782 | + $this->test = $test; |
| 783 | + } |
| 784 | + |
| 785 | + public function test($number) |
| 786 | + { |
| 787 | + if (!preg_match('{^([<>]=?)?(.*?)([kmg]i?)?$}i', $this->test, $matches)) |
| 788 | + { |
| 789 | + throw new MWException(sprintf('don\'t understand "%s" as a test.', $this->test)); |
| 790 | + } |
| 791 | + |
| 792 | + $target = array_key_exists(2, $matches) ? $matches[2] : ''; |
| 793 | + $magnitude = array_key_exists(3, $matches) ? $matches[3] : ''; |
| 794 | + if (strtolower($magnitude) === 'k') $target *= 1000; |
| 795 | + if (strtolower($magnitude) === 'ki') $target *= 1024; |
| 796 | + if (strtolower($magnitude) === 'm') $target *= 1000000; |
| 797 | + if (strtolower($magnitude) === 'mi') $target *= 1024*1024; |
| 798 | + if (strtolower($magnitude) === 'g') $target *= 1000000000; |
| 799 | + if (strtolower($magnitude) === 'gi') $target *= 1024*1024*1024; |
| 800 | + |
| 801 | + $comparison = array_key_exists(1, $matches) ? $matches[1] : '=='; |
| 802 | + if ($comparison === '==' || $comparison == '') |
| 803 | + { |
| 804 | + return ($number == $target); |
| 805 | + } |
| 806 | + if ($comparison === '>') |
| 807 | + { |
| 808 | + return ($number > $target); |
| 809 | + } |
| 810 | + if ($comparison === '>=') |
| 811 | + { |
| 812 | + return ($number >= $target); |
| 813 | + } |
| 814 | + if ($comparison === '<') |
| 815 | + { |
| 816 | + return ($number < $target); |
| 817 | + } |
| 818 | + if ($comparison === '<=') |
| 819 | + { |
| 820 | + return ($number <= $target); |
| 821 | + } |
| 822 | + |
| 823 | + return false; |
| 824 | + } |
| 825 | +} |