lightship-core / lightship-php
网页性能/SEO/安全/无障碍分析,无浏览器。
0.9.0
2022-12-11 10:39 UTC
Requires
- php: >=8.1.0
- ext-curl: *
- guzzlehttp/guzzle: ^7.5.0
- webmozart/assert: 1.*
Requires (Dev)
- fakerphp/faker: 1.20.0
- friendsofphp/php-cs-fixer: 3.13.0
- pestphp/pest: 1.22.3
- phpstan/phpstan: 1.9.2
- thibautselingue/local-php-security-checker-installer: 1.0.3
- dev-master
- 0.9.0
- 0.8.0
- 0.7.1
- 0.7.0
- 0.6.0
- 0.5.0
- 0.4.0
- 0.3.0
- 0.1.0
- dev-13-add-rector
- dev-16-fix-php-stan-xdebug-warning
- dev-15-add-composer-command-to-run-all-tests-at-once
- dev-10-use-composer-outdated-instead-of-symfony-checker
- dev-9-support-for-php-8-2
- dev-6-to-be-able-to-assert-if-a-page-has-passed-certain-or-all-rules
- dev-4-accessibility-total-is-not-100
This package is auto-updated.
Last update: 2024-09-16 01:53:03 UTC
README
网页性能/SEO/安全/无障碍分析,无浏览器。
[
{
"url": "https:\/\/example.com\/",
"durationInSeconds": 0.15,
"scores": {
"seo": 100,
"security": 75
},
"seo": [
{
"name": "titlePresent",
"passes": true
},
{
"name": "langPresent",
"passes": true
}
],
"security": [
{
"name": "xFrameOptionsPresent",
"passes": true
},
{
"name": "strictTransportSecurityHeaderPresent",
"passes": true
},
{
"name": "serverHeaderHidden",
"passes": false
},
{
"name": "xPoweredByHidden",
"passes": true
}
]
}
]
摘要
关于
我制作了一个Web应用,并希望能够频繁测试我的公共页面响应各种标准以优化它们的引用。
由于我的Web应用完全基于服务器端,所有测试都非常快,且不依赖于浏览器。我希望保持这一点,并拥有一个快速的工具体现,例如不遗漏图像的alt属性,确保我的描述和标题已填写,或评估页面加载时间的近似值。
示例
1. 简单的示例代码驱动
在这个示例中,我们将使用代码配置我们的网页。
require __DIR__ . "/vendor/autoload.php"; use Lightship\Lightship; $lightship = new Lightship(); $lightship->domain("https://news.google.com") ->route("/foryou") ->route("/topstories") ->route("/my/searches", ["hl" => "fr", "gl" => "FR"]); ->analyse(); file_put_contents("report.json", $lightship->toPrettyJson());
2. 使用配置文件的简单示例
在这个示例中,我们将告诉Lightship使用我们的"lightship.json"文件而不是在代码中配置。
require __DIR__ . "/vendor/autoload.php"; use Lightship\Lightship; $lightship = new Lightship(); $lightship->config(__DIR__ . "/lightship.json"); ->analyse(); file_put_contents("report.json", $lightship->toPrettyJson());
这里是我们的配置文件。
{
"domains": [
{
"base": "https://news.google.com",
"routes": [
{
"path": "/foryou"
},
{
"path": "/topstories"
},
{
"path": "/my/searches",
"queries": [
{
"key": "hl",
"value": "fr"
},
{
"key": "gl",
"value": "FR"
}
]
}
]
}
]
}
3. 设置响应回调
在这个示例中,我们将在一个响应被解析并生成报告后触发我们的函数。这对于CLI程序很有用。
use Lightship\Lightship; require __DIR__ . "/vendor/autoload.php"; $lightship = new Lightship(); $lightship->route("https://example.com") ->route("https://northerwind.com") ->onReportedRoute(function (Route $route, Report $report): void { echo "{$route->path()}: {$report->score(RuleType::Security)}" . PHP_EOL; foreach ($report->results as $result) { echo " {$result->name} {$result->passes}"; } }) ->analyse();
这会显示类似以下内容
https://example.com/: 38 https://northerwind.com/: 61
4. 断言URL通过了规则
在测试和CI中很有用,您可以断言您的URL通过了某些/所有规则。
use Lightship\Lightship; use Lightship\Rules\Seo\LangPresent; use Lightship\Rules\Performance\FastResponseTime; $lightship = new Lightship(); $lightship->route("https://example.com") ->route("https://google.com") ->analyse(); assert($lightship->rulePassed(["https://example.com"], LangPresent::class) === true); assert($lightship->allRulesPassed(["https://google.com"]) === true); assert($lightship->someRulesPassed(["https://google.com"], [LangPresent::class, FastResponseTime::class]) === true);
要求
安装
composer require lightship-core/lightship-php
兼容性表
此表仅显示此当前软件包版本与PHP版本的兼容性。
测试
composer run analyse
composer run test
composer run lint
composer run check
composer run scan
composer run updates
或
composer run all