Skip to content

Commit

Permalink
Merge pull request #3 from php-middleware/v3
Browse files Browse the repository at this point in the history
V3
  • Loading branch information
snapshotpl authored Apr 29, 2017
2 parents 43198c0 + 8baad8b commit 68e946e
Show file tree
Hide file tree
Showing 18 changed files with 301 additions and 216 deletions.
15 changes: 10 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
language: php

php:
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm

env:
- COMPOSER_FLAGS=--prefer-lowest
- COMPOSER_FLAGS=

before_script:

before_install:
- phpenv config-rm xdebug.ini

install:
- composer update --no-interaction --no-suggest --prefer-dist $COMPOSER_FLAGS

script:
- vendor/bin/phpunit

cache:
directories:
- $HOME/.composer/cache
- vendor
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ Support double and single (PSR-15) pass middleware.
composer require php-middleware/log-http-messages
```

To log http messages you need pass into `LogRequestMiddleware` implementation of `PhpMiddleware\LogHttpMessages\Formatter\HttpMessagesFormatter`,
instance `Psr\Log\LoggerInterface` and add middleware to your middleware runner.
Third parameter is log level and it's optional (`Psr\Log\LogLevel::INFO` as default).
To log http messages you need pass into `LogRequestMiddleware` implementation of
`PhpMiddleware\LogHttpMessages\Formatter\ServerRequestFormatter`,
`PhpMiddleware\LogHttpMessages\Formatter\ResponseFormatter`,
instance `Psr\Log\LoggerInterface` and add this middleware to your middleware runner.
You can also set log level (`Psr\Log\LogLevel::INFO` as default) and log message (`Request/Response` as default).

There are tree implementation of `PhpMiddleware\LogHttpMessages\Formatter\HttpMessagesFormatter`:
Provided implementation of formatters:

* `PhpMiddleware\LogHttpMessages\Formatter\RequestFormatter` to log request message,
* `PhpMiddleware\LogHttpMessages\Formatter\ResponseFormatter` to log response message,
* `PhpMiddleware\LogHttpMessages\Formatter\BothFormatter` to log request and response message.
* `PhpMiddleware\LogHttpMessages\Formatter\EmptyMessageFormatter`,
* `PhpMiddleware\LogHttpMessages\Formatter\ZendDiactorosToArrayMessageFormatter`,
* `PhpMiddleware\LogHttpMessages\Formatter\ZendDiactorosToStringMessageFormatter`.

```php
$requestFormatter = PhpMiddleware\LogHttpMessages\Formatter\RequestFormatter();
$responseFormatter = PhpMiddleware\LogHttpMessages\Formatter\ResponseFormatter();
$formatter = new PhpMiddleware\LogHttpMessages\Formatter\BothFormatter(requestFormatter, responseFormatter);
$logMiddleware = new PhpMiddleware\LogHttpMessages\LogMiddleware(formatter, $logger);
$formatter = PhpMiddleware\LogHttpMessages\Formatter\ZendDiactorosToArrayMessageFormatter();
$logMiddleware = new PhpMiddleware\LogHttpMessages\LogMiddleware($formatter, $formatter, $logger);

$app = new MiddlewareRunner();
$app->add($logMiddleware);
Expand All @@ -41,4 +41,4 @@ Middleware should works with:
* [Slim 3.x](https://github.com/slimphp/Slim)
* [zend-log 2.6](https://github.com/zendframework/zend-log)

And any other modern framework [supported middlewares and PSR-7](https://mwop.net/blog/2015-01-08-on-http-middleware-and-psr-7.html) and [PSR-3 implementation](http://www.php-fig.org/psr/psr-3/) logger.
And any other modern framework [supported PSR-15 middlewares and PSR-7](https://mwop.net/blog/2015-01-08-on-http-middleware-and-psr-7.html) and [PSR-3 implementation](http://www.php-fig.org/psr/psr-3/) logger.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
"psr-3"
],
"require": {
"php": ">=5.5",
"psr/log": "^1.0.0",
"php": ">=7.0",
"psr/log": "^1.0",
"psr/http-message": "^1.0",
"zendframework/zend-diactoros": "^1.1.3",
"zendframework/zend-diactoros": "^1.4",
"http-interop/http-middleware": "^0.4.1"
},
"require-dev": {
"phpunit/phpunit": "^4.8"
"phpunit/phpunit": "^6.1"
},
"autoload": {
"psr-4": {
Expand Down
36 changes: 0 additions & 36 deletions src/Formatter/BothFormatter.php

This file was deleted.

24 changes: 24 additions & 0 deletions src/Formatter/EmptyMessageFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare (strict_types=1);

namespace PhpMiddleware\LogHttpMessages\Formatter;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
* @codeCoverageIgnore
*/
final class EmptyMessageFormatter implements ServerRequestFormatter, ResponseFormatter
{
public function formatServerRequest(ServerRequestInterface $request): FormattedMessage
{
return FormattedMessage::createEmpty();
}

public function formatResponse(ResponseInterface $response): FormattedMessage
{
return FormattedMessage::createEmpty();
}
}
39 changes: 39 additions & 0 deletions src/Formatter/FormattedMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare (strict_types=1);

namespace PhpMiddleware\LogHttpMessages\Formatter;

final class FormattedMessage
{
private $value;

public static function fromString(string $value) : self
{
$instance = new self();
$instance->value = $value;

return $instance;
}

public static function fromArray(array $value) : self
{
$instance = new self();
$instance->value = $value;

return $instance;
}

public static function createEmpty() : self
{
return new self();
}

/**
* @return array|string|null
*/
public function getValue()
{
return $this->value;
}
}
17 changes: 0 additions & 17 deletions src/Formatter/HttpMessagesFormatter.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/Formatter/RequestFormatter.php

This file was deleted.

11 changes: 4 additions & 7 deletions src/Formatter/ResponseFormatter.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php

declare (strict_types=1);

namespace PhpMiddleware\LogHttpMessages\Formatter;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\Serializer;

final class ResponseFormatter implements HttpMessagesFormatter
interface ResponseFormatter
{
public function format(ServerRequestInterface $request, ResponseInterface $response)
{
return Serializer::toString($response);
}
public function formatResponse(ResponseInterface $response) : FormattedMessage;
}
12 changes: 12 additions & 0 deletions src/Formatter/ServerRequestFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare (strict_types=1);

namespace PhpMiddleware\LogHttpMessages\Formatter;

use Psr\Http\Message\ServerRequestInterface;

interface ServerRequestFormatter
{
public function formatServerRequest(ServerRequestInterface $request) : FormattedMessage;
}
28 changes: 28 additions & 0 deletions src/Formatter/ZendDiactorosToArrayMessageFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare (strict_types=1);

namespace PhpMiddleware\LogHttpMessages\Formatter;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\ArraySerializer as ResponseSerializer;
use Zend\Diactoros\Request\ArraySerializer as RequestSerializer;

final class ZendDiactorosToArrayMessageFormatter implements ServerRequestFormatter, ResponseFormatter
{
public function formatResponse(ResponseInterface $response): FormattedMessage
{
$array = ResponseSerializer::toArray($response);

return FormattedMessage::fromArray($array);
}

public function formatServerRequest(ServerRequestInterface $request): FormattedMessage
{
$array = RequestSerializer::toArray($request);

return FormattedMessage::fromArray($array);
}

}
27 changes: 27 additions & 0 deletions src/Formatter/ZendDiactorosToStringMessageFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare (strict_types=1);

namespace PhpMiddleware\LogHttpMessages\Formatter;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\Serializer as ResponseSerializer;
use Zend\Diactoros\Request\Serializer as RequestSerializer;

final class ZendDiactorosToStringMessageFormatter implements ServerRequestFormatter, ResponseFormatter
{
public function formatResponse(ResponseInterface $response): FormattedMessage
{
$string = ResponseSerializer::toString($response);

return FormattedMessage::fromString($string);
}

public function formatServerRequest(ServerRequestInterface $request): FormattedMessage
{
$string = RequestSerializer::toString($request);

return FormattedMessage::fromString($string);
}
}
Loading

0 comments on commit 68e946e

Please sign in to comment.