Author: dr
Date: Wed Jan 16 14:44:56 2008
New Revision: 7160
Log:
- Implemented issue #8529: Added a by-reference argument to
ezcBaseFile::findRecursive that returns statistsics (count and total size)
of all files that are returned by this function.
Modified:
trunk/Base/ChangeLog
trunk/Base/src/file.php
trunk/Base/tests/file_find_recursive_test.php
Modified: trunk/Base/ChangeLog
==============================================================================
--- trunk/Base/ChangeLog [iso-8859-1] (original)
+++ trunk/Base/ChangeLog [iso-8859-1] Wed Jan 16 14:44:56 2008
@@ -1,6 +1,9 @@
1.5alpha1 - [RELEASEDATE]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+- Implemented issue #8529: Added a by-reference argument to
+ ezcBaseFile::findRecursive that returns statistsics (count and total size)
+ of all files that are returned by this function.
- Implemented issue #11506: Added the static method
ezcBase::getInstallationPath().
Modified: trunk/Base/src/file.php
==============================================================================
--- trunk/Base/src/file.php [iso-8859-1] (original)
+++ trunk/Base/src/file.php [iso-8859-1] Wed Jan 16 14:44:56 2008
@@ -19,11 +19,13 @@
* $confFiles = ezcBaseFile::findRecursive( "/etc", array( '@\.conf$@' ) );
*
* // lists all autoload files in the components source tree and excludes the
- * // ones in the autoload subdirectory.
+ * // ones in the autoload subdirectory. Statistics are returned in the $stats
+ * // variable which is passed by reference.
* $files = ezcBaseFile::findRecursive(
* "/dat/dev/ezcomponents",
* array( '@src/.*_autoload.php$@' ),
- * array( '@/autoload/@' )
+ * array( '@/autoload/@' ),
+ * $stats
* );
*
* // lists all binaries in /bin except the ones starting with a "g"
@@ -44,10 +46,14 @@
* $includeFilters to include only specific files, and $excludeFilters to
* exclude certain files from being returned. The function will always go
* into subdirectories even if the entry would not have passed the filters.
- *
- * @param string $sourceDir
- * @param array(string) $includeFilters
- * @param array(string) $excludeFilters
+ * If you pass an empty array to the $statistics argument, the function
+ * will in details about the number of files found into the 'count' array
+ * element, and the total filesize in the 'size' array element.
+ *
+ * @param string $sourceDir
+ * @param array(string) $includeFilters
+ * @param array(string) $excludeFilters
+ * @param array() &$statistics
*
* @throws ezcBaseFileNotFoundException if the $sourceDir directory is not
* a directory or does not exist.
@@ -55,7 +61,7 @@
* not be opened for reading.
* @return array
*/
- static public function findRecursive( $sourceDir, array $includeFilters =
array(), array $excludeFilters = array() )
+ static public function findRecursive( $sourceDir, array $includeFilters =
array(), array $excludeFilters = array(), &$statistics = null )
{
if ( !is_dir( $sourceDir ) )
{
@@ -67,6 +73,13 @@
{
throw new ezcBaseFilePermissionException( $sourceDir,
ezcBaseFileException::READ );
}
+
+ // init statistics array
+ if ( !is_array( $statistics ) )
+ {
+ $statistics['size'] = 0;
+ $statistics['count'] = 0;
+ }
while ( ( $entry = $d->read() ) !== false )
{
@@ -75,14 +88,20 @@
continue;
}
- if ( is_dir( $sourceDir . DIRECTORY_SEPARATOR . $entry ) )
+ $fileInfo = @stat( $sourceDir . DIRECTORY_SEPARATOR . $entry );
+ if ( !$fileInfo )
+ {
+ $fileInfo = array( 'size' => 0, 'mode' => 0 );
+ }
+
+ if ( $fileInfo['mode'] & 0x4000 )
{
// We need to ignore the Permission exceptions here as it can
// be normal that a directory can not be accessed. We only need
// the exception if the top directory could not be read.
try
{
- $subList = self::findRecursive( $sourceDir .
DIRECTORY_SEPARATOR . $entry, $includeFilters, $excludeFilters );
+ $subList = self::findRecursive( $sourceDir .
DIRECTORY_SEPARATOR . $entry, $includeFilters, $excludeFilters, $statistics );
$elements = array_merge( $elements, $subList );
}
catch ( ezcBaseFilePermissionException $e )
@@ -117,6 +136,9 @@
if ( $ok )
{
$elements[] = $sourceDir . DIRECTORY_SEPARATOR . $entry;
+ echo "{$entry} = {$fileInfo['size']}\n";
+ $statistics['count']++;
+ $statistics['size'] += $fileInfo['size'];
}
}
}
Modified: trunk/Base/tests/file_find_recursive_test.php
==============================================================================
--- trunk/Base/tests/file_find_recursive_test.php [iso-8859-1] (original)
+++ trunk/Base/tests/file_find_recursive_test.php [iso-8859-1] Wed Jan 16
14:44:56 2008
@@ -33,7 +33,8 @@
13 => 'File/tests/file_remove_recursive_test.php',
14 => 'File/tests/suite.php',
);
- self::assertEquals( $expected, ezcBaseFile::findRecursive( "File",
array(), array( '@/docs/@', '@svn@', '@\.swp$@' ) ) );
+ self::assertEquals( $expected, ezcBaseFile::findRecursive( "File",
array(), array( '@/docs/@', '@svn@', '@\.swp$@' ), $stats ) );
+ self::assertEquals( array( 'size' => 130984, 'count' => 15 ), $stats );
}
public function testRecursive2()
@@ -55,7 +56,8 @@
13 => './File/tests/file_remove_recursive_test.php',
14 => './File/tests/suite.php',
);
- self::assertEquals( $expected, ezcBaseFile::findRecursive( ".", array(
'@^\./File/@' ), array( '@/docs/@', '@\.svn@', '@\.swp$@' ) ) );
+ self::assertEquals( $expected, ezcBaseFile::findRecursive( ".", array(
'@^\./File/@' ), array( '@/docs/@', '@\.svn@', '@\.swp$@' ), $stats ) );
+ self::assertEquals( array( 'size' => 130984, 'count' => 15 ), $stats );
}
public function testRecursive3()
@@ -65,7 +67,8 @@
1 => 'File/design/file_operations.png',
2 => 'File/design/md5.png',
);
- self::assertEquals( $expected, ezcBaseFile::findRecursive( "File",
array( '@\.png$@' ), array( '@\.svn@' ) ) );
+ self::assertEquals( $expected, ezcBaseFile::findRecursive( "File",
array( '@\.png$@' ), array( '@\.svn@' ), $stats ) );
+ self::assertEquals( array( 'size' => 17642, 'count' => 3 ), $stats );
}
public function testRecursive4()
@@ -78,7 +81,8 @@
4 => 'File/design/md5.png',
5 => 'File/design/requirements.txt',
);
- self::assertEquals( $expected, ezcBaseFile::findRecursive( "File",
array( '@/design/@' ), array( '@\.svn@' ) ) );
+ self::assertEquals( $expected, ezcBaseFile::findRecursive( "File",
array( '@/design/@' ), array( '@\.svn@' ), $stats ) );
+ self::assertEquals( array( 'size' => 114282, 'count' => 6 ), $stats );
}
public function testRecursive5()
--
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components