Skip to content

Instantly share code, notes, and snippets.

@binarykore
Created June 23, 2023 14:56
Show Gist options
  • Select an option

  • Save binarykore/29ea335114703279b0813faca5cb55d7 to your computer and use it in GitHub Desktop.

Select an option

Save binarykore/29ea335114703279b0813faca5cb55d7 to your computer and use it in GitHub Desktop.
Lines of Code Counter and JSON API for PHP-based Files, and written via PHP - Working for Vercel and other Web Hosting which allows PHP Runtime for a Singular Directory only, can be used for CLI ..
<?php
class counter{
protected static $_hash = [];
protected static $_loc = [];
protected static $_fcount = [];
protected static $_total = [];
protected static function loc($_filename){
$_loc = @fopen($_filename,"rb");
$_string = fread($_loc,filesize($_filename));
$_count = (count(explode("<br/>",(str_replace(["\n","\r","\s"],["<br/>","<br/>","&nbsp;"],$_string)))));
return($_count);
}//Get Lines of Code from File of Source Code..
public static function scan($_directory = NULL){
if(file_exists($_directory)){
$_dir = scandir($_directory);
foreach($_dir as $_token => $_coin){
if($_coin == "." || $_coin == ".."){
continue;
}elseif($_coin != "." && $_coin != ".."){
if(mime_content_type($_directory."/".$_coin) == "directory"){
self::$_hash[] = (("Type: ").(mime_content_type($_directory."/".$_coin)).(" | ").("File: ".($_coin)));
}else{
if(mime_content_type($_directory."/".$_coin) == "text/x-php"){
self::$_hash[] = (("Type: ").(mime_content_type($_directory."/".$_coin)).(" | ").("File: ".($_coin)));
self::$_loc[] = self::loc($_directory."/".$_coin);
}else{
continue;
}//If not PHP dont read, continue..
}//
}else{
continue;
}//Check if File is Iterative..
}//Scan Directory for Access..
self::$_fcount = (array_sum(self::$_loc));
}elseif(!file_exists($_directory)){
//If Directory does not Exists, throw Error..
self::$_hash[] = "404";
self::$_fcount[] = "404";
self::$_loc[] = "404";
}//Directories Check..
return(["filetypes" => self::$_hash,"files" => self::$_loc,"filecount" => self::$_fcount]);
}//Scan Directory PHP Recursive..
}//
if(!empty($_GET["path"])){
$_path = $_GET["path"];
}elseif(empty($_GET["path"])){
$_path = "api";
}//
Header("Content-Type:Application/JSON");
print_r(counter::scan($_SERVER["DOCUMENT_ROOT"]."/".($_path)));
//List All Files according to Directory..
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment