php-hotfix/src/System/Facts.php

91 lines
2.1 KiB
PHP

<?php
namespace NoccyLabs\Hotfix\System;
use Symfony\Component\Console\Application;
class Facts
{
private $facts;
public static function getSystemFacts()
{
static $facts;
if (!$facts) {
$facts = new self();
$facts->read();
}
return $facts;
}
public function read()
{
$facts = [];
$facts['lsb'] = $this->readLsbInfo();
$facts['system'] = $this->readSystemInfo();
$this->facts = $facts;
}
private function readLsbInfo()
{
$has_lsb_release = exec("which lsb_release");
if ($has_lsb_release) {
exec("lsb_release -idrcs", $output);
$facts = (object)[
'id' => strtolower($output[0]),
'description' => $output[1],
'release' => $output[2],
'codename' => $output[3]
];
} elseif (file_exists("/etc/os-release")) {
$ini = parse_ini_file("/etc/os-release");
$facts = (object)[
'id' => strtolower(@$ini['ID']),
'description' => @$ini['PRETTY_NAME'],
'release' => @$ini['VERSION_ID'],
'codename' => @$ini['VERSION_CODENAME']
];
}
return $facts;
}
private function readSystemInfo()
{
$facts = (object)[
'bits' => trim(exec('getconf LONG_BIT')),
'arch' => trim(exec('uname -m')),
];
return $facts;
}
public function getFacts()
{
return $this->facts;
}
public function getFlat()
{
return $this->flatten($this->facts, []);
}
private function flatten($facts, array $path)
{
$ret = [];
foreach ((array)$facts as $fact=>$value) {
$sub = array_merge($path, [ $fact ]);
if (is_object($value)) {
$ret = array_merge($ret, $this->flatten($value, $sub));
} else {
$ret[join(".",$sub)] = $value;
}
}
return $ret;
}
}