PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/symfony/2.0.0pr4/src/vendor/symfony/src/Symfony/Component/Finder/Iterator/FilenameFilterIterator.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 94 lines | 54 code | 13 blank | 27 comment | 8 complexity | 52b0c5f021d84d14169ff5c6d7e64fa4 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  1. <?php
  2. namespace Symfony\Component\Finder\Iterator;
  3. use Symfony\Component\Finder\Glob;
  4. /*
  5. * This file is part of the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. /**
  13. * FilenameFilterIterator filters files by patterns (a regexp, a glob, or a string).
  14. *
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. class FilenameFilterIterator extends \FilterIterator
  18. {
  19. protected $matchRegexps;
  20. protected $noMatchRegexps;
  21. /**
  22. * Constructor.
  23. *
  24. * @param \Iterator $iterator The Iterator to filter
  25. * @param array $matchPatterns An array of patterns that need to match
  26. * @param array $noMatchPatterns An array of patterns that need to not match
  27. */
  28. public function __construct(\Iterator $iterator, array $matchPatterns, array $noMatchPatterns)
  29. {
  30. $this->matchRegexps = array();
  31. foreach ($matchPatterns as $pattern) {
  32. $this->matchRegexps[] = $this->toRegex($pattern);
  33. }
  34. $this->noMatchRegexps = array();
  35. foreach ($noMatchPatterns as $pattern) {
  36. $this->noMatchRegexps[] = $this->toRegex($pattern);
  37. }
  38. parent::__construct($iterator);
  39. }
  40. /**
  41. * Filters the iterator values.
  42. *
  43. * @return Boolean true if the value should be kept, false otherwise
  44. */
  45. public function accept()
  46. {
  47. $fileinfo = $this->getInnerIterator()->current();
  48. // should at least match one rule
  49. if ($this->matchRegexps) {
  50. $match = false;
  51. foreach ($this->matchRegexps as $regex) {
  52. if (preg_match($regex, $fileinfo->getFilename())) {
  53. $match = true;
  54. break;
  55. }
  56. }
  57. } else {
  58. $match = true;
  59. }
  60. // should at least not match one rule to exclude
  61. if ($this->noMatchRegexps) {
  62. $exclude = false;
  63. foreach ($this->noMatchRegexps as $regex) {
  64. if (preg_match($regex, $fileinfo->getFilename())) {
  65. $exclude = true;
  66. break;
  67. }
  68. }
  69. } else {
  70. $exclude = false;
  71. }
  72. return $match && !$exclude;
  73. }
  74. protected function toRegex($str)
  75. {
  76. if (preg_match('/^([^a-zA-Z0-9\\\\]).+?\\1[ims]?$/', $str)) {
  77. return $str;
  78. }
  79. return Glob::toRegex($str);
  80. }
  81. }