Po dopsání docela robustní aplikace v PHP, jsem zatoužil vědět, do kolika řádků jsem tuto aplikaci vlastně vměstnal. Po krátké úvaze jsem navrhl a napsal malou třídu která tuto čínnost umožňuje.

Počítání řádků v souboru

Pro tuto akci lze použít funkci file(), která načte doubor do pole, kde každý prvek je jeden řádek souboru. Stačí tedy potom zjistit velikost pole pomocí fukce count() a počet řádků souboru je na světě.

Hierarchické prohledávání adresářů

Jistě by bylo od věci nehledat soubory jen v základním adresáři ale i v jeho podadresářích. Pro takový případ je nejlepším řešením použít rekurzivní funkci.

Výsledná třída

Z uvedených informací již není složité naprogramovat script který bude tyto akce zvládat.

PHP:
  1. <?php
  2. /**
  3. * LineCounter
  4. * @author Ondřej Fibich
  5. * @version 1.1
  6. *
  7. * Class counts number of lines, files, dirs in $dir and it's subdirs
  8. */
  9. class LineCounter extends Object {
  10.     /** field of files where you want to count lines */
  11.     protected static $ACCEPT_FILES = array("php", "js", "html", "htm", "xhtml", "xml", "css", "xsl");
  12.     /** dirs which sill be ignored */
  13.     protected static $IGNORE_DIRS = array("fckeditor");
  14.     /** count of lines */
  15.     protected $line_count;
  16.     /** count of dirs */
  17.     protected $dir_count;
  18.     /** count of files */
  19.     protected $file_count;
  20.     /** root dir */
  21.     protected $dir;
  22.     /**
  23.      * Construct - null value
  24.      */
  25.     public function __contruct() {
  26.         $this->setNullValue();
  27.     }
  28.     /**
  29.      * Nulls value for new countion
  30.      */
  31.     public function setNullValue() {
  32.         $this->line_count = 0;
  33.         $this->dir_count = 0;
  34.         $this->file_count = 0;
  35.     }
  36.     /**
  37.      * Counts number of lines in $file
  38.      * @param string $file path o file
  39.      * @return void
  40.      */
  41.     protected function getLinesCount($file) {
  42.         if (! is_file($file) {           
  43.             return;
  44.         }
  45.         $this->file_count++; // another file => increment
  46.         $this->line_count += count(file($file)); // add lines count
  47.     }
  48.     /**
  49.      * Scand root dir($this->dir) for files to count line. Countion is recursive so it's scan any subdir in root dir
  50.      * @param string $dir path of dir to scan
  51.      * @return void
  52.      */
  53.     protected function scaner($dir) {
  54.         if (! is_dir($dir) || ! file_exists($dir)) { // test dir
  55.             throw new InvalidArgumentException("Can't found {$dir}"); // not dir => exception
  56.         }
  57.         foreach (self::$IGNORE_DIRS as $ignore) {
  58.             if (mb_eregi("{$ignore}$", $dir)) { // ignored dir
  59.                 return;
  60.             }
  61.         }
  62.         $files = scandir($dir); // scans dir
  63.         $this->dir_count++; // another dir => increment
  64.         foreach ($files as $file) {
  65.             if (($file == ".") || ($file == "..")) { // needed step
  66.                 continue; // we must ignore . .. or script will call recursive function again and again
  67.             } else if (is_dir("{$dir}/{$file}")) { // file is dir
  68.                 $this->scaner("{$dir}/{$file}"); // scan it with recursive function
  69.                 continue; // dir so jump to start
  70.             }
  71.             foreach (self::$ACCEPT_FILES as $bin) { // is it the file we want?
  72.                 if (mb_eregi("{$bin}$", $file)) {
  73.                     $this->getLinesCount("{$dir}/{$file}"); // count line
  74.                     break;
  75.                 }
  76.             }
  77.         }
  78.     }
  79.     /**
  80.      * Starts scan
  81.      */
  82.     public function calculation() {
  83.         $this->scaner($this->dir);
  84.     }
  85. }
  86. $lineCounter = new LineCounter();
  87. $lineCounter->set("dir", "..");
  88. $lineCounter->calculation();
  89. ?>

** Pozn.: třída object již byla představena v mém předchozím čánku Zlepšujeme PHP vlastnostmi Javy, díl 1.

Použití třídy

PHP:
  1. <?php
  2. $lineCounter = new LineCounter();
  3. $lineCounter->set("dir", "."); // vybereme aktuální adresář
  4. $lineCounter->calculation(); // vypočítáme
  5. echo $lineCounter->get("dir_count") . "<br/>"; // počet adresářů, které jsme prohledaly
  6. echo $lineCounter->get("file_count") . "<br/>"; // počet souborů, ve kterých jsme počítali řádky
  7. echo $lineCounter->get("line_count"). "<br/>"; // celkový počet řádků
  8. ?>

Stažení celého programu

Stáhnout line_counter.zip

Pro start aplikace spusťte line_counter.php