streams/Code/Lib/Infocon.php

81 lines
2 KiB
PHP
Raw Normal View History

<?php
2022-02-16 04:08:28 +00:00
namespace Code\Lib;
/**
* Infocon class: extract information and configuration structures from source modules.
*/
2022-03-07 05:39:21 +00:00
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);
}
catch (Exception $e) {
2022-10-09 11:04:11 +00:00
logger('exception: ' . $e->getMessage());
}
}
return $info;
2022-10-09 11:04:11 +00:00
}
2022-10-09 11:04:11 +00:00
/** @noinspection PhpUnused */
public static function from_str($str) {
$info = NULL;
if ($str) {
try {
2022-03-07 00:03:26 +00:00
$info = Yaml::parse($str);
}
catch (Exception $e) {
2022-10-09 11:04:11 +00:00
logger('exception: ' . $e->getMessage());
}
}
return $info;
}
2022-10-09 11:04:11 +00:00
public static function from_c_comment($file): array|null {
2022-02-20 08:12:31 +00:00
$info = NULL;
try {
$code = file_get_contents($file);
}
catch (Exception $e) {
2022-10-09 11:04:11 +00:00
logger('exception: ' . $e->getMessage());
}
2022-02-20 08:12:31 +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]);
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;
}
2022-10-09 11:04:11 +00:00
}