stechstudio/phpinfo

轻松交互 phpinfo() 配置

0.3 2024-03-21 13:31 UTC

This package is auto-updated.

Last update: 2024-09-21 14:29:03 UTC


README

此包将从 phpinfo() 获取输出,并提供以下内容

  1. 检查特定模块和配置的查找方法
  2. 基于集合的数据结构,用于迭代和构建自定义输出
  3. 一个漂亮、响应式、可搜索的界面,可替换默认的 phpinfo() 页面

安装

composer require stechstudio/phpinfo

快速入门

如果您想显示一个漂亮、移动友好的 phpinfo() 页面,只需在 Info 工厂类上调用 render()

<?php
// Make sure this points to your composer autoload file, if you are using plain PHP.
// If you are in a framework context, you can probably remove this line as your
// framework likely handles it for you.
require __DIR__ . '/../vendor/autoload.php';

// This will capture your current phpinfo() and display a prettier page.
STS\Phpinfo\Info::render();
?>

phpinfo() 配置交互

如果您想直接检查和交互配置,首先需要捕获它

use STS\Phpinfo\Info;

$info = Info::capture();

如果您有之前保存的 phpinfo() 输出,并想加载和解析

use STS\Phpinfo\Info;

// If you've saved the HTML output from phpinfo()
$info = Info::fromHtml($yourSavedHtmlOutput);

// If you've saved the CLI output from phpinfo()
$info = Info::fromText($yourSavedHtmlOutput);

从这里,您可以查询一些基本信息、模块和配置

// Your PHP version
$info->version(); // 8.2.0

// Check for the presence of a specific module. Name is case-insensitive.
$info->hasModule('redis'); // true

// Check to see if a specific configuration key is present. Name is case-insensitive.
$info->hasConfig('ICU version'); // true

// Retrieve the value for a specific configuration key. Name is case-insensitive. If there is both a local and master value, the local is returned as default.
$info->config('max_file_uploads'); // 5

// Pass in 'master' as a second parameter to retrieve the master value instead. Note that this will return null if there is no master value;
$info->config('max_file_uploads', 'master'); // 20
$info->config('BCMath support', 'master'); // null

迭代数据结构

您可以访问 collections 数据结构,轻松遍历 phpinfo() 配置。

// Loop over defined modules
foreach($info->modules() AS $module) {
    $module->name(); // session
    
    // Configs are grouped the same way phpinfo() groups them by table
    // Different groups have different table headers, different number of values
    foreach($module->groups() AS $group) {
        $group->headings(); // [Directive, Local Value, Master Value]
        
        foreach($group->configs() AS $config) {
            $config->name(); // session.gc_maxlifetime
            $config->localValue(); // 1440
            
            $config->hasMasterValue(); // True (will be false if there is only one value)
            $config->masterValue(); // 28800
        }
    }
}

您可以看到数据结构有四个级别

  1. 包含 modules() 的基本 info
  2. 具有 name() 方法的模块,并包含 groups()
  3. 包含 configs() 和可选的 headings() 的组
  4. 具有 name()value()/localValue() 和可选的 masterValue() 的配置

您还可以直接从模块和基本信息级别访问配置

// This flattens the grouped 'session' configs down to a single collection
$info->module('session')->configs();

// This flattens ALL configs across all modules down to a single collection
$info->configs();

模块和组

我们已经看到了如何遍历模块和组。有时您可能想查找特定模块并直接检查它。

// This lookup is case-insensitive. Will return null if no matching module is found.
$module = $info->module('zend opcache');

// Retrieve the name of the module as displayed in phpinfo(), which might have a different case.
$module->name(); // Zend OPcache

// Flatten all configs into one collection. You can then use any Laravel collection method.
$module->configs()->count(); // 59

// Retrieve a specific configuration from this module. This works exactly the same as the main `config()` method shown in the previous section.
$module->config('Max keys'); // 16229
$module->config('opcache.enable_file_override', 'master'); // Off

// Retrieve just the first group of configs, which is often the list of single-value configs
$group = $info->module('session')->groups()->first(); // Collection of Configs

以下是一个显示模块和配置的非常简单的示例

foreach ($info->modules() AS $module) {
    echo '<h2>' . $module->name() . '</h2>';

    echo '<ul>';
    foreach($module->configs() AS $config) {
        echo '<li>';
        echo $config->name() . ': ' . $config->value();
        if($config->hasMasterValue()) {
            echo ' (master: ' . $config->masterValue() . ')';
        }
        echo '</li>';
    }
    echo '</ul>';
}