Created
December 14, 2011 09:54
-
-
Save xurenlu/1475944 to your computer and use it in GitHub Desktop.
walk walk and walk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 遍历某个目录或某个目录用; | |
* 只需设置回调即可; | |
* */ | |
class Walker{ | |
var $dir; | |
var $callback=null; | |
var $num = 0; | |
var $ecode=0; | |
var $error=""; | |
var $eof=false; | |
var $errors=array(-999=>"call backfunction not exists"); | |
var $custom_errors=array( | |
0=>"No error occures", | |
-1=>"demo error" | |
); | |
var $last_result=null; | |
function push_error($ecode,$error){ | |
$this->errors[$ecode]=$error; | |
} | |
function __construct(){ | |
foreach($this->custom_errors as $k=>$v){ | |
$this->push_error($k,$v); | |
} | |
} | |
function getNextItem(){ | |
} | |
function prepare(){ | |
} | |
function isEOF(){ | |
} | |
function run($callback){ | |
if(!function_exists($callback)){ | |
return -9999; | |
} | |
while(!$this->isEOF()){ | |
$item=$this->getNextItem(); | |
if($item===NULL||$item===false) return true; | |
{ | |
$this->last_result=$callback($item); | |
} | |
} | |
} | |
function __destruct(){ | |
$this->free(); | |
} | |
function free(){ | |
} | |
} | |
/** | |
* @example: | |
function demo_echo_file($file){ | |
echo $file."\n"; | |
} | |
$dir=new DirWalker(); | |
$dir->prepare("./"); | |
$dir->run("demo_echo_file"); | |
unset($dir); | |
* */ | |
class DirWalker extends Walker { | |
var $dir=null; | |
var $dh=null; | |
var $custom_errors=array( | |
-1=>"is not a dir", | |
-2=>"dir can't be opend" | |
); | |
function prepare($dir){ | |
$this->dir=$dir; | |
if (!is_dir($this->dir)) { | |
$this->ecode=-1; | |
$this->ecode=$this->dir." is not a dir"; | |
return false; | |
} | |
if (($this->dh = opendir($this->dir))==false){ | |
$this->ecode=-2; | |
$this->ecode=$this->dir." can't be opend"; | |
return false; | |
} | |
return $this->dir; | |
} | |
function getNextItem(){ | |
$item=readdir($this->dh); | |
if($item===false){ | |
$this->eof=true; | |
} | |
return $item; | |
} | |
function isEOF(){ | |
return $this->eof; | |
} | |
} | |
/** | |
* @example: | |
function demo_frow_walker($row){ | |
echo $row; | |
} | |
$fw=new FileRowWalker(); | |
$fw->prepare("./walker.php"); | |
$fw->run("demo_frow_walker"); | |
*/ | |
class FileRowWalker extends Walker { | |
var $fp=null; | |
var $file=""; | |
function getNextItem(){ | |
return $item=fgets($this->fp); | |
} | |
function isEOF(){ | |
return feof($this->fp); | |
} | |
function prepare($file){ | |
$this->file=$file; | |
/** | |
if(!is_file(file)){ | |
$this->ecode=-9; | |
$this->error="$file is not a file"; | |
return false; | |
} | |
*/ | |
$this->fp=fopen($file,"r"); | |
if($this->fp===false) | |
{ | |
echo "run to ".__LINE__; | |
$this->ecode=-10; | |
$this->error="$file open failed;"; | |
return false; | |
} | |
return $this->fp; | |
} | |
function free(){ | |
fclose($this->fp); | |
} | |
} | |
class CSVRowWalker extends Walker { | |
var $fp=null; | |
var $file=""; | |
function getNextItem(){ | |
return $data = fgetcsv($this->fp, 20000, ","); | |
} | |
function isEOF(){ | |
return feof($this->fp); | |
} | |
function prepare($file){ | |
$this->file=$file; | |
/** | |
if(!is_file(file)){ | |
$this->ecode=-9; | |
$this->error="$file is not a file"; | |
return false; | |
} | |
*/ | |
$this->fp=fopen($file,"r"); | |
if($this->fp===false) | |
{ | |
echo "run to ".__LINE__; | |
$this->ecode=-10; | |
$this->error="$file open failed;"; | |
return false; | |
} | |
return $this->fp; | |
} | |
function free(){ | |
fclose($this->fp); | |
} | |
} | |
function demo_csv_row($row){ | |
print $row[3]."\n"; | |
} | |
$fc = new CSVRowWalker(); | |
$fc->prepare("./utf8.csv"); | |
$fc->run("demo_csv_row"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment