7Říj2008
Napsáno pod kategorií: PHP, Programování, Webdesign
Autor: LOLO
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:
-
<?php
-
/**
-
* LineCounter
-
* @author Ondřej Fibich
-
* @version 1.1
-
*
-
* Class counts number of lines, files, dirs in $dir and it's subdirs
-
*/
-
class LineCounter extends Object {
-
/** field of files where you want to count lines */
-
protected
static $ACCEPT_FILES =
array("php",
"js",
"html",
"htm",
"xhtml",
"xml",
"css",
"xsl");
-
/** dirs which sill be ignored */
-
-
/** count of lines */
-
protected $line_count;
-
/** count of dirs */
-
protected $dir_count;
-
/** count of files */
-
protected $file_count;
-
/** root dir */
-
protected $dir;
-
/**
-
* Construct - null value
-
*/
-
public function __contruct() {
-
$this->setNullValue();
-
}
-
/**
-
* Nulls value for new countion
-
*/
-
public function setNullValue() {
-
$this->line_count = 0;
-
$this->dir_count = 0;
-
$this->file_count = 0;
-
}
-
/**
-
* Counts number of lines in $file
-
* @param string $file path o file
-
* @return void
-
*/
-
protected function getLinesCount($file) {
-
-
return;
-
}
-
$this->file_count++; // another file => increment
-
$this->
line_count +=
count(file($file));
// add lines count
-
}
-
/**
-
* Scand root dir($this->dir) for files to count line. Countion is recursive so it's scan any subdir in root dir
-
* @param string $dir path of dir to scan
-
* @return void
-
*/
-
protected function scaner($dir) {
-
-
throw new InvalidArgumentException("Can't found {$dir}"); // not dir => exception
-
}
-
foreach (self::$IGNORE_DIRS as $ignore) {
-
if (mb_eregi("{$ignore}$",
$dir)) { // ignored dir
-
return;
-
}
-
}
-
$files = scandir($dir); // scans dir
-
$this->dir_count++; // another dir => increment
-
foreach ($files as $file) {
-
if (($file == ".") || ($file == "..")) { // needed step
-
continue; // we must ignore . .. or script will call recursive function again and again
-
} else if (is_dir("{$dir}/{$file}")) { // file is dir
-
$this->scaner("{$dir}/{$file}"); // scan it with recursive function
-
continue; // dir so jump to start
-
}
-
foreach (self::$ACCEPT_FILES as $bin) { // is it the file we want?
-
-
$this->getLinesCount("{$dir}/{$file}"); // count line
-
break;
-
}
-
}
-
}
-
}
-
/**
-
* Starts scan
-
*/
-
public function calculation() {
-
$this->scaner($this->dir);
-
}
-
}
-
$lineCounter = new LineCounter();
-
$lineCounter->set("dir", "..");
-
$lineCounter->calculation();
-
?>
** 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:
-
<?php
-
$lineCounter = new LineCounter();
-
$lineCounter->set("dir", "."); // vybereme aktuální adresář
-
$lineCounter->calculation(); // vypočítáme
-
echo $lineCounter->
get("dir_count") .
"<br/>";
// počet adresářů, které jsme prohledaly
-
echo $lineCounter->
get("file_count") .
"<br/>";
// počet souborů, ve kterých jsme počítali řádky
-
echo $lineCounter->
get("line_count").
"<br/>";
// celkový počet řádků
-
?>
Stažení celého programu
Pro start aplikace spusťte line_counter.php
strteo
Únor 26th, 2010 at 15:35
RH3iIx eakbammyvlib, [url=http://yrnhtggkmoxu.com/]yrnhtggkmoxu[/url], [link=http://spacrzfyjgrt.com/]spacrzfyjgrt[/link], http://mldvhpomlreg.com/
pyychmw
Únor 26th, 2010 at 15:50
UkTUid cutvcnfssvqm, [url=http://nifianbdibjb.com/]nifianbdibjb[/url], [link=http://dfyaitifdfjt.com/]dfyaitifdfjt[/link], http://rceamgabuuya.com/
Pharme933
Březen 2nd, 2010 at 3:52
Hello! ffdfeac interesting ffdfeac site!
Pharmb2
Březen 2nd, 2010 at 3:52
Very nice site! cheap viagra
Pharmg124
Březen 2nd, 2010 at 3:52
Very nice site! [url=http://oieypxa.com/kyravk/2.html]cheap cialis[/url]
Pharme619
Březen 2nd, 2010 at 3:52
Very nice site! cheap cialis http://oieypxa.com/kyravk/4.html
Pharmb194
Březen 2nd, 2010 at 3:53
Very nice site!
Pharmd743
Březen 2nd, 2010 at 3:53
Very nice site! cheap viagra , cheap viagra , cheap viagra , cheap viagra , cheap viagra ,
Allenoserly
Březen 6th, 2010 at 10:40
Making money on the internet is easy in the hush-hush world of clickbank blackhat, It’s not a big surprise if you have no clue about blackhat marketing. Blackhat marketing uses little-known or little-understood avenues to produce an income online.
lmjkvrcden
Březen 7th, 2010 at 10:19
V7gxfx naonhubpppto, [url=http://oaktguxpfyve.com/]oaktguxpfyve[/url], [link=http://vrujtysjjzgb.com/]vrujtysjjzgb[/link], http://btcagmppzsrw.com/
pokerice
Březen 8th, 2010 at 5:44
I read this forum since 2 weeks and now i have decided to register to share with you my ideas.