Last active
August 14, 2019 20:30
-
-
Save eusonlito/6fd66a9b83385da91a4b3f31fef7f08b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
namespace App\Services\Opcache; | |
use RecursiveDirectoryIterator; | |
use RecursiveIteratorIterator; | |
use RecursiveRegexIterator; | |
use RegexIterator; | |
class Generate | |
{ | |
/** | |
* @param string $dir | |
* | |
* @return array | |
*/ | |
public static function compile(string $dir): array | |
{ | |
$dir = realpath($dir); | |
$stats = []; | |
foreach (static::files($dir) as $file) { | |
$stats[str_replace($dir, '', $file)] = @opcache_compile_file($file); | |
} | |
return $stats; | |
} | |
/** | |
* @param string $dir | |
* | |
* @return array | |
*/ | |
protected static function files(string $dir): array | |
{ | |
clearstatcache(true); | |
$directory = new RecursiveDirectoryIterator($dir); | |
$iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST); | |
$files = new RegexIterator($iterator, '/^.+\.php$/', RecursiveRegexIterator::GET_MATCH); | |
$files = array_unique(array_filter(array_keys(iterator_to_array($files)), static function ($file) { | |
return stripos($file, '/tests/') === false; | |
})); | |
usort($files, [__CLASS__, 'sort']); | |
return $files; | |
} | |
/** | |
* @param string $a | |
* @param string $b | |
* | |
* @return int | |
*/ | |
protected static function sort(string $a, string $b): int | |
{ | |
$a = file_get_contents($a); | |
if (preg_match('/^\s*class .+ (extends|implements) /i', $a)) { | |
return 1; | |
} | |
if (preg_match('/^\s*class .+ (extends|implements) /i', file_get_contents($b))) { | |
return -1; | |
} | |
return preg_match('/^\s*use .+[,;]$/', $a) ? 1 : -1; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
namespace App\Services\Opcache; | |
class Stats | |
{ | |
/** | |
* @param bool $files = false | |
* | |
* @return array | |
*/ | |
public static function status(bool $files = false): array | |
{ | |
return opcache_get_status($files); | |
} | |
/** | |
* @return array | |
*/ | |
public static function stats(): array | |
{ | |
$mega = 1024 * 1024; | |
$values = static::status(); | |
$stats = $values['opcache_statistics']; | |
$values['uptime'] = static::date($stats['start_time']); | |
$values['last_restart'] = static::date($stats['last_restart_time']); | |
$mem = $values['memory_usage']; | |
$values['limit_maxbytes'] = $mem['used_memory'] + $mem['free_memory'] + $mem['wasted_memory']; | |
$values['bytes_percent'] = round(($mem['used_memory'] / $values['limit_maxbytes']) * 100); | |
$values['bytes_free_percent'] = round(100 - $values['bytes_percent'], 2); | |
$values['megas'] = round($mem['used_memory'] / $mega, 2); | |
$values['limit_maxmegas'] = round($values['limit_maxbytes'] / $mega, 2); | |
$values['megas_free'] = $values['limit_maxmegas'] - $values['megas']; | |
$values['megas_wasted_memory'] = round($mem['wasted_memory'] / $mega, 2); | |
$values['hits_percent'] = round($stats['opcache_hit_rate'], 2); | |
$values['misses_percent'] = round(100 - $values['hits_percent'], 2); | |
return $values; | |
} | |
/** | |
* @param int $time | |
* | |
* @return string | |
*/ | |
protected static function date(int $time): string | |
{ | |
if ($time === 0) { | |
return '-'; | |
} | |
$date = time() - $time; | |
if (($date / (3600 * 24)) > 1) { | |
$date = round($date / (3600 * 24), 2).' days'; | |
} elseif (($date / 3600) > 1) { | |
$date = round($date / 3600, 2).' hours'; | |
} elseif (($date / 60) > 1) { | |
$date = round($date / 60, 2).' minutes'; | |
} else { | |
$date = $date.' seconds'; | |
} | |
return $date; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment