Sfoglia il codice sorgente

[Finder] Add support for relative path

Victor Berchet 14 anni fa
parent
commit
0a33cbb403

+ 4 - 1
src/Symfony/Component/Finder/Finder.php

@@ -344,7 +344,10 @@ class Finder implements \IteratorAggregate
             $flags |= \RecursiveDirectoryIterator::FOLLOW_SYMLINKS;
         }
 
-        $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
+        $iterator = new \RecursiveIteratorIterator(
+            new Iterator\RecursiveDirectoryIterator($dir, $flags),
+            \RecursiveIteratorIterator::SELF_FIRST
+        );
 
         if ($this->depths) {
             $iterator = new Iterator\DepthRangeFilterIterator($iterator, $this->depths);

+ 41 - 0
src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php

@@ -0,0 +1,41 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.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;
+
+use Symfony\Component\Finder\SplFileInfo;
+
+/**
+ * Extends the \RecursiveDirectoryIterator to support relative paths
+ *
+ * @author Victor Berchet <victor@suumit.com>
+ */
+class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
+{
+    public function __construct($path, $flags)
+    {
+        if ($flags & (self::CURRENT_AS_PATHNAME | self::CURRENT_AS_SELF)) {
+            throw new RuntimeException('This iterator only support returning current as fileinfo.');
+        }
+        
+        parent::__construct($path, $flags);
+    }
+
+    /**
+     * Return an instance of SplFileInfo with support for relative paths
+     *
+     * @return SplFileInfo File information
+     */
+    public function current()
+    {
+        return new SplFileInfo(parent::current()->getPathname(), $this->getSubPath(), $this->getSubPathname());
+    }
+}

+ 57 - 0
src/Symfony/Component/Finder/SplFileInfo.php

@@ -0,0 +1,57 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Finder;
+
+/**
+ * Extends \SplFileInfo to support relative paths
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class SplFileInfo extends \SplFileInfo
+{
+    protected $relativePath;
+    protected $relativePathname;
+
+    /**
+     * Constructor
+     *
+     * @param string  $fileInfo         The file name
+     * @param string  $relativePath     The relative path
+     * @param string  $relativePathname The relative path name
+     */
+    public function __construct($file, $relativePath, $relativePathname)
+    {
+        parent::__construct($file);
+        $this->relativePath = $relativePath;
+        $this->relativePathname = $relativePathname;
+    }
+
+    /**
+     * Returns the relative path
+     *
+     * @return string the relative path
+     */
+    public function getRelativePath()
+    {
+        return $this->relativePath;
+    }
+
+    /**
+     * Returns the relative path name
+     *
+     * @return string the relative path name
+     */
+    public function getRelativePathname()
+    {
+        return $this->relativePathname;
+    }
+}

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

@@ -222,6 +222,46 @@ class FinderTest extends Iterator\RealIteratorTestCase
         $this->assertEquals($expected, $a, 'implements the \IteratorAggregate interface');
     }
 
+    public function testRelativePath()
+    {
+        $finder = new Finder();
+
+        $finder->in(self::$tmpDir);
+
+        $paths = array();
+
+        foreach($finder as $file) {
+            $paths[] = $file->getRelativePath();
+        }
+
+        $ref = array("", "", "", "", "foo");
+
+        sort($ref);
+        sort($paths);
+
+        $this->assertEquals($paths, $ref);
+    }
+
+    public function testRelativePathname()
+    {
+        $finder = new Finder();
+
+        $finder->in(self::$tmpDir)->sortByName();
+
+        $paths = array();
+
+        foreach($finder as $file) {
+            $paths[] = $file->getRelativePathname();
+        }
+
+        $ref = array("test.php", "toto", "test.py", "foo", "foo".DIRECTORY_SEPARATOR."bar.tmp");
+
+        sort($paths);
+        sort($ref);
+
+        $this->assertEquals($paths, $ref);
+    }
+
     protected function toAbsolute($files)
     {
         $f = array();