pachico/slim-correlationid

Resolves and propagates correlation id

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

This package is auto-updated.

Last update: 2024-10-22 04:31:37 UTC


README

Build Status codecov Scrutinizer Code Quality

Resolves and propagates correlation ids. If not found, it creates one. If callable is provided, it will invoke it as soon as it resolves/creates it. Especially useful for microservices platforms.

It has been designed with Slim framework in mind but it works with any double pass middleware signature:

fn(request, response, next): response

Refer to Slim documentation for details about middlewares.

Requirements

This library currently supports PHP >= 5.4.

Install

Via Composer

$ composer require pachico/slim-correlationid

Usage

Simple

<?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());

With custom header key

<?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'
]));

Note: default key is Pachico\SlimCorrelationId\Model\CorrelationId::DEFAULT_HEADER_KEY

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

With custom callable

<?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));

With custom id generator

<?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()));

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email pachicodev@gmail.com instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.