packico/slim-correlationid

解析和传播关联ID

v0.1.0 2017-01-21 15:53 UTC

This package is auto-updated.

Last update: 2024-09-22 04:24:31 UTC


README

Build Status codecov Scrutinizer Code Quality

解析和传播关联ID。如果未找到,则创建一个。如果提供了可调用的函数,则它将在解析/创建后立即调用它。特别适用于微服务平台。

它专为Slim框架设计,但与任何双遍历中间件签名兼容

fn(request, response, next): response

有关中间件的详细信息,请参阅Slim文档

要求

此库目前支持 PHP >= 5.4

安装

通过Composer

$ composer require pachico/slim-correlationid

用法

简单

<?php
/**
 * This example will just resolve the current correlation id from the request header.
 * If not present, then it will create one and append it to the request and the response.
 */
use \Pachico\SlimCorrelationId\Middleware;

$app = new \Slim\App();
$app->add(new Middleware\CorrelationId());

使用自定义头部键

<?php

/**
 * It is also possible to set which key in the request haader it will try to resolve it from.
 */
use \Pachico\SlimCorrelationId\Middleware;

$app = new \Slim\App();
$app->add(new Middleware\CorrelationId([
    'header_key' => 'X-CustomCorrelation-Id'
]));

注意:默认键为 Pachico\SlimCorrelationId\Model\CorrelationId::DEFAULT_HEADER_KEY

echo \Pachico\SlimCorrelationId\Model\CorrelationId::DEFAULT_HEADER_KEY;
// X-Correlation-Id

使用自定义可调用

<?php

/**
 * In this case, we pass a callable that will be executed right after the correlation id
 * is resolved.
 * It might be useful for registering it to dependency containers, or instantiate objects
 * with the id (loggers, http clients, etc)
 */
use \Pachico\SlimCorrelationId\Middleware;
use \Pachico\SlimCorrelationId\Model;

$app = new \Slim\App();
$dummyObject = (object) [
        'correlationIdObject' => null
];
$customCallable = function (Model\CorrelationId $correlationid) use ($dummyObject) {
    $dummyObject->correlationIdObject = $correlationid;
};

$app->add(new Middleware\CorrelationId([], $customCallable));

使用自定义ID生成器

<?php

/**
 * How ids are generated can also be customized by injecting a custom Id generator, 
 * as long as it implements the IdGenerator interface.
 */
use \Pachico\SlimCorrelationId\Middleware;

$app = new \Slim\App();

$app->add(new Middleware\CorrelationId([], null, new MyCustomIdGenerator()));

变更日志

请参阅CHANGELOG,了解最近的变化。

测试

$ composer test

贡献

请参阅CONTRIBUTINGCONDUCT,了解详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件pachicodev@gmail.com而不是使用问题跟踪器。

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件