improve pattern manipulation

This commit is contained in:
John Bintz 2009-12-04 21:03:09 -05:00
parent aca6d4712b
commit 090cc45576
2 changed files with 40 additions and 4 deletions

View File

@ -58,6 +58,15 @@ class ComicPressMediaHandling {
return $result;
}
function _get_regex_dirname($input) {
return dirname($this->_resolve_regex_path($input));
}
function _get_regex_filename($input) {
$input = preg_replace('#\\\(?![.])#', '/', $input);
return basename($input);
}
function _resolve_regex_path($input) {
$input = str_replace('\.', '.', $input);
$input = str_replace('\\', '/', $input);
@ -92,15 +101,13 @@ class ComicPressMediaHandling {
return apply_filters('comicpress_expand_filter_callback', $value, $matches);
}
// TODO: Fix Pattern Escaping
function _read_directory($pattern) {
$resolved_pattern = $this->_resolve_regex_path($pattern);
$dirname = dirname($resolved_pattern);
$dirname = $this->_get_regex_dirname($pattern);
$results = false;
if (is_dir($dirname)) {
$results = array();
if (($dh = opendir($dirname)) !== false) {
$filename_pattern = str_replace('#', '\#', basename($resolved_pattern));
$filename_pattern = str_replace('#', '\#', $this->_get_regex_filename($pattern));
while (($file = readdir($dh)) !== false) {
$target = $dirname . '/' . $file;
if (is_file($target)) {

View File

@ -160,6 +160,35 @@ class ComicPressMediaHandlingTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected_output, $this->cpmh->_resolve_regex_path($input));
}
function providerTestGetRegexDirname() {
return array(
array('/test/test2', '/test')
);
}
/**
* @dataProvider providerTestGetRegexDirname
*/
function testGetRegexDirname($input, $expected_output) {
$this->assertEquals($expected_output, $this->cpmh->_get_regex_dirname($input));
}
function providerTestGetRegexFilename() {
return array(
array('/test/test2', 'test2'),
array('c:\test\test2', 'test2'),
array('/test/test2\.cat', 'test2\.cat'),
array('c:\test\test2\.cat', 'test2\.cat'),
);
}
/**
* @dataProvider providerTestGetRegexFilename
*/
function testGetRegexFilename($input, $expected_output) {
$this->assertEquals($expected_output, $this->cpmh->_get_regex_filename($input));
}
function providerTestPreHandleComicPathResults() {
return array(
array('', '', false),