Advertisement
Guest User

Count lines of code in folder - PHP Class

a guest
Jun 29th, 2014
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.77 KB | None | 0 0
  1.  
  2.     define('SHOW_DETAILS', true);
  3.  
  4.     class Folder {
  5.  
  6.         var $name;
  7.         var $path;
  8.         var $folders;
  9.         var $files;
  10.         var $exclude_extensions;
  11.         var $exclude_files;
  12.         var $exclude_folders;
  13.  
  14.  
  15.         function Folder($path) {
  16.             $this -> path       = $path;
  17.             $this -> name       = array_pop( array_filter( explode(DIRECTORY_SEPARATOR, $path) ) );
  18.             $this -> folders    = array();
  19.             $this -> files      = array();
  20.             $this -> exclude_extensions = array('gif', 'jpg', 'jpeg', 'png', 'tft', 'bmp', 'rest-of-the-file-extensions-to-exclude');
  21.             $this -> exclude_files      = array('count_lines.php', 'rest-of-the-files-to-exclude');
  22.             $this -> exclude_folders     = array('_private', '_vti_bin', '_vti_cnf', '_vti_log', '_vti_pvt', '_vti_txt', 'rest-of-the-folders-to-exclude');
  23.         }
  24.  
  25.         function count_lines() {
  26.             if( defined('SHOW_DETAILS') ) print "";
  27.             $total_lines = 0;
  28.             $this -> get_contents();
  29.             foreach($this -> files as $file) {
  30.                 if( in_array($file -> ext, $this -> exclude_extensions) || in_array($file -> name, $this -> exclude_files) ) {
  31.                     if( defined('SHOW_DETAILS') ) print "#---Skipping File: {$file -> name};\n";
  32.                     continue;
  33.                 }
  34.                 $total_lines += $file -> get_num_lines();
  35.             }
  36.             foreach($this -> folders as $folder) {
  37.                 if( in_array($folder -> name, $this -> exclude_folders) ) {
  38.                     if( defined('SHOW_DETAILS') ) print "#Skipping Folder: {$folder -> name};\n";
  39.                     continue;
  40.                 }
  41.                 $total_lines += $folder -> count_lines();
  42.             }
  43.             if( defined('SHOW_DETAILS') ) print "";
  44.             return $total_lines;
  45.         }
  46.  
  47.         function get_contents() {
  48.             $contents = $this -> _get_contents();
  49.             foreach($contents as $key => $value) {
  50.                 if( $value['type'] == 'Folder' ) {
  51.                     $this -> folders[] = new Folder($value['item']);
  52.                 } else {
  53.                     $this -> files[]   = new File  ($value['item']);
  54.                 }
  55.             }
  56.         }
  57.  
  58.         function _get_contents() {
  59.             $folder = $this -> path;
  60.             if( !is_dir($folder) ) {
  61.                 return array();
  62.             }
  63.             $return_array = array();
  64.             $count        = 0;
  65.             if( $dh = opendir($folder) ) {
  66.                 while( ($file = readdir($dh)) !== false ) {
  67.                     if( $file == '.' || $file == '..' ) continue;
  68.                     $return_array[$count]['item']   = $folder .$file .(is_dir($folder .$file) ? DIRECTORY_SEPARATOR : '');
  69.                     $return_array[$count]['type']   = is_dir($folder .$file) ? 'Folder' : 'File';
  70.                     $count++;
  71.                 }
  72.                 closedir($dh);
  73.             }
  74.             return $return_array;
  75.         }
  76.  
  77.     } // Class
  78.  
  79.     class File {
  80.  
  81.         var $name;
  82.         var $path;
  83.         var $ext;
  84.  
  85.  
  86.         function File($path) {
  87.             $this -> path = $path;
  88.             $this -> name = basename($path);
  89.             $this -> ext  = array_pop( explode('.', $this -> name) );
  90.         }
  91.  
  92.         function get_num_lines() {
  93.             $count_lines = count(file($this -> path));
  94.             if( defined('SHOW_DETAILS') ) print "";
  95.             return $count_lines;
  96.         }
  97.  
  98.     } // Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement