Skip to content

Commit

Permalink
[Finder] added a way to ignore all 'hidden' files
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 23, 2011
1 parent 37d5baf commit 42709a7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
61 changes: 54 additions & 7 deletions Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
*/
class Finder implements \IteratorAggregate
{
const IGNORE_VCS_FILES = 1;
const IGNORE_DOT_FILES = 2;

private $mode = 0;
private $names = array();
private $notNames = array();
Expand All @@ -38,11 +41,18 @@ class Finder implements \IteratorAggregate
private $sizes = array();
private $followLinks = false;
private $sort = false;
private $ignoreVCS = true;
private $ignore = 0;
private $dirs = array();
private $dates = array();
private $iterators = array();

static private $vcsPatterns = array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg');

public function __construct()
{
$this->ignore = static::IGNORE_VCS_FILES | static::IGNORE_DOT_FILES;
}

/**
* Restricts the matching to directories only.
*
Expand Down Expand Up @@ -205,22 +215,55 @@ public function exclude($dir)
return $this;
}

/**
* Excludes "hidden" directories and files (starting with a dot).
*
* @param Boolean $ignoreDotFiles Whether to exclude "hidden" files or not
*
* @return Finder The current Finder instance
*
* @see Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator
*
* @api
*/
public function ignoreDotFiles($ignoreDotFiles)
{
if ($ignoreDotFiles) {
$this->ignore = $this->ignore | static::IGNORE_DOT_FILES;
} else {
$this->ignore = $this->ignore ^ static::IGNORE_DOT_FILES;
}

return $this;
}

/**
* Forces the finder to ignore version control directories.
*
* @param Boolean $ignoreVCS Whether to exclude VCS files or not
*
* @return Finder The current Finder instance
*
* @see Symfony\Component\Finder\Iterator\IgnoreVcsFilterIterator
* @see Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator
*
* @api
*/
public function ignoreVCS($ignoreVCS)
{
$this->ignoreVCS = (Boolean) $ignoreVCS;
if ($ignoreVCS) {
$this->ignore = $this->ignore | static::IGNORE_VCS_FILES;
} else {
$this->ignore = $this->ignore ^ static::IGNORE_VCS_FILES;
}

return $this;
}

static public function addVCSPattern($pattern)
{
static::$vcsPatterns[] = $pattern;
}

/**
* Sorts files and directories by an anonymous function.
*
Expand Down Expand Up @@ -416,12 +459,16 @@ private function searchInDirectory($dir)
$iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
}

if ($this->exclude) {
$iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
if (static::IGNORE_VCS_FILES === (static::IGNORE_VCS_FILES & $this->ignore)) {
$this->exclude = array_merge($this->exclude, static::$vcsPatterns);
}

if ($this->ignoreVCS) {
$iterator = new Iterator\IgnoreVcsFilterIterator($iterator);
if (static::IGNORE_DOT_FILES === (static::IGNORE_DOT_FILES & $this->ignore)) {
$this->exclude[] = '\..+';
}

if ($this->exclude) {
$iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
}

if ($this->names || $this->notNames) {
Expand Down
32 changes: 0 additions & 32 deletions Iterator/IgnoreVcsFilterIterator.php

This file was deleted.

0 comments on commit 42709a7

Please sign in to comment.