Skip to content

Commit

Permalink
Merge pull request #3 from kodus/1.0.0
Browse files Browse the repository at this point in the history
add PSR-15 middleware and test: upgrades minimum PHP version to 7
  • Loading branch information
mindplay-dk authored Aug 28, 2018
2 parents f7bf2bb + 767253c commit 0fd4d00
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2

before_script:
- 'composer install --prefer-source'
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
kodus/chrome-logger
===================

[![PHP Version](https://img.shields.io/badge/php-5.6%2B-blue.svg)](https://packagist.org/packages/kodus/chrome-logger)
[![PHP Version](https://img.shields.io/badge/php-7.0%2B-blue.svg)](https://packagist.org/packages/kodus/chrome-logger)
[![Build Status](https://travis-ci.org/kodus/chrome-logger.svg?branch=master)](https://travis-ci.org/kodus/chrome-logger)
[![Code Coverage](https://scrutinizer-ci.com/g/kodus/chrome-logger/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/kodus/chrome-logger/?branch=master)

[PSR-3](http://www.php-fig.org/psr/psr-3/) and [PSR-7](http://www.php-fig.org/psr/psr-7/) compliant alternative
to the original [ChromeLogger](https://craig.is/writing/chrome-logger) for PHP by Craig Campbell. [Because](#because).
Alternative to the original [ChromeLogger](https://craig.is/writing/chrome-logger) for PHP by Craig Campbell, using:

* [PSR-3](http://www.php-fig.org/psr/psr-3/) compliant interface for logging,
* [PSR-7](http://www.php-fig.org/psr/psr-7/) HTTP message abstraction for the models, and
* [PSR-15](https://www.php-fig.org/psr/psr-15/) compliant middleware for quick integration.


## Usage

It's PSR-3, so:
The logging interface is PSR-3 compliant, so:

```php
$logger = new ChromeLogger();

$logger->notice("awesome sauce!");
```

Assuming you have a PSR-7 `ResponseInterface` instance, such as in a middleware stack, you can populate
Using a PSR-7 compliant `ResponseInterface` instance, such as in a middleware stack, you can populate
the Response as follows:

```php
$response = $logger->writeToResponse($response);
```

Or just add an instance of the included PSR-15 `ChromeLoggerMiddleware` to the top of your middleware stack.

If you're not using PSR-7, emitting the headers old-school is also possible with `ChromeLogger::emitHeader()`.

### Logging Table Data
Expand Down
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kodus/chrome-logger",
"type": "library",
"description": "PSR-3 and PSR-7 compliant alternative to the original ChromeLogger for PHP",
"description": "PSR-3 (logging), PSR-7 (HTTP) and PSR-15 (middleware) compliant alternative to the original ChromeLogger for PHP",
"license": "MIT",
"authors": [
{
Expand All @@ -16,13 +16,16 @@
}
],
"require": {
"php": ">=5.6",
"php": ">=7.0",
"psr/log": "^1",
"psr/http-message": "^1"
"psr/http-message": "^1",
"psr/http-server-middleware": "^1"
},
"require-dev": {
"codeception/codeception": "^2",
"mockery/mockery": "^0.9"
"mockery/mockery": "^0.9",
"zendframework/zend-diactoros": "^1.8.5",
"mindplay/middleman": "^3.0.3"
},
"autoload": {
"psr-4": {
Expand Down
44 changes: 44 additions & 0 deletions src/ChromeLoggerMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Kodus\Logging;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
* Add this to the *top* of your middleware stack - it will delegate to the rest of the
* middleware stack unconditionally, then decorates the Response with ChromeLogger headers.
*/
class ChromeLoggerMiddleware implements MiddlewareInterface
{
/**
* @var ChromeLogger
*/
private $logger;

/**
* @param ChromeLogger $logger
*/
public function __construct(ChromeLogger $logger)
{
$this->logger = $logger;
}

/**
* Process an incoming server request and return a response, optionally delegating
* response creation to a handler.
*
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);

return $this->logger->writeToResponse($response);
}
}
59 changes: 59 additions & 0 deletions tests/unit/ChromeLoggerMiddlewareCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Kodus\Logging\Test\Unit;

use Kodus\Logging\ChromeLogger;
use Kodus\Logging\ChromeLoggerMiddleware;
use mindplay\middleman\Dispatcher;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use UnitTester;
use Zend\Diactoros\Response\TextResponse;
use Zend\Diactoros\ServerRequest;

class MockMiddleware implements MiddlewareInterface
{
/**
* @var ChromeLogger
*/
private $logger;

public function __construct(ChromeLogger $logger)
{
$this->logger = $logger;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->logger->notice("running mock middleware");

return new TextResponse("Hello");
}
}

class ChromeLoggerMiddlewareCest
{
public function logDuringMiddlewareDispatch(UnitTester $I)
{
$logger = new ChromeLogger();

$logger_middleware = new ChromeLoggerMiddleware($logger);

$mock_middleware = new MockMiddleware($logger);

$dispatcher = new Dispatcher([
$logger_middleware,
$mock_middleware
]);

$response = $dispatcher->dispatch(new ServerRequest());

$header = $response->getHeaderLine(ChromeLogger::HEADER_NAME);

$data = json_decode(base64_decode($header), true);

$I->assertSame([[["running mock middleware"], "info"]], $data["rows"]);
}
}

0 comments on commit 0fd4d00

Please sign in to comment.