Author: Derick Rethans
Date: 2006-11-17 15:33:29 +0100 (Fri, 17 Nov 2006)
New Revision: 3963

Log:
- Implemented FR #9134: Added the ezcFile::calculateRelativePath() method
  which calculates the relative path of the file/directory to a given base
  path.

Added:
   trunk/File/tests/file_calculate_relative_path_test.php
Modified:
   trunk/File/ChangeLog
   trunk/File/src/file.php
   trunk/File/tests/file_find_recursive_test.php
   trunk/File/tests/suite.php

Modified: trunk/File/ChangeLog
===================================================================
--- trunk/File/ChangeLog        2006-11-17 14:31:41 UTC (rev 3962)
+++ trunk/File/ChangeLog        2006-11-17 14:33:29 UTC (rev 3963)
@@ -1,3 +1,11 @@
+1.1beta1 - [RELEASEDATE]
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+- Implemented FR #9134: Added the ezcFile::calculateRelativePath() method
+  which calculates the relative path of the file/directory to a given base
+  path.
+
+
 1.0.1 - Monday 09 October 2006
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 

Modified: trunk/File/src/file.php
===================================================================
--- trunk/File/src/file.php     2006-11-17 14:31:41 UTC (rev 3962)
+++ trunk/File/src/file.php     2006-11-17 14:33:29 UTC (rev 3963)
@@ -166,5 +166,48 @@
         $d->close();
         rmdir( $sourceDir );
     }
+
+    /**
+     * Calculates the relative path of the file/directory '$path' to a given
+     * $base path.
+     * This method does not touch the filesystem.
+     *
+     * @param string $path
+     * @param string $base
+     * @return string
+     */
+    static public function calculateRelativePath( $path, $base )
+    {
+        // Sanitize the paths to use the correct directory separator for the 
platform
+        $path = strtr( $path, '\\/', DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR 
);
+        $base = strtr( $base, '\\/', DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR 
);
+
+        $base = explode( DIRECTORY_SEPARATOR, $base );
+        $path = explode( DIRECTORY_SEPARATOR, $path );
+
+        $result = '';
+
+        $pathPart = array_shift( $path );
+        $basePart = array_shift( $base );
+        while ( $pathPart == $basePart )
+        {
+            $pathPart = array_shift( $path );
+            $basePart = array_shift( $base );
+        }
+
+        if ( $pathPart != null )
+        {
+            array_unshift( $path, $pathPart );
+        }
+        if ( $basePart != null ) 
+        {
+            array_unshift( $base, $basePart );
+        }
+
+        $result = str_repeat( '..' . DIRECTORY_SEPARATOR, count( $base ) );
+        $result .= join( DIRECTORY_SEPARATOR, $path );
+
+        return $result;
+    }
 }
 ?>

Added: trunk/File/tests/file_calculate_relative_path_test.php
===================================================================
--- trunk/File/tests/file_calculate_relative_path_test.php      2006-11-17 
14:31:41 UTC (rev 3962)
+++ trunk/File/tests/file_calculate_relative_path_test.php      2006-11-17 
14:33:29 UTC (rev 3963)
@@ -0,0 +1,63 @@
+<?php
+/**
+ * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @version //autogentag//
+ * @filesource
+ * @package File
+ * @subpackage Tests
+ */
+
+class ezcFileCalculateRelativePathTest extends ezcTestCase
+{
+    public function testRelative1()
+    {
+        $result = ezcFile::calculateRelativePath( 'C:/foo/1/2/php.php', 
'C:/foo/bar' );
+        self::assertEquals( '..' . DIRECTORY_SEPARATOR . '1' . 
DIRECTORY_SEPARATOR . '2' . DIRECTORY_SEPARATOR . 'php.php', $result );
+
+        $result = ezcFile::calculateRelativePath( 'C:/foo/bar/php.php', 
'C:/foo/bar' );
+        self::assertEquals( 'php.php', $result );
+
+        $result = ezcFile::calculateRelativePath( 'C:/php.php', 
'C:/foo/bar/1/2');
+        self::assertEquals( '..' . DIRECTORY_SEPARATOR . '..' . 
DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 
'php.php', $result );
+
+        $result = ezcFile::calculateRelativePath( 'C:/bar/php.php', 
'C:/foo/bar/1/2');
+        self::assertEquals('..' . DIRECTORY_SEPARATOR . '..' . 
DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 
'bar' . DIRECTORY_SEPARATOR . 'php.php', $result);
+    }
+
+    public function testRelative2()
+    {
+        $result = ezcFile::calculateRelativePath( 'C:\\foo\\1\\2\\php.php', 
'C:\\foo\\bar' );
+        self::assertEquals( '..' . DIRECTORY_SEPARATOR . '1' . 
DIRECTORY_SEPARATOR . '2' . DIRECTORY_SEPARATOR . 'php.php', $result );
+
+        $result = ezcFile::calculateRelativePath( 'C:\\foo\\bar\\php.php', 
'C:\\foo\\bar' );
+        self::assertEquals( 'php.php', $result );
+
+        $result = ezcFile::calculateRelativePath( 'C:\\php.php', 
'C:\\foo\\bar\\1\\2');
+        self::assertEquals( '..' . DIRECTORY_SEPARATOR . '..' . 
DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 
'php.php', $result );
+
+        $result = ezcFile::calculateRelativePath( 'C:\\bar\\php.php', 
'C:\\foo\\bar\\1\\2');
+        self::assertEquals('..' . DIRECTORY_SEPARATOR . '..' . 
DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 
'bar' . DIRECTORY_SEPARATOR . 'php.php', $result);
+    }
+
+    public function testRelative3()
+    {
+        $result = ezcFile::calculateRelativePath( '/foo/1/2/php.php', 
'/foo/bar' );
+        self::assertEquals( '..' . DIRECTORY_SEPARATOR . '1' . 
DIRECTORY_SEPARATOR . '2' . DIRECTORY_SEPARATOR . 'php.php', $result );
+
+        $result = ezcFile::calculateRelativePath( '/foo/bar/php.php', 
'/foo/bar' );
+        self::assertEquals( 'php.php', $result );
+
+        $result = ezcFile::calculateRelativePath( '/php.php', '/foo/bar/1/2');
+        self::assertEquals( '..' . DIRECTORY_SEPARATOR . '..' . 
DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 
'php.php', $result );
+
+        $result = ezcFile::calculateRelativePath( '/bar/php.php', 
'/foo/bar/1/2');
+        self::assertEquals('..' . DIRECTORY_SEPARATOR . '..' . 
DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 
'bar' . DIRECTORY_SEPARATOR . 'php.php', $result);
+    }
+
+    public static function suite()
+    {
+         return new PHPUnit_Framework_TestSuite( 
"ezcFileCalculateRelativePathTest" );
+    }
+}
+?>


Property changes on: trunk/File/tests/file_calculate_relative_path_test.php
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/File/tests/file_find_recursive_test.php
===================================================================
--- trunk/File/tests/file_find_recursive_test.php       2006-11-17 14:31:41 UTC 
(rev 3962)
+++ trunk/File/tests/file_find_recursive_test.php       2006-11-17 14:33:29 UTC 
(rev 3963)
@@ -23,9 +23,10 @@
             7 => 'File/design/requirements.txt',
             8 => 'File/src/file.php',
             9 => 'File/src/file_autoload.php',
-            10 => 'File/tests/file_find_recursive_test.php',
-            11 => 'File/tests/file_remove_recursive_test.php',
-            12 => 'File/tests/suite.php',
+            10 => 'File/tests/file_calculate_relative_path_test.php',
+            11 => 'File/tests/file_find_recursive_test.php',
+            12 => 'File/tests/file_remove_recursive_test.php',
+            13 => 'File/tests/suite.php',
         );
         self::assertEquals( $expected, ezcFile::findRecursive( "File", 
array(), array( '@/docs/@', '@svn@', '@\.swp$@' ) ) );
     }
@@ -43,9 +44,10 @@
             7 => './File/design/requirements.txt',
             8 => './File/src/file.php',
             9 => './File/src/file_autoload.php',
-            10 => './File/tests/file_find_recursive_test.php',
-            11 => './File/tests/file_remove_recursive_test.php',
-            12 => './File/tests/suite.php',
+            10 => './File/tests/file_calculate_relative_path_test.php',
+            11 => './File/tests/file_find_recursive_test.php',
+            12 => './File/tests/file_remove_recursive_test.php',
+            13 => './File/tests/suite.php',
         );
         self::assertEquals( $expected, ezcFile::findRecursive( ".", array( 
'@^\./File/@' ), array( '@/docs/@', '@\.svn@', '@\.swp$@' ) ) );
     }
@@ -78,9 +80,10 @@
             1 => 'File/design/requirements.txt',
             2 => 'File/src/file.php',
             3 => 'File/src/file_autoload.php',
-            4 => 'File/tests/file_find_recursive_test.php',
-            5 => 'File/tests/file_remove_recursive_test.php',
-            6 => 'File/tests/suite.php',
+            4 => 'File/tests/file_calculate_relative_path_test.php',
+            5 => 'File/tests/file_find_recursive_test.php',
+            6 => 'File/tests/file_remove_recursive_test.php',
+            7 => 'File/tests/suite.php',
         );
         self::assertEquals( $expected, ezcFile::findRecursive( "File", array( 
'@\.(php|txt)$@' ), array( '@/docs/@', '@\.svn@' ) ) );
     }

Modified: trunk/File/tests/suite.php
===================================================================
--- trunk/File/tests/suite.php  2006-11-17 14:31:41 UTC (rev 3962)
+++ trunk/File/tests/suite.php  2006-11-17 14:33:29 UTC (rev 3963)
@@ -13,6 +13,7 @@
  */
 require_once 'file_find_recursive_test.php';
 require_once 'file_remove_recursive_test.php';
+require_once 'file_calculate_relative_path_test.php';
 
 /**
  * @package File
@@ -27,6 +28,7 @@
 
         $this->addTest( ezcFileFindRecursiveTest::suite() );
         $this->addTest( ezcFileRemoveRecursiveTest::suite() );
+        $this->addTest( ezcFileCalculateRelativePathTest::suite() );
     }
 
     public static function suite()

-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to