aedart / athenaeum-etags
为您的Laravel应用程序提供ETags工具和Http条件请求评估
8.10.0
2024-09-23 07:54 UTC
Requires
- php: ^8.2
- aedart/athenaeum-contracts: ^8.10
- aedart/athenaeum-streams: ^8.10
- aedart/athenaeum-support: ^8.10
- aedart/athenaeum-utils: ^8.10
- illuminate/http: ^v11.23.5
- ramsey/http-range: ^1.1.0
- dev-main
- 8.10.0
- 8.9.0
- 8.8.0
- 8.7.0
- 8.6.0
- 8.5.0
- 8.4.0
- 8.3.0
- 8.2.0
- 8.1.0
- 8.0.0
- 7.33.0
- 7.32.0
- 7.31.0
- 7.30.1
- 7.30.0
- 7.29.0
- 7.28.0
- 7.27.0
- 7.26.0
- 7.25.0
- 7.24.0
- 7.23.0
- 7.22.1
- 7.22.0
- 7.21.0
- 7.20.0
- 7.19.0
- 7.18.1
- 7.18.0
- 7.17.0
- 7.16.0
- 7.15.0
- 7.14.0
- 7.13.0
- 7.12.0
- 7.11.3
- 7.11.2
- 7.11.1
- 7.11.0
- 7.10.1
- 7.10.0
- 7.9.1
- 7.9.0
- 7.8.0
- 7.7.2
- 7.7.1
- 7.7.0
- 7.6.0
- 7.5.0
- 7.4.0
- 7.3.0
- 7.2.0
- 7.1.0
- 7.0.1
- 7.0.0
- 7.0.0-alpha.1
- 6.8.1
- 6.8.0
- 6.7.0
- 6.6.0
This package is auto-updated.
Last update: 2024-09-23 07:55:53 UTC
README
此包提供了一种基于"配置文件"的方法来生成ETags,并为处理Http条件请求提供了一个评估器,适用于您的Laravel应用程序。
ETags示例
生成
use Aedart\ETags\Facades\Generator; // Generate an ETag for strong comparison, of content $etag = Generator::makeStrong($content); echo (string) $etag; // "4720b076892bb2fb65e75af902273c73a2967e4a"
或者生成标记为"弱"的ETags(用于弱比较)
$etag = Generator::makeWeak($content); echo (string) $etag; // W/"0815"
解析
要解析Http头中的ETags,您可以使用parse()
方法。它返回一个包含ETag
实例的集合。
// E.g. If-None-Match: W/"0815", W/"0816", W/"0817" $collection = Generator::parse($request->header('If-None-Match')); foreach ($collection as $etag) { echo (string) $etag; }
比较
ETags也可以根据RFC9110进行相互匹配。
使用集合
// Etags from Http Header $collection = Generator::parse($request->header('If-Match')); // E.g. 'W/"0815"' // Other Etag for your resource $etag = Generator::makeWeak($content); // E.g. W/"0815" // Compare etags against resource's etag echo $collection->contains($etag, true); // false - strong comparison echo $collection->contains($etag); // true - weak comparison
使用ETag实例
您还可以使用matches()
方法比较单个ETag
实例。
$etagA = Generator::parseSingle('W/"0815"'); $etagB = Generator::parseSingle('W/"0815"'); echo $etagA->matches($etagB, true); // false - strong comparison echo $etagA->matches($etagB); // true - weak comparison
Http预处理条件示例
Evaluator
组件能够处理传入请求与所有定义的RFC9110预处理条件,按照指定的评估优先级。根据请求的预处理条件,如果通过或失败,请求可以继续进行或通过可定制的Http异常中止。您的Laravel应用程序应在请求中止时完成剩余操作。
use Aedart\ETags\Preconditions\Evaluator; use Aedart\ETags\Preconditions\Resources\GenericResource; // Process If-Match, If-None-Match, If-Modified-Since... etc // Depending on condition's pass/fail, the request can be aborted via // an appropriate Http Exception, or proceed to your logic... $resource = Evaluator::make($request) ->evaluate(new GenericResource( data: $model, etag: $model->getStrongEtag(), lastModifiedDate: $model->updated_at ));
总结如下,以下预处理条件受支持
Evaluator
还支持添加您自己的自定义预处理条件进行评估,如果您需要的话。
官方文档
请阅读官方文档以获取更多信息。
单一代码库位于github.com/aedart/athenaeum
版本控制
此包遵循语义版本控制2.0.0
许可证
BSD-3-Clause,请阅读包含在此包中的LICENSE文件