Skip to content

Commit

Permalink
add recursion to list_files(). Refs #4650
Browse files Browse the repository at this point in the history
  • Loading branch information
zombor committed Nov 7, 2012
1 parent 8b97c7d commit 4bbd9cf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
32 changes: 28 additions & 4 deletions classes/Kohana/Core/Filesystem.php
Expand Up @@ -16,11 +16,11 @@ public function include_paths()
return $this->_paths;
}

public function list_files($directory)
public function list_files($directory, array $paths = NULL)
{
$found = array();

foreach ($this->_paths as $path)
foreach ($paths === NULL ? $this->_paths : $paths as $path)
{
if (is_dir($path.$directory))
{
Expand All @@ -30,10 +30,34 @@ public function list_files($directory)
{
$filename = $file->getFilename();

// Skip all hidden files and UNIX backup files
if ($filename[0] === '.' OR $filename[strlen($filename)-1] === '~')
continue;

// Relative filename is the array key
$key = $directory.'/'.$filename;
if ( ! isset($found[$key]))
$found[$key] = realpath($file->getPathName());

if ($file->isDir())
{
if ($sub_dir = $this->list_files($key, $paths))
{
if (isset($found[$key]))
{
// Append the sub-directory list
$found[$key] += $sub_dir;
}
else
{
// Create a new sub-directory list
$found[$key] = $sub_dir;
}
}
}
else
{
if ( ! isset($found[$key]))
$found[$key] = realpath($file->getPathName());
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions spec/Kohana/Core/FilesystemTest.php
Expand Up @@ -14,7 +14,11 @@ protected function setUp()
array(
'APPPATH' => array(
'classes' => array(
'.' => array(),
'Foobar.php' => 'the content',
'subfolder' => array(
'Foobar.php' => 'the content',
),
),
'vendor' => array(
'foobar.png' => 'content',
Expand Down Expand Up @@ -103,6 +107,9 @@ public function test_it_lists_all_files()
array(
'classes/Foobar.php' => 'vfs://APPPATH/classes/Foobar.php',
'classes/Foo.php' => 'vfs://SYSPATH/classes/Foo.php',
'classes/subfolder' => array(
'classes/subfolder/Foobar.php' => 'vfs://APPPATH/classes/subfolder/Foobar.php',
)
),
$this->filesystem->list_files('classes')
);
Expand Down

0 comments on commit 4bbd9cf

Please sign in to comment.