PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/symfony/2.0.0pr2/src/vendor/symfony/src/Symfony/Components/Finder/Iterator/FilenameFilterIterator.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 96 lines | 54 code | 13 blank | 29 comment | 8 complexity | e7c5e001595ac3714e0fdc692ea2784f 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\Components\Finder\Iterator;
  3. use Symfony\Components\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. * @package Symfony
  16. * @subpackage Components_Finder
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. */
  19. class FilenameFilterIterator extends \FilterIterator
  20. {
  21. protected $matchRegexps;
  22. protected $noMatchRegexps;
  23. /**
  24. * Constructor.
  25. *
  26. * @param \Iterator $iterator The Iterator to filter
  27. * @param array $matchPatterns An array of patterns that need to match
  28. * @param array $noMatchPatterns An array of patterns that need to not match
  29. */
  30. public function __construct(\Iterator $iterator, array $matchPatterns, array $noMatchPatterns)
  31. {
  32. $this->matchRegexps = array();
  33. foreach ($matchPatterns as $pattern) {
  34. $this->matchRegexps[] = $this->toRegex($pattern);
  35. }
  36. $this->noMatchRegexps = array();
  37. foreach ($noMatchPatterns as $pattern) {
  38. $this->noMatchRegexps[] = $this->toRegex($pattern);
  39. }
  40. parent::__construct($iterator);
  41. }
  42. /**
  43. * Filters the iterator values.
  44. *
  45. * @return Boolean true if the value should be kept, false otherwise
  46. */
  47. public function accept()
  48. {
  49. $fileinfo = $this->getInnerIterator()->current();
  50. // should at least match one rule
  51. if ($this->matchRegexps) {
  52. $match = false;
  53. foreach ($this->matchRegexps as $regex) {
  54. if (preg_match($regex, $fileinfo->getFilename())) {
  55. $match = true;
  56. break;
  57. }
  58. }
  59. } else {
  60. $match = true;
  61. }
  62. // should at least not match one rule to exclude
  63. if ($this->noMatchRegexps) {
  64. $exclude = false;
  65. foreach ($this->noMatchRegexps as $regex) {
  66. if (preg_match($regex, $fileinfo->getFilename())) {
  67. $exclude = true;
  68. break;
  69. }
  70. }
  71. } else {
  72. $exclude = false;
  73. }
  74. return $match && !$exclude;
  75. }
  76. protected function toRegex($str)
  77. {
  78. if (preg_match('/^([^a-zA-Z0-9\\\\]).+?\\1[ims]?$/', $str)) {
  79. return $str;
  80. }
  81. return Glob::toRegex($str);
  82. }
  83. }