2022-02-12 08:50:48 +00:00
|
|
|
<?php
|
2022-02-16 04:08:28 +00:00
|
|
|
namespace Code\Lib;
|
2022-02-12 08:50:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Infocon class: extract information and configuration structures from source modules.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
|
|
|
|
|
|
|
|
class Infocon {
|
|
|
|
|
|
|
|
public static function from_file($name) {
|
|
|
|
$info = NULL;
|
|
|
|
if (file_exists($name)) {
|
|
|
|
try {
|
2022-03-07 00:03:26 +00:00
|
|
|
$info = Yaml::parseFile($name);
|
2022-02-12 08:50:48 +00:00
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function from_str($str) {
|
|
|
|
$info = NULL;
|
|
|
|
if ($str) {
|
|
|
|
try {
|
2022-03-07 00:03:26 +00:00
|
|
|
$info = Yaml::parse($str);
|
2022-02-12 08:50:48 +00:00
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function from_c_comment($file) {
|
2022-02-20 08:12:31 +00:00
|
|
|
|
2022-02-12 08:50:48 +00:00
|
|
|
$info = NULL;
|
|
|
|
try {
|
|
|
|
$code = file_get_contents($file);
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
;
|
|
|
|
}
|
2022-02-20 08:12:31 +00:00
|
|
|
|
2022-02-12 08:50:48 +00:00
|
|
|
// Match and fetch the first C-style comment
|
|
|
|
$result = preg_match("|/\*.*\*/|msU", $code, $matches);
|
|
|
|
|
|
|
|
if ($result) {
|
2022-02-20 08:12:31 +00:00
|
|
|
|
|
|
|
$lines = explode("\n", $matches[0]);
|
2022-02-12 08:50:48 +00:00
|
|
|
foreach ($lines as $line) {
|
|
|
|
$line = trim($line, "\t\n\r */");
|
|
|
|
if ($line != "") {
|
|
|
|
list($k, $v) = array_map("trim", explode(":", $line, 2));
|
|
|
|
$k = strtolower($k);
|
|
|
|
// multiple lines with the same key are turned into an array
|
|
|
|
if (isset($info[$k])) {
|
|
|
|
if (is_array($info[$k])) {
|
|
|
|
$info[$k][] = $v;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$info[$k] = [ $info[$k], $v ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$info[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|