瀏覽代碼

[Finder] added a way to ignore all 'hidden' files

Fabien Potencier 14 年之前
父節點
當前提交
7ab3fdeb83

+ 54 - 7
src/Symfony/Component/Finder/Finder.php

@@ -29,6 +29,9 @@ namespace Symfony\Component\Finder;
  */
 class Finder implements \IteratorAggregate
 {
+    const IGNORE_VCS_FILES = 1;
+    const IGNORE_DOT_FILES = 2;
+
     private $mode        = 0;
     private $names       = array();
     private $notNames    = array();
@@ -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.
      *
@@ -205,22 +215,55 @@ class Finder implements \IteratorAggregate
         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.
      *
@@ -416,12 +459,16 @@ class Finder implements \IteratorAggregate
             $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) {

+ 0 - 32
src/Symfony/Component/Finder/Iterator/IgnoreVcsFilterIterator.php

@@ -1,32 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Component\Finder\Iterator;
-
-/**
- * IgnoreVcsFilterIterator filters out VCS files and directories.
- *
- * It currently supports Subversion, CVS, DARCS, Gnu Arch, Monotone, Bazaar-NG, GIT, and Mercurial.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- */
-class IgnoreVcsFilterIterator extends ExcludeDirectoryFilterIterator
-{
-    /**
-     * Constructor.
-     *
-     * @param \Iterator $iterator The Iterator to filter
-     */
-    public function __construct(\Iterator $iterator)
-    {
-        parent::__construct($iterator, array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg'));
-    }
-}

+ 11 - 0
tests/Symfony/Tests/Component/Finder/FinderTest.php

@@ -134,6 +134,17 @@ class FinderTest extends Iterator\RealIteratorTestCase
         $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
     }
 
+    public function testIgnoreDotFiles()
+    {
+        $finder = new Finder();
+        $this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false));
+        $this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
+
+        $finder = new Finder();
+        $this->assertSame($finder, $finder->ignoreDotFiles(true));
+        $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
+    }
+
     public function testSortByName()
     {
         $finder = new Finder();

+ 0 - 36
tests/Symfony/Tests/Component/Finder/Iterator/IgnoreVcsFilterIteratorTest.php

@@ -1,36 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Tests\Component\Finder\Iterator;
-
-use Symfony\Component\Finder\Iterator\IgnoreVcsFilterIterator;
-
-require_once __DIR__.'/RealIteratorTestCase.php';
-
-class IgnoreVcsFilterIteratorTest extends RealIteratorTestCase
-{
-    public function testAccept()
-    {
-        $inner = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(sys_get_temp_dir().'/symfony2_finder', \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
-        //$inner = new Iterator(array('/.git/test.php', '/foo/test.py', '/bar/foo.php'));
-
-        $iterator = new IgnoreVcsFilterIterator($inner);
-        $tmpDir = sys_get_temp_dir().'/symfony2_finder';
-
-        $this->assertIterator(array(
-            $tmpDir.DIRECTORY_SEPARATOR.'test.py',
-            $tmpDir.DIRECTORY_SEPARATOR.'foo',
-            $tmpDir.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'bar.tmp',
-            $tmpDir.DIRECTORY_SEPARATOR.'test.php',
-            $tmpDir.DIRECTORY_SEPARATOR.'toto'
-        ), $iterator);
-    }
-}