Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feat(system): can get more system info via class SystemInfoHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Jul 23, 2019
1 parent d871952 commit 6dc1028
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 33 deletions.
8 changes: 6 additions & 2 deletions bin/rid-httpd
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

require __DIR__ . '/../vendor/autoload.php';

Rid\Base\Env::load(__DIR__ . '/../.env'); // 获取环境变量
if (!\Rid\Helpers\SystemInfoHelper::isCli())
exit('Run in cli like: `php bin/rid-httpd service start`.');

$config = require __DIR__ . '/../apps/config/httpd.php';
// 获取环境变量
Rid\Base\Env::load(__DIR__ . '/../.env');

$config = require __DIR__ . '/../apps/config/httpd.php';
$exitCode = (new Rid\Console\Application($config))->run();
exit($exitCode);
29 changes: 0 additions & 29 deletions framework/Helpers/PhpInfoHelper.php

This file was deleted.

2 changes: 1 addition & 1 deletion framework/Helpers/ProcessHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function daemon($closeStandardInputOutput = true)
// 设置进程标题
public static function setTitle($title)
{
if (PhpInfoHelper::isMac()) {
if (SystemInfoHelper::isMac()) {
return false;
}
if (!function_exists('cli_set_process_title')) {
Expand Down
91 changes: 91 additions & 0 deletions framework/Helpers/SystemInfoHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Rid\Helpers;

/**
* PhpInfoHelper
*/
class SystemInfoHelper
{

/** 是否为 CLI 模式
* @return bool
*/
public static function isCli(): bool
{
return PHP_SAPI === 'cli';
}

/** 是否为 Win 系统
* @return bool
*/
public static function isWin(): bool
{
return stripos(PHP_OS, 'WINNT') !== false;
}

/**
* 是否为 Mac 系统
* @return bool
*/
public static function isMac(): bool
{
return stripos(PHP_OS, 'Darwin') !== false;
}

public static function getAvg(): array
{
if (is_readable('/proc/loadavg')) {
$loadavg = explode(' ', file_get_contents('/proc/loadavg'));
return array_slice($loadavg, 0, 3);
}
return [0, 0, 0];
}

public static function getMemory(): array
{
if (is_readable('/proc/meminfo')) {
$content = file_get_contents('/proc/meminfo');
preg_match('/^MemTotal: \s*(\d*)/m', $content, $matches_total);
$total = $matches_total[1] * 1024;
preg_match('/^MemFree: \s*(\d*)/m', $content, $matches_free);
$free = $matches_free[1] * 1024;
preg_match('/^MemAvailable: \s*(\d*)/m', $content, $matches_available);
if ($matches_available) {
$used = $matches_available[1] * 1024;
} else {
$used = $total - $free;
}
return ['total' => $total, 'free' => $free, 'used' => $used];
}
return ['total' => 0, 'free' => 0, 'used' => 0];
}

public static function getUptime(): int
{
if (is_readable('/proc/uptime')) {
return (float)file_get_contents('/proc/uptime');
}
return 0;
}

public static function getProcessor(): int
{
if (is_readable('/proc/cpuinfo')) {
return (int)substr_count(file_get_contents('/proc/cpuinfo'), 'processor');
}
return 0;
}

public static function getIdlePercent(): float
{
if (is_readable('/proc/cpuinfo') && is_readable('/proc/uptime')) {
$processors = self::getProcessor();
if ($processors == 0) return 0;

[$uptime, $idle] = explode(' ', file_get_contents('/proc/uptime'));
return (float)$idle / ((float)$uptime * $processors);
}
return 0;
}
}
1 change: 0 additions & 1 deletion framework/Validators/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ final public function getData($key)
protected function buildDefaultDataForValid()
{
\Rid::setDefault($this->_data, static::defaultData());
if (env('APP_DEBUG')) var_dump($this->_data);
}

protected function buildDefaultPropBeforeValid()
Expand Down

0 comments on commit 6dc1028

Please sign in to comment.