承接 dzava/lighthouse 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dzava/lighthouse

最新稳定版本:v0.2.0

Composer 安装命令:

composer require dzava/lighthouse

包简介

Interface for the Google Lighthouse project

关键字:

README 文档

README

This package provide a php interface for Google Lighthouse.

Installation

You can install the package via composer: composer require dzava/lighthouse

Install Lighthouse yarn add lighthouse. Last tested with Lighthouse v8.5.1.

Usage

Here's an example that will perform the default Lighthouse audits and store the result in report.json (You can use the Lighthouse Viewer to open the report):

use Dzava\Lighthouse\Lighthouse;

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

Output

The setOutput method accepts a second argument that can be used to specify the format (json,html). If the format argument is missing then the file extension will be used to determine the output format. If the file extension does not specify an accepted format, then json will be used.

You can output both the json and html reports by passing an array as the second argument. For the example the following code will create two reports example.report.html and example.report.json.

use Dzava\Lighthouse\Lighthouse;

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

Using a custom config

You can provide your own configuration file using the withConfig method.

use Dzava\Lighthouse\Lighthouse;

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

You can also pass a php array to the withConfig method containing your configuration.

use Dzava\Lighthouse\Lighthouse;

(new Lighthouse())
    ->withConfig([
         'extends' => 'lighthouse:default',
         'settings' => [
             'onlyCategories' => ['accessibility'],
         ],
     ])
    ->audit('http://example.com');

Note: in order to use an array to specify the configuration options, php needs to be able to create and move temporary files.

Details about the configuration options can be found here

Customizing node and Lighthouse paths

If you need to manually set these paths, you can do this by calling the setNodeBinary and setLighthousePath methods.

use Dzava\Lighthouse\Lighthouse;

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

Passing flags to Chrome

Use the setChromeFlags method to pass any flags to the Chrome instance.

use Dzava\Lighthouse\Lighthouse;

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

Troubleshooting

Audit of 'url' failed

Use the following snippet to check why the audit fails.

require "./vendor/autoload.php";

use Dzava\Lighthouse\Exceptions\AuditFailedException;
use Dzava\Lighthouse\Lighthouse;


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

统计信息

  • 总下载量: 10.32k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 40
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 40
  • Watchers: 2
  • Forks: 18
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-03