Skip to content

Commit

Permalink
Release 2.0
Browse files Browse the repository at this point in the history
- Added RepositoryInterface to implement different log saving options.
- Added SyncRepository to save as in version 1.0
- Added QueueRepository to save through a queue to bypass
the database transaction problem repository implementations
- Added the repository property in Bootstrap to configure the repository.
- Static create methods from Request, Response, Exception replaced with Factory class
- Add docker-compose.yml to .gitattributes export-ignore
  • Loading branch information
Horat1us committed Jul 7, 2023
1 parent 2036503 commit 97adb14
Show file tree
Hide file tree
Showing 20 changed files with 574 additions and 84 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
/.github export-ignore
/phpunit.pgsql.xml export-ignore
/composer.lock export-ignore
/CHANGELOG.md export-ignore
/docker-compose.yml export-ignore
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Yii2 Guzzle Changelog

## Release 2.0
- Added RepositoryInterface to implement different log saving options.
- Added [SyncRepository](./src/Log/SyncRepository.php) to save as in version 1.0
- Added [QueueRepository](./src/Log/QueueRepository.php) to save through a queue to bypass
the database transaction problem repository implementations
- Added the repository property in [Bootstrap](./src/Bootstrap.php) to configure the repository.
- Static create methods from Request, Response, Exception replaced with [Factory](./src/Log/Factory.php) class
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Library for storing http queries into database

## Installation
```bash
composer require wearesho-team/yii2-guzzle
composer require wearesho-team/yii2-guzzle:^2.0
```

## Usage
Expand Down Expand Up @@ -44,6 +44,48 @@ return [
Note: for not UTF-8 request or response body (for example, files)
`(invalid UTF-8 bytes)` will be saved.

### Log using Queue
Sometimes it is not possible to use synchronous saving of query logs
when the query was executed during an initiated database transaction.
In this case it is possible that the transaction will not be saved
and the logs will be lost.

The recommended solution is to use [QueueRepository](./src/Log/QueueRepository.php)
to save the logs. To do this, configure Bootstrap this way:

```php
<?php

use Wearesho\Yii\Guzzle;
use Psr\Http\Message\RequestInterface;

return [
'bootstrap' => [
'http-log' => [
'class' => Guzzle\Bootstrap::class,
// Here you configure repository
'repository' => [
'class' => Guzzle\Log\QueueRepository::class,
],
// Logs exclude rules
'exclude' => [
// your rules
],
// Guzzle client configuration settings
'config' => [
'timeout' => 10,
],
],
],
];
```

Also, you have the option to implement your own
[RepositoryInterface](./src/Log/RepositoryInterface.php) and use it.

## TODO
- Tests for [Job](./src/Log/Job.php)

## Contributors
- [Alexander <horat1us> Letnikow](mailto:[email protected])
- [Roman <KartaviK> Varkuta](mailto:[email protected])
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"horat1us/yii2-carbon-behavior": "^1.2",
"horat1us/yii2-migration-bootstrap": "^1.3",
"horat1us/yii2-validation-exception": "^1.1",
"yiisoft/yii2": "^2.0.47"
"yiisoft/yii2": "^2.0.47",
"yiisoft/yii2-queue": "^2.3"
},
"require-dev": {
"ext-json": "*",
Expand Down
170 changes: 169 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Wearesho\Yii\Guzzle;

use GuzzleHttp;
use Psr\Http\Message;
use Wearesho\Yii\Guzzle\Log;
use yii\base;
use yii\console;
use GuzzleHttp;
use yii\base;
use yii\di;

/**
* Class Bootstrap
Expand Down Expand Up @@ -36,6 +36,19 @@ class Bootstrap extends base\BaseObject implements base\BootstrapInterface
*/
public array $config = [];

/**
* Repository configuration
*
* @see Log\RepositoryInterface
* @see Log\SyncRepository
* @see Log\QueueRepository
*
* @var array
*/
public array $repository = [
'class' => Log\SyncRepository::class,
];

/**
* @param base\Application $app
*
Expand All @@ -53,7 +66,11 @@ public function bootstrap($app)
}
}

$middleware = new Log\Middleware($this->exclude);
/** @var Log\RepositoryInterface $repository */
$repository = di\Instance::ensure($this->repository, Log\RepositoryInterface::class);
\Yii::$container->setSingleton(Log\RepositoryInterface::class, $repository);

$middleware = new Log\Middleware($repository, $this->exclude);
\Yii::$container->setSingleton(Log\Middleware::class, fn() => $middleware);

$handlerStack = GuzzleHttp\HandlerStack::create();
Expand Down
2 changes: 1 addition & 1 deletion src/Log/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @property string $type
* @property array $trace [jsonb] Exception Trace Format
* @property int $created_at [timestamp(0)]
* @property-read Request $request
* @property Request $request
*/
class Exception extends db\ActiveRecord
{
Expand Down
59 changes: 59 additions & 0 deletions src/Log/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Wearesho\Yii\Guzzle\Log;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\RequestInterface;

class Factory
{
public const NOT_UTF_8_BODY = "(invalid UTF-8 bytes)";

public function fromRequest(RequestInterface $request): array
{
$body = (string)$request->getBody();
if (!mb_check_encoding($body, 'UTF-8')) {
$body = static::NOT_UTF_8_BODY;
}

return [
'method' => $request->getMethod(),
'uri' => (string)$request->getUri(),
'headers' => $request->getHeaders(),
'body' => $body,
];
}

public function fromResponse(ResponseInterface $response): array
{
$body = (string)$response->getBody();
if (!mb_check_encoding($body, "UTF-8")) {
$body = static::NOT_UTF_8_BODY;
}

return [
'status' => $response->getStatusCode(),
'headers' => $response->getHeaders(),
'body' => $body,
];
}

public function fromException(\Throwable $exception): array
{
$trace = array_map(
fn(array $data): array => array_intersect_key($data, array_flip([
'file',
'line',
'function',
'class',
])),
$exception->getTrace()
);
return [
'type' => get_class($exception),
'trace' => $trace,
];
}
}
Loading

0 comments on commit 97adb14

Please sign in to comment.