PHP统计目录下的文件总数及代码行数
- <?php
-
-
-
-
-
-
-
- $obj = new caculatefiles();
-
- $obj->setshowflag(false);
-
- $obj->setfileskip(array('all'));
- $obj->run("d:phpappphp_tests");
-
-
- $obj->setfileskip(array());
- $obj->run("d:phpappphp");
-
- $obj->setshowflag(true);
-
- $obj->setfileskip(array('i', 'a'));
- $obj->run("d:phpappphp");
-
-
-
-
-
-
-
-
-
-
-
-
- class caculatefiles {
-
-
-
- private $ext = ".php";
-
-
-
- private $showeveryfile = true;
-
-
-
- private $fileskip = array();
-
-
-
- private $lineskip = array("*", "/*", "//", "#");
- /**
- * 统计跳过的目录规则
- */
- private $dirskip = array(".", "..", '.svn');
-
- public function __construct($ext = '', $dir = '', $showeveryfile = true, $dirskip = array(), $lineskip = array(), $fileskip = array()) {
- $this->setext($ext);
- $this->setdirskip($dirskip);
- $this->setfileskip($fileskip);
- $this->setlineskip($lineskip);
- $this->setshowflag($showeveryfile);
- $this->run($dir);
- }
-
- public function setext($ext) {
- trim($ext) && $this->ext = strtolower(trim($ext));
- }
- public function setshowflag($flag = true) {
- $this->showeveryfile = $flag;
- }
- public function setdirskip($dirskip) {
- $dirskip && is_array($dirskip) && $this->dirskip = $dirskip;
- }
- public function setfileskip($fileskip) {
- $this->fileskip = $fileskip;
- }
- public function setlineskip($lineskip) {
- $lineskip && is_array($lineskip) && $this->lineskip = array_merge($this->lineskip, $lineskip);
- }
-
-
-
-
- public function run($dir = '') {
- if ($dir == '') return;
- if (!is_dir($dir)) exit('path error!');
- $this->dump($dir, $this->readdir($dir));
- }
-
-
-
-
-
-
- private function dump($dir, $result) {
- $totalline = $result['totalline'];
- $linenum = $result['linenum'];
- $filenum = $result['filenum'];
- echo "*************************************************************rn<br/>";
- echo $dir . ":rn<br/>";
- echo "totalline: " . $totalline . "rn<br/>";
- echo "totalline with no comment and empty: " . $linenum . "rn<br/>";
- echo 'totalfiles:' . $filenum . "rn<br/>";
- }
-
-
-
-
-
- private function readdir($dir) {
- $num = array('totalline' => 0, 'linenum' => 0, 'filenum' => 0);
- if ($dh = opendir($dir)) {
- while (($file = readdir($dh)) !== false) {
- if ($this->skipdir($file)) continue;
- if (is_dir($dir . '/' . $file)) {
- $result = $this->readdir($dir . '/' . $file);
- $num['totalline'] += $result['totalline'];
- $num['linenum'] += $result['linenum'];
- $num['filenum'] += $result['filenum'];
- } else {
- if ($this->skipfile($file)) continue;
- list($num1, $num2) = $this->readfiles($dir . '/' . $file);
- $num['totalline'] += $num1;
- $num['linenum'] += $num2;
- $num['filenum']++;
- }
- }
- closedir($dh);
- } else {
- echo 'open dir <' . $dir . '> error!' . "r";
- }
- return $num;
- }
-
-
-
-
-
- private function readfiles($file) {
- $str = file($file);
- $linenum = 0;
- foreach ($str as $value) {
- if ($this->skipline(trim($value))) continue;
- $linenum++;
- }
- $totalnum = count(file($file));
- if (!$this->showeveryfile) return array($totalnum, $linenum);
- echo $file . "rn";
- echo 'totalline in the file:' . $totalnum . "rn";
- echo 'totalline with no comment and empty in the file:' . $linenum . "rn";
- return array($totalnum, $linenum);
- }
-
-
-
-
-
- private function skipdir($dir) {
- if (in_array($dir, $this->dirskip)) return true;
- return false;
- }
-
-
-
-
-
- private function skipfile($file) {
- if (strtolower(strrchr($file, '.')) != $this->ext) return true;
- if (!$this->fileskip) return false;
- foreach ($this->fileskip as $skip) {
- if (strpos($file, $skip) === 0) return true;
- }
- return false;
- }
-
-
-
-
-
- private function skipline($string) {
- if ($string == '') return true;
- foreach ($this->lineskip as $tag) {
- if (strpos($string, $tag) === 0) return true;
- }
- return false;
- }
- }
- ?>
|