scarlett / lshw-parser
一个简单的PHP lshw解析器,没有非扩展依赖。
1.0.3
2024-08-01 04:38 UTC
Requires
- php: ^8.2
- ext-dom: *
- ext-mbstring: *
- ext-xml: *
Requires (Dev)
- phpunit/phpunit: ^11.0
README
这个PHP库可以解析来自lshw命令的XML输出,以提取Linux系统上的硬件信息。
特性
- 解析lshw XML输出。
- 检索系统内存、CPU、存储设备和网络接口信息。
- 基于属性的节点动态过滤。
- 使用可调用的函数自定义过滤逻辑。
- 可以启用Skiphubs来跳过hub/bridge对象。
- >85% 代码覆盖率。
安装
使用Composer安装库
sh composer require scarlett/lshw-parser
用法
解析XML输出
use Scarlett\LshwParser\Parser; $xmlContent = file_get_contents('path/to/lshw-output.xml'); $parser = new Parser($xmlContent); // Get system memory information $memory = $parser->getSystemMemory(); echo $memory->getProperty('description')->getFirstValue(); // Get CPU information $cpuInfo = $parser->getCpuInfo(); echo $cpuInfo->getProperty('vendor')->getFirstValue();
根据属性过滤节点
use Scarlett\LshwParser\Parser; $xmlContent = file_get_contents('path/to/lshw-output.xml'); $parser = new Parser($xmlContent); // Filter nodes with AND logic $results = $parser->parseByProperties(['class' => 'processor', 'vendor' => 'Intel Corp.'], 'and'); foreach ($results as $result) { echo $result->getProperty('description')->getFirstValue(); } // Filter nodes with OR logic $results = $parser->parseByProperties(['class' => 'processor', 'vendor' => 'AMD'], 'or'); foreach ($results as $result) { echo $result->getProperty('description')->getFirstValue(); }
使用可调用的函数自定义过滤
use Scarlett\LshwParser\Parser; $xmlContent = file_get_contents('path/to/lshw-output.xml'); $parser = new Parser($xmlContent); $results = $parser->searchNodesByFilter(function($properties) { return isset($properties['vendor']) && $properties['vendor'] === 'Intel Corp.'; }); foreach ($results as $result) { echo $result->getProperty('description')->getFirstValue(); }
跳过hub
解析器类中的skipHubs特性(默认:OFF)允许在解析过程中跳过被识别为hub的节点。以下正则表达式模式用于匹配节点描述
/usb\s*(hub|2(\.0)?\s*hub|3(\.0)?\s*hub)/i
:匹配"usb hub"、"usb2 hub"、"usb2.0 hub"、"usb3 hub"、"usb3.0 hub"。/(pci(e)?\s*bridge)/i
:匹配"pci bridge"、"pcie bridge"。/(isa\s*bridge)/i
:匹配"isa bridge"。/hub/i
:匹配"hub"。
将skipHubs标志设置为true启用此功能,确保这些节点被排除在结果之外。要启用public function __construct(string $xmlContent, bool $skipHubs = true)
或实例化时setSkipHubs(bool) -> void
。
运行测试
项目使用PHPUnit进行单元测试。要运行测试,使用以下命令
./vendor/bin/phpunit .
贡献
欢迎贡献!请为任何更改打开一个问题或提交一个pull请求。
许可证
本项目采用MIT许可证。