Skip to content

Instantly share code, notes, and snippets.

@clouddueling
Last active December 28, 2015 16:38
Show Gist options
  • Select an option

  • Save clouddueling/7529939 to your computer and use it in GitHub Desktop.

Select an option

Save clouddueling/7529939 to your computer and use it in GitHub Desktop.
<?php
class Analyze
{
/**
* You can either scan your entire project or select some folders.
*
* @var array
*/
public static $dirs = array();
/**
* Holds the number of different filetypes in your project
*
* @var int
*/
public static $filetypes = array();
private static $instance;
public static function dirs($dirs)
{
if (is_null(self::$instance)) {
self::$instance = new self();
}
self::$dirs = $dirs;
return self::$instance;
}
/**
* Totals up all recorded data.
*
* @param array $results
* @return int
*/
public function scan()
{
foreach (self::$dirs as $dir) {
$results = self::getDirStats($dir);
foreach ($results as $file) {
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (! isset(self::$filetypes[$ext])) {
self::$filetypes[$ext] = array(
'lines' => 0,
'bytes' => 0,
'count' => 0
);
}
$file_contents = file_get_contents($file);
++self::$filetypes[$ext]['count'];
self::$filetypes[$ext]['lines'] += count(explode("\n", $file_contents));
self::$filetypes[$ext]['bytes'] += filesize($file);
}
}
return self::$instance;
}
/**
* Recursively read all files into an array.
*
* @param array $dir
* @return array
*/
public function getDirStats($dir = array())
{
$root = scandir($dir);
$result = array();
foreach ($root as $value) {
if ($value === '.' || $value === '..') {
continue;
}
if (is_file("{$dir}/{$value}")) {
$result[] = "{$dir}/{$value}";
continue;
}
foreach ($this->getDirStats("$dir/$value") as $value) {
$result[] = $value;
}
}
return $result;
}
public function sort($key = 'bytes', $asc = true)
{
$sorter = array();
$ret = array();
reset(self::$filetypes);
foreach (self::$filetypes as $ii => $va) {
$sorter[$ii] = $va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii] = self::$filetypes[$ii];
}
self::$filetypes = $ret;
if (! $asc)
self::$filetypes = array_reverse(self::$filetypes);
}
/**
* Save the data to json
*
* @return void
*/
public function save($filename)
{
if (is_file($filename)) {
$contents = file_get_contents($filename);
$contents = strlen($contents) > 0 ? json_decode($contents) : array();
} else {
$contents = array();
}
self::sort();
array_push($contents, self::$filetypes);
file_put_contents($filename, json_encode($contents));
return self::$instance;
}
public function report()
{
foreach (self::$filetypes as $ext => $type) {
echo "{$ext}\n" .
"Lines: " . number_format($type['lines']) . "\n" .
"Bytes: " . number_format($type['bytes']) . "\n" .
"Number of files: " . number_format($type['count']) .
"\n-------------------------------------------------------\n";
}
return self::$instance;
}
}
<?php
class Analyze_Task
{
public function run($arguments)
{
Analyze::dirs(array(
"./application",
path("public")
))
->scan()
// Somewhere outside of your git repo.
->save("../analyze/clouddueling.json")
->report();
}
}
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
*/30 * * * * cd ~/Sites/clouddueling && php artisan analyze
[{"tmp":{"lines":2,"bytes":0,"count":2},"bowerrc":{"lines":9,"bytes":108,"count":3},"jshintrc":{"lines":12,"bytes":176,"count":1},"gitattributes":{"lines":15,"bytes":194,"count":2},"gitignore":{"lines":62,"bytes":608,"count":11},"sln":{"lines":21,"bytes":915,"count":1},"license":{"lines":21,"bytes":1061,"count":1},"user":{"lines":32,"bytes":1195,"count":1},"srt":{"lines":61,"bytes":1371,"count":1},"htaccess":{"lines":71,"bytes":2352,"count":3},"py":{"lines":147,"bytes":4545,"count":1},"ico":{"lines":4,"bytes":4600,"count":4},"csproj":{"lines":112,"bytes":4866,"count":1},"yml":{"lines":249,"bytes":7418,"count":10},"":{"lines":175,"bytes":8225,"count":8},"mdown":{"lines":130,"bytes":8916,"count":1},"txt":{"lines":210,"bytes":11467,"count":6},"xap":{"lines":58,"bytes":12461,"count":1},"xaml":{"lines":285,"bytes":18274,"count":2},"jpeg":{"lines":75,"bytes":18493,"count":2},"json":{"lines":366,"bytes":19212,"count":19},"cs":{"lines":742,"bytes":22990,"count":3},"fla":{"lines":79,"bytes":26182,"count":1},"manifest":{"lines":789,"bytes":26686,"count":4},"xml":{"lines":595,"bytes":59355,"count":4},"swf":{"lines":231,"bytes":64325,"count":3},"as":{"lines":1884,"bytes":67304,"count":6},"scss":{"lines":4460,"bytes":99952,"count":22},"md":{"lines":2719,"bytes":113933,"count":33},"less":{"lines":5380,"bytes":120277,"count":25},"otf":{"lines":5715,"bytes":225160,"count":4},"ogv":{"lines":700,"bytes":259119,"count":1},"woff":{"lines":1072,"bytes":301360,"count":9},"eot":{"lines":2546,"bytes":338722,"count":9},"gif":{"lines":3157,"bytes":530795,"count":14},"map":{"lines":144,"bytes":629086,"count":16},"ds_store":{"lines":286,"bytes":673096,"count":82},"webm":{"lines":3539,"bytes":903634,"count":1},"svg":{"lines":3565,"bytes":1171566,"count":34},"php":{"lines":53398,"bytes":1497848,"count":415},"css":{"lines":40933,"bytes":1906710,"count":113},"ttf":{"lines":20989,"bytes":2811556,"count":21},"psd":{"lines":7715,"bytes":2860495,"count":6},"html":{"lines":80312,"bytes":3876052,"count":989},"jar":{"lines":22843,"bytes":4790673,"count":2},"mp4":{"lines":30367,"bytes":6850873,"count":2},"js":{"lines":365611,"bytes":14365412,"count":1053},"png":{"lines":126106,"bytes":32092872,"count":1461},"jpg":{"lines":141908,"bytes":54422658,"count":44}}]
-------------------------------------------------------
php
Lines: 53,398
Bytes: 1,497,848
Number of files: 415
-------------------------------------------------------
css
Lines: 40,933
Bytes: 1,906,710
Number of files: 113
-------------------------------------------------------
ttf
Lines: 20,989
Bytes: 2,811,556
Number of files: 21
-------------------------------------------------------
psd
Lines: 7,715
Bytes: 2,860,495
Number of files: 6
-------------------------------------------------------
html
Lines: 80,312
Bytes: 3,876,052
Number of files: 989
-------------------------------------------------------
jar
Lines: 22,843
Bytes: 4,790,673
Number of files: 2
-------------------------------------------------------
mp4
Lines: 30,367
Bytes: 6,850,873
Number of files: 2
-------------------------------------------------------
js
Lines: 365,611
Bytes: 14,365,412
Number of files: 1,053
-------------------------------------------------------
png
Lines: 126,106
Bytes: 32,092,872
Number of files: 1,461
-------------------------------------------------------
jpg
Lines: 141,908
Bytes: 54,422,658
Number of files: 44
-------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment