4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Finder\Iterator;
15 * FilecontentFilterIterator filters files by their contents using patterns (regexps or strings).
17 * @author Fabien Potencier <fabien@symfony.com>
18 * @author Włodzimierz Gajda <gajdaw@gajdaw.pl>
20 class FilecontentFilterIterator extends MultiplePcreFilterIterator
23 * Filters the iterator values.
25 * @return bool true if the value should be kept, false otherwise
27 public function accept()
29 if (!$this->matchRegexps && !$this->noMatchRegexps) {
33 $fileinfo = $this->current();
35 if ($fileinfo->isDir() || !$fileinfo->isReadable()) {
39 $content = $fileinfo->getContents();
44 return $this->isAccepted($content);
48 * Converts string to regexp if necessary.
50 * @param string $str Pattern: string or regexp
52 * @return string regexp corresponding to a given string or regexp
54 protected function toRegex($str)
56 return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/';