directory reading and more cleanup

This commit is contained in:
John Bintz 2009-12-01 21:53:46 -05:00
parent 6aea00a8d0
commit 3d514a3544
2 changed files with 56 additions and 5 deletions

View File

@ -43,17 +43,17 @@ class ComicPressMediaHandling {
return $old; return $old;
} }
function _expand_filter($filter, $type_filter, $override_post = null) { function _expand_filter($filter, $type_folder, $override_post = null) {
global $post; global $post;
$this->post_to_use = !is_null($override_post) ? $override_post : $post; $this->post_to_use = !is_null($override_post) ? $override_post : $post;
$this->type_filter = $type_filter; $this->type_folder = $type_folder;
$result = preg_replace_callback('#%([a-z0-9-]+)%#i', array(&$this, '_expand_filter_callback'), $filter); $result = preg_replace_callback('#%([a-z0-9-]+)%#i', array(&$this, '_expand_filter_callback'), $filter);
$result = str_replace('.', '\.', $result); $result = str_replace('.', '\.', $result);
$result = str_replace('*', '.*', $result); $result = str_replace('*', '.*', $result);
unset($this->post_to_use); unset($this->post_to_use);
unset($this->type_filter); unset($this->type_folder);
return $result; return $result;
} }
@ -73,7 +73,7 @@ class ComicPressMediaHandling {
} }
break; break;
case 'type-folder': case 'type-folder':
$value = $this->type_filter; $value = $this->type_folder;
break; break;
default: default:
if (preg_match('#^date-(.*)$#', $matches[1], $date_matches) > 0) { if (preg_match('#^date-(.*)$#', $matches[1], $date_matches) > 0) {
@ -83,10 +83,38 @@ class ComicPressMediaHandling {
$value = $matches[0]; $value = $matches[0];
break; break;
} }
return $value; return apply_filters('comicpress_expand_filter_callback', $value, $matches);
}
function _read_directory($pattern) {
$dirname = dirname($pattern);
$results = false;
if (is_dir($dirname)) {
$results = array();
if (($dh = opendir($dirname)) !== false) {
$filename_pattern = str_replace('#', '\#', basename($pattern));
while (($file = readdir($dh)) !== false) {
$target = $dirname . '/' . $file;
if (is_file($target)) {
if (preg_match("#${filename_pattern}#", $file) > 0) {
$results[] = $target;
}
}
}
}
}
return $results;
} }
function get_comic_path($type = 'comic', $override_post = null, $filter = 'default', $multi = false) { function get_comic_path($type = 'comic', $override_post = null, $filter = 'default', $multi = false) {
global $post;
$post_to_use = !is_null($override_post) ? $override_post : $post;
$filter = $this->get_filter($filter); $filter = $this->get_filter($filter);
$globals = $this->_bundle_global_variables();
if (isset($globals[$type])) {
$filter = $this->_expand_filter($filter, $globals[$type], $post_to_use);
}
} }
} }

View File

@ -102,4 +102,27 @@ class ComicPressMediaHandlingTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected_result, $cpmh->_expand_filter($filter, 'comic', (object)array('ID' => 1, 'post_date' => '2009-01-01 15:00:00'))); $this->assertEquals($expected_result, $cpmh->_expand_filter($filter, 'comic', (object)array('ID' => 1, 'post_date' => '2009-01-01 15:00:00')));
} }
function providerTestReadDirectory() {
return array(
array(vfsStream::url('root2/.*'), false),
array(vfsStream::url('root/.*'), array('2009-01-01.jpg', '2009-01-02.jpg', '2009-02-02-two.jpg', '2008-01-01.jpg')),
array(vfsStream::url('root/2009.*'), array('2009-01-01.jpg', '2009-01-02.jpg', '2009-02-02-two.jpg')),
array(vfsStream::url('root/2009-01.*'), array('2009-01-01.jpg', '2009-01-02.jpg')),
array(vfsStream::url('root/2009-01-01.*'), array('2009-01-01.jpg')),
);
}
/**
* @dataProvider providerTestReadDirectory
*/
function testReadDirectory($pattern, $expected_results) {
foreach (array('2009-01-01.jpg', '2009-01-02.jpg', '2009-02-02-two.jpg', '2008-01-01.jpg') as $file) {
file_put_contents(vfsStream::url("root/${file}"), 'file');
}
if (is_array($expected_results)) {
foreach ($expected_results as &$result) { $result = vfsStream::url("root/${result}"); }
}
$this->assertEquals($expected_results, $this->cpmh->_read_directory($pattern));
}
} }