Skip to content

Commit a5de462

Browse files
authored
Merge pull request #3 from Micro-PHP/v1.0.2-release
Uddate to v1.0.2
2 parents 659fbfc + d7e8d44 commit a5de462

14 files changed

+83
-55
lines changed

src/Business/Logger/Formatter/Format/AbstractFormat.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace Micro\Plugin\Http\Business\Logger\Formatter\Format;
1515

16-
use Micro\Plugin\Http\Business\Logger\Formatter\LogFormatterInterface;
1716
use Symfony\Component\HttpFoundation\Request;
1817
use Symfony\Component\HttpFoundation\Response;
1918

@@ -22,25 +21,24 @@
2221
*
2322
* @codeCoverageIgnore
2423
*/
25-
abstract class AbstractFormat implements LogFormatterInterface
24+
abstract class AbstractFormat implements LogFormatterConcreteInterface
2625
{
27-
public function __construct(
28-
private readonly string $formatString
29-
) {
30-
}
31-
32-
public function format(Request $request, Response|null $response, ?\Throwable $exception): string
26+
public function format(Request $request, Response|null $response, ?\Throwable $exception, ?string $message = null): string
3327
{
28+
if (!$message) {
29+
return '';
30+
}
31+
3432
$var = '{{'.$this->getVarName().'}}';
3533

36-
if (!str_contains($this->formatString, $var)) {
37-
return $this->formatString;
34+
if (!str_contains($message, $var)) {
35+
return $message;
3836
}
3937

4038
return str_ireplace(
4139
$var,
42-
$this->formatString,
43-
$this->getVarValue($request, $response, $exception)
40+
$this->getVarValue($request, $response, $exception),
41+
$message
4442
);
4543
}
4644

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <kost@micro-php.net>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Http\Business\Logger\Formatter\Format;
15+
16+
use Micro\Plugin\Http\Business\Logger\Formatter\LogFormatterInterface;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\Response;
19+
20+
/**
21+
* @author Stanislau Komar <head.trackingsoft@gmail.com>
22+
*/
23+
interface LogFormatterConcreteInterface extends LogFormatterInterface
24+
{
25+
public function format(Request $request, ?Response $response, ?\Throwable $exception, ?string $message = null): string;
26+
}

src/Business/Logger/Formatter/LogFormatter.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Micro\Plugin\Http\Business\Logger\Formatter;
1515

16+
use Micro\Plugin\Http\Business\Logger\Formatter\Format\LogFormatterConcreteInterface;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpFoundation\Response;
1819

@@ -22,18 +23,19 @@
2223
readonly class LogFormatter implements LogFormatterInterface
2324
{
2425
/**
25-
* @param iterable<LogFormatterInterface> $logFormatterCollection
26+
* @param iterable<LogFormatterConcreteInterface> $logFormatterCollection
2627
*/
2728
public function __construct(
28-
private iterable $logFormatterCollection
29+
private iterable $logFormatterCollection,
30+
private string $messageTemplate
2931
) {
3032
}
3133

3234
public function format(Request $request, Response|null $response, ?\Throwable $exception): string
3335
{
34-
$message = '';
36+
$message = $this->messageTemplate;
3537
foreach ($this->logFormatterCollection as $formatter) {
36-
$message = $formatter->format($request, $response, $exception);
38+
$message = $formatter->format($request, $response, $exception, $message);
3739
}
3840

3941
return $message;

src/Business/Logger/Formatter/LogFormatterFactory.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,17 @@ class LogFormatterFactory implements LogFormatterFactoryInterface
3030
public function create(string $format): LogFormatterInterface
3131
{
3232
return new LogFormatter(
33-
$this->createLogFormatters($format)
33+
[
34+
new HttpRefererFormat(),
35+
new IpFormat(),
36+
new MethodFormat(),
37+
new RequestBodyFormat(),
38+
new RequestFormat(),
39+
new StatusFormat(),
40+
new TimeFormat(),
41+
new UsernameFormat(),
42+
],
43+
$format
3444
);
3545
}
36-
37-
/**
38-
* @return iterable<LogFormatterInterface>
39-
*/
40-
protected function createLogFormatters(string $format): iterable
41-
{
42-
return [
43-
new HttpRefererFormat($format),
44-
new IpFormat($format),
45-
new MethodFormat($format),
46-
new RequestBodyFormat($format),
47-
new RequestFormat($format),
48-
new StatusFormat($format),
49-
new TimeFormat($format),
50-
new UsernameFormat($format),
51-
];
52-
}
5346
}

tests/Unit/Business/Logger/Formatter/Format/AbstractFormatTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Micro\Plugin\Http\Test\Unit\Business\Logger\Formatter\Format;
1515

16-
use Micro\Plugin\Http\Business\Logger\Formatter\LogFormatterInterface;
16+
use Micro\Plugin\Http\Business\Logger\Formatter\Format\LogFormatterConcreteInterface;
1717
use Micro\Plugin\Http\Exception\HttpException;
1818
use PHPUnit\Framework\TestCase;
1919
use Symfony\Component\HttpFoundation\Request;
@@ -36,11 +36,17 @@ public function testFormat(bool $hasResponse, \Throwable|null $throwable)
3636
$object->format(
3737
$this->createRequest(),
3838
$this->createResponse($hasResponse),
39-
$this->createThrowable($throwable)
39+
$this->createThrowable($throwable),
40+
$this->getFormattedVariable()
4041
)
4142
);
4243
}
4344

45+
protected function getAcceptedValue()
46+
{
47+
return 'hello - '.$this->getVariable();
48+
}
49+
4450
public function dataProvider()
4551
{
4652
return [
@@ -56,7 +62,7 @@ public function dataProvider()
5662
protected function assertResult(mixed $object, mixed $result)
5763
{
5864
$this->assertEquals(
59-
$this->getVariable(),
65+
$this->getAcceptedValue(),
6066
$result
6167
);
6268
}
@@ -82,16 +88,16 @@ protected function createRequest(): Request
8288

8389
abstract protected function getTestClass(): string;
8490

85-
protected function createTestObject(): LogFormatterInterface
91+
protected function createTestObject(): LogFormatterConcreteInterface
8692
{
8793
$testClass = $this->getTestClass();
8894

89-
return new $testClass($this->getFormattedVariable());
95+
return new $testClass();
9096
}
9197

9298
protected function getFormattedVariable()
9399
{
94-
return '{{'.$this->getVariable().'}}';
100+
return 'hello - {{'.$this->getVariable().'}}';
95101
}
96102

97103
abstract public function getVariable(): string;

tests/Unit/Business/Logger/Formatter/Format/HttpRefererFormatTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ public function getVariable(): string
2727

2828
public function assertResult(mixed $object, mixed $result)
2929
{
30-
$this->assertEquals('', $result);
30+
$this->assertEquals('hello - ', $result);
3131
}
3232
}

tests/Unit/Business/Logger/Formatter/Format/IpFormatTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ public function testFormat(bool $hasResponse, \Throwable|null $throwable, $hasIp
2525
$object = $this->createTestObject();
2626

2727
$this->assertEquals(
28-
'127.0.0.1',
28+
'hello - '.($hasIp ? '127.0.0.1' : ''),
2929
$object->format(
3030
$this->createRequest($hasIp),
3131
$this->createResponse($hasResponse),
32-
$this->createThrowable($throwable)
32+
$this->createThrowable($throwable),
33+
$this->getFormattedVariable()
3334
)
3435
);
3536
}
@@ -59,14 +60,10 @@ public function dataProvider()
5960
protected function createRequest(bool $hasIp = null): Request
6061
{
6162
if (!$hasIp) {
62-
return parent::createRequest();
63+
return new Request();
6364
}
6465

6566
return parent::createRequest();
66-
67-
$request = new Request([], [], [], [], [], []);
68-
69-
return $request;
7067
}
7168

7269
protected function getTestClass(): string

tests/Unit/Business/Logger/Formatter/Format/MethodFormatTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public function getVariable(): string
2525
return 'request_method';
2626
}
2727

28-
public function assertResult(mixed $object, mixed $result)
28+
protected function getAcceptedValue()
2929
{
30-
$this->assertEquals('GET', $result);
30+
return 'hello - GET';
3131
}
3232
}

tests/Unit/Business/Logger/Formatter/Format/RequestBodyFormatTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public function testFormat(bool $hasResponse, \Throwable|null $throwable, string
2727
$object->format(
2828
$this->createRequest($contentType),
2929
$this->createResponse($hasResponse),
30-
$this->createThrowable($throwable)
30+
$this->createThrowable($throwable),
31+
$this->getFormattedVariable()
3132
)
3233
);
3334
}

tests/Unit/Business/Logger/Formatter/Format/RequestFormatTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ public function getVariable(): string
2727

2828
public function assertResult(mixed $object, mixed $result)
2929
{
30-
$this->assertEquals('/test', $result);
30+
$this->assertEquals('hello - /test', $result);
3131
}
3232
}

0 commit comments

Comments
 (0)