better file search support

This commit is contained in:
John Bintz 2009-11-07 17:00:14 -05:00
parent 6e6f6f71c9
commit f38823df7b
2 changed files with 75 additions and 0 deletions

View File

@ -143,6 +143,29 @@ class ComicPress {
}
return array_reverse($all_paths);
}
/**
* Recursively search a particular template directory for a specified file, limiting the search to the provided categories.
* @param string $name The name of the file with no path information.
* @param string $path The path within the template directories to start the search.
* @param attay $categories The child -> root category slugs to search within the specified path. If not provided, retrieve from current post.
* @return string|boolean The path to the file, for false if not found.
*/
function find_file($name, $path, $categories = null) {
foreach (array(get_stylesheet_directory(), get_template_directory()) as $dir) {
$dir = trailingslashit($dir) . $path;
$dirs_to_search = $this->category_search($categories, $dir);
$dirs_to_search[] = $dir;
foreach ($dirs_to_search as $category_path) {
$target = trailingslashit($category_path) . $name;
if (file_exists($target)) {
return $target;
}
}
}
return false;
}
}
?>

View File

@ -73,6 +73,58 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($found_path, $this->cp->category_search($categories, vfsStream::url('root/style')));
}
function providerTestFindFile() {
return array(
array(
array(), array(), false,
),
array(
array('root/parent/partials/index.inc'), array(), vfsStream::url('root/parent/partials/index.inc')
),
array(
array(
'root/parent/partials/index.inc',
'root/child/partials/index.inc'
),
array(),
vfsStream::url('root/child/partials/index.inc')
),
array(
array(
'root/child/partials/index.inc',
'root/child/partials/comic/index.inc'
),
array('comic'),
vfsStream::url('root/child/partials/comic/index.inc')
),
array(
array(
'root/child/partials/index.inc',
'root/child/partials/comic/index.inc'
),
array('chapter-1', 'comic'),
vfsStream::url('root/child/partials/comic/index.inc')
)
);
}
/**
* @dataProvider providerTestFindFile
*/
function testFindFile($files_to_setup, $post_categories, $expected_path_result) {
mkdir(vfsStream::url('root/parent/partials/comic/chapter-1'), 0777, true);
mkdir(vfsStream::url('root/child/partials/comic/chapter-1'), 0777, true);
foreach ($files_to_setup as $path) {
file_put_contents(vfsStream::url($path), "test");
}
_set_template_directory(vfsStream::url('root/parent'));
_set_stylesheet_directory(vfsStream::url('root/child'));
$this->assertEquals($expected_path_result, $this->cp->find_file('index.inc', 'partials', $post_categories));
}
}
?>