rocketeers-app/lighthouse-php

Google Lighthouse项目的接口

v1.0.0 2022-10-02 20:25 UTC

This package is auto-updated.

Last update: 2024-08-30 01:32:21 UTC


README

Latest Version on Packagist Total Downloads

本包提供Google Lighthouse的PHP接口。

安装

您可以通过composer安装此包

composer require rocketeers-app/lighthouse-php

安装Lighthouse

npm install lighthouse

使用方法

以下是一个示例,它将执行默认的Lighthouse审核并将结果存储在report.json中(您可以使用Lighthouse Viewer打开报告)

use Rocketeers\Lighthouse\Lighthouse;

(new Lighthouse())
    ->setOutput('report.json')
    ->accessibility()
    ->bestPractices()
    ->performance()
    ->pwa()
    ->seo()
    ->audit('http://example.com');

输出

setOutput方法接受第二个参数,可以用来指定格式(json、html)。如果省略格式参数,则将使用文件扩展名来确定输出格式。如果文件扩展名未指定一个接受的格式,则将使用json。

您可以通过传递一个数组作为第二个参数来输出json和html报告。在示例中,以下代码将创建两个报告example.report.htmlexample.report.json

use Rocketeers\Lighthouse\Lighthouse;

(new Lighthouse())
    ->setOutput('example', ['html', 'json'])
    ->performance()
    ->audit('http://example.com');

使用自定义配置

您可以使用withConfig方法提供自己的配置文件。

use Rocketeers\Lighthouse\Lighthouse;

(new Lighthouse())
    ->withConfig('./my-config.js')
    ->audit('http://example.com');

自定义node和Lighthouse路径

如果您需要手动设置这些路径,可以通过调用setNodeBinarysetLighthousePath方法来完成。

use Rocketeers\Lighthouse\Lighthouse;

(new Lighthouse())
    ->setNodeBinary('/usr/bin/node')
    ->setLighthousePath('./node_modules/lighthouse/lighthouse-cli/index.js')
    ->audit('http://example.com');

传递标志到Chrome

使用setChromeFlags方法向Chrome实例传递任何标志。

use Rocketeers\Lighthouse\Lighthouse;

(new Lighthouse())
    // these are the default flags used
    ->setChromeFlags(['--headless', '--disable-gpu', '--no-sandbox'])
    ->audit('http://example.com');

故障排除

'url'审核失败

使用以下片段来检查审核失败的原因。

require "./vendor/autoload.php";

use Rocketeers\Lighthouse\Exceptions\AuditFailedException;
use Rocketeers\Lighthouse\Lighthouse;


try {
(new Lighthouse())
    ->performance()
    ->audit('http://example.com');
} catch(AuditFailedException $e) {
    echo $e->getOutput();
}