PageRenderTime 31ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/doctrine/vendors/Symfony/Component/Finder/Iterator/FilenameFilterIterator.php

https://bitbucket.org/NordLabs/yiidoctrine
PHP | 92 lines | 53 code | 12 blank | 27 comment | 8 complexity | 0c6facb789c270842adc1daaa56cb578 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Finder\Iterator;
  11. use Symfony\Component\Finder\Glob;
  12. /**
  13. * FilenameFilterIterator filters files by patterns (a regexp, a glob, or a string).
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class FilenameFilterIterator extends \FilterIterator
  18. {
  19. private $matchRegexps;
  20. private $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. // should at least match one rule
  48. if ($this->matchRegexps) {
  49. $match = false;
  50. foreach ($this->matchRegexps as $regex) {
  51. if (preg_match($regex, $this->getFilename())) {
  52. $match = true;
  53. break;
  54. }
  55. }
  56. } else {
  57. $match = true;
  58. }
  59. // should at least not match one rule to exclude
  60. if ($this->noMatchRegexps) {
  61. $exclude = false;
  62. foreach ($this->noMatchRegexps as $regex) {
  63. if (preg_match($regex, $this->getFilename())) {
  64. $exclude = true;
  65. break;
  66. }
  67. }
  68. } else {
  69. $exclude = false;
  70. }
  71. return $match && !$exclude;
  72. }
  73. private function toRegex($str)
  74. {
  75. if (preg_match('/^([^a-zA-Z0-9\\\\]).+?\\1[ims]?$/', $str)) {
  76. return $str;
  77. }
  78. return Glob::toRegex($str);
  79. }
  80. }