Skip to content

Commit

Permalink
Enhancing stat utils. See: wpsharks/comet-cache#83
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswsinc committed Aug 7, 2015
1 parent edd8a48 commit bbc7b17
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
16 changes: 10 additions & 6 deletions src/includes/closures/Plugin/CacheDirStatUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@
*
* @since 15xxxx Adding cache directory statistics.
*
* @param boolean $include_paths Include array of all scanned file paths?
*
* @return array Cache directory stats.
*/
$self->statsForCacheDir = function () use ($self) {
return $self->getDirRegexStats($self->cacheDir());
$self->statsForCacheDir = function ($include_paths = false) use ($self) {
return $self->getDirRegexStats($self->cacheDir(), '', $include_paths);
};

/*
* HTML compressor cache directory stats.
*
* @since 15xxxx Adding cache directory statistics.
*
* @param boolean $include_paths Include array of all scanned file paths?
*
* @return array HTML compressor cache directory stats.
*/
$self->statsForHtmlCCacheDirs = function () use ($self) {
$self->statsForHtmlCCacheDirs = function ($include_paths = false) use ($self) {
$stats = array(); // Initialize the stats array.

$htmlc_cache_dirs = array(); // Initialize array directories.
Expand All @@ -30,7 +34,7 @@
foreach (array_unique($htmlc_cache_dirs) as $_htmlc_cache_dir) {
$_check_disk_stats = $stats ? false : true;

foreach ($self->getDirRegexStats($_htmlc_cache_dir, '', $_check_disk_stats) as $_key => $_value) {
foreach ($self->getDirRegexStats($_htmlc_cache_dir, '', $include_paths, $_check_disk_stats) as $_key => $_value) {
$stats[$_key] = isset($stats[$_key]) ? $stats[$_key] + $_value : $_value;
}
unset($_key, $_value); // Housekeeping.
Expand Down Expand Up @@ -75,7 +79,7 @@
$_host_cache_dir = $self->nDirSeps($cache_dir.'/'.$_host_cache_path); // Normalize.
$_check_disk_stats = $stats || $___considering_domain_mapping ? false : true;

foreach ($self->getDirRegexStats($_host_cache_dir, '', $_check_disk_stats) as $_key => $_value) {
foreach ($self->getDirRegexStats($_host_cache_dir, '', false, $_check_disk_stats) as $_key => $_value) {
$stats[$_key] = isset($stats[$_key]) ? $stats[$_key] + $_value : $_value;
}
unset($_key, $_value); // Housekeeping.
Expand Down Expand Up @@ -150,7 +154,7 @@
foreach (array_unique($htmlc_cache_dirs) as $_htmlc_cache_dir) {
$_check_disk_stats = $stats ? false : true;

foreach ($self->getDirRegexStats($_htmlc_cache_dir, '', $_check_disk_stats) as $_key => $_value) {
foreach ($self->getDirRegexStats($_htmlc_cache_dir, '', false, $_check_disk_stats) as $_key => $_value) {
$stats[$_key] = isset($stats[$_key]) ? $stats[$_key] + $_value : $_value;
}
unset($_key, $_value); // Housekeeping.
Expand Down
41 changes: 30 additions & 11 deletions src/includes/closures/Shared/FsUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,40 +172,59 @@
*
* @param string $dir An absolute server directory path.
* @param string $regex A regex pattern; compares to each full file path.
* @param boolean $include_paths Include array of all scanned file paths?
* @param boolean $check_disk Also check disk statistics?
* @param boolean $reconsider Bypass cached statistics?
*
* @return array `total_size`, `total_links`, `total_files`, `total_dirs`, `disk_free_space`, `disk_total_space`.
* @return array Directory stats.
*/
$self->getDirRegexStats = function ($dir, $regex = '', $check_disk = true, $reconsider = false) use ($self) {
if (!is_null($stats = &$self->staticKey('getDirStats', array($dir, $regex, $check_disk))) && !$reconsider) {
return $stats; // Already cached this.
}
$self->getDirRegexStats = function ($dir, $regex = '', $include_paths = false, $check_disk = true) use ($self) {
$dir = (string) $dir; // Force string.

$stats = array(
'total_size' => 0,
'total_links' => 0,
'total_files' => 0,
'total_dirs' => 0,
'dir' => $dir,
'total_size' => 0,

'total_links' => 0,
'link_subpaths' => array(),

'total_files' => 0,
'file_subpaths' => array(),

'total_dirs' => 0,
'dir_subpaths' => array(),

'disk_free_space' => 0,
'disk_total_space' => 0,
);
if (!is_dir($dir = (string) $dir)) {
if (!$dir || !is_dir($dir)) {
return $stats; // Not possible.
}
foreach ($self->dirRegexIteration($dir, $regex) as $_resource) {
switch ($_resource->getType()) { // `link`, `file`, `dir`.

case 'link': // Symbolic links.
if ($include_paths) {
$stats['link_subpaths'][] = $_resource->getSubpathname();
}
++$stats['total_links']; // Counter.

break; // Break switch handler.

case 'file': // Regular files; i.e., not symlinks.
if ($include_paths) {
$stats['file_subpaths'][] = $_resource->getSubpathname();
}
$stats['total_size'] += $_resource->getSize();
++$stats['total_files']; // Counter.

break; // Break switch.

case 'dir': // Regular dirs; i.e., not symlinks.
if ($include_paths) {
$stats['dir_subpaths'][] = $_resource->getSubpathname();
}
++$stats['total_dirs']; // Counter.

break; // Break switch.
}
}
Expand Down

0 comments on commit bbc7b17

Please sign in to comment.