Skip to content

Commit 1f9d651

Browse files
committed
v1.0 initial commit
0 parents  commit 1f9d651

26 files changed

+1031
-0
lines changed

.gitattributes

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/.github export-ignore
2+
/tests export-ignore
3+
/phpunit.xml.dist export-ignore
4+
/.gitattributes export-ignore
5+
/.gitignore export-ignore
6+
/.php-cs-fixer.dist.php export-ignore
7+
/psalm.xml export-ignore
8+
9+
*.php diff=php

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
vendor
2+
composer.lock
3+
.phpunit.result.cache
4+
.php-cs-fixer.cache
5+
test-coverage-report
6+
phpunit.xml
7+
.php-cs-fixer.php
8+
phpstan.neon

.php-cs-fixer.dist.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
if (!file_exists(__DIR__.'/src')) {
4+
exit(0);
5+
}
6+
7+
$finder = (new PhpCsFixer\Finder())
8+
->in(__DIR__.'/src')
9+
->in(__DIR__.'/tests')
10+
;
11+
12+
return (new PhpCsFixer\Config())
13+
->setRules(array(
14+
'@Symfony' => true,
15+
'@Symfony:risky' => true,
16+
'protected_to_private' => false,
17+
'semicolon_after_instruction' => false,
18+
'header_comment' => [
19+
'header' => <<<EOF
20+
This file is part of the Micro framework package.
21+
22+
(c) Stanislau Komar <kost@micro-php.net>
23+
24+
For the full copyright and license information, please view the LICENSE
25+
file that was distributed with this source code.
26+
EOF
27+
]
28+
))
29+
->setRiskyAllowed(true)
30+
->setFinder($finder);

composer.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "micro/plugin-http-logger",
3+
"description": "Micro Framework: ROuter actions logger",
4+
"type": "micro-plugin",
5+
"require": {
6+
"micro/http-core": "^1",
7+
"micro/plugin-logger-core": "^1"
8+
},
9+
"require-dev": {
10+
"ergebnis/composer-normalize": "^2.29",
11+
"friendsofphp/php-cs-fixer": "^3.13",
12+
"phpstan/phpstan": "^1.9",
13+
"phpunit/php-code-coverage": "^9.2",
14+
"phpunit/phpunit": "^9.5",
15+
"vimeo/psalm": "^5.2"
16+
},
17+
"license": "MIT",
18+
"autoload": {
19+
"psr-4": {
20+
"Micro\\Plugin\\Http\\": "src/"
21+
}
22+
},
23+
"config": {
24+
"allow-plugins": {
25+
"ergebnis/composer-normalize": true
26+
},
27+
"sort-packages": true
28+
},
29+
"authors": [
30+
{
31+
"name": "Stanislau Komar",
32+
"email": "kost@micro-php.net"
33+
}
34+
],
35+
"scripts": {
36+
"coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-text",
37+
"php-cs-fix": "./vendor/bin/php-cs-fixer fix --verbose --using-cache=no",
38+
"php-cs-try": "./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no",
39+
"phpstan": "./vendor/bin/phpstan analyze --no-progress",
40+
"phpunit": "./vendor/bin/phpunit",
41+
"psalm": "./vendor/bin/psalm --no-progress --show-info=true",
42+
"statics": [
43+
"@phpstan",
44+
"@psalm",
45+
"@php-cs-try"
46+
],
47+
"test": [
48+
"@statics",
49+
"composer validate --strict",
50+
"composer normalize",
51+
"@coverage"
52+
]
53+
}
54+
}

phpstan.neon.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: 7
3+
paths:
4+
- src

phpunit.xml.dist

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- https://phpunit.readthedocs.io/en/9.5/configuration.html#the-phpunit-element -->
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
5+
backupGlobals="false"
6+
bootstrap="vendor/autoload.php"
7+
colors="true"
8+
failOnRisky="true"
9+
failOnWarning="true"
10+
>
11+
<php>
12+
<ini name="error_reporting" value="-1" force="true"/>
13+
</php>
14+
<testsuites>
15+
<testsuite name="Unit tests">
16+
<directory>tests/Unit</directory>
17+
</testsuite>
18+
</testsuites>
19+
<filter>
20+
<whitelist>
21+
<directory suffix=".php">src/</directory>
22+
<exclude>
23+
<directory>src/Exception</directory>
24+
<file>src/HttpCorePlugin.php</file>
25+
</exclude>
26+
</whitelist>
27+
</filter>
28+
<coverage>
29+
<include>
30+
<directory suffix=".php">src</directory>
31+
</include>
32+
<report>
33+
<html outputDirectory="test-coverage-report/" />
34+
</report>
35+
</coverage>
36+
</phpunit>

psalm.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="2"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
>
9+
<projectFiles>
10+
<directory name="src" />
11+
<ignoreFiles>
12+
<directory name="vendor" />
13+
</ignoreFiles>
14+
15+
</projectFiles>
16+
17+
<issueHandlers>
18+
19+
<UnnecessaryVarAnnotation>
20+
<errorLevel type="suppress">
21+
<file name="src/HttpCorePlugin.php"/>
22+
</errorLevel>
23+
</UnnecessaryVarAnnotation>
24+
25+
<MissingConstructor>
26+
<errorLevel type="suppress">
27+
<file name="src/HttpCorePlugin.php"/>
28+
</errorLevel>
29+
</MissingConstructor>
30+
31+
<ImplementedReturnTypeMismatch>
32+
<errorLevel type="suppress">
33+
<file name="src/HttpCorePlugin.php"/>
34+
</errorLevel>
35+
</ImplementedReturnTypeMismatch>
36+
</issueHandlers>
37+
38+
</psalm>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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\Executor;
15+
16+
use Micro\Plugin\Http\Business\Formatter\LogFormatterInterface;
17+
use Micro\Plugin\Http\Exception\HttpException;
18+
use Psr\Log\LoggerInterface;
19+
use Symfony\Component\HttpFoundation\Request;
20+
use Symfony\Component\HttpFoundation\Response;
21+
22+
/**
23+
* @author Stanislau Komar <kost@micro-php.net>
24+
*/
25+
readonly class HttpExecutorLoggerAwareDecorator implements RouteExecutorInterface
26+
{
27+
public function __construct(
28+
private RouteExecutorInterface $decorated,
29+
private LoggerInterface $loggerAccess,
30+
private LoggerInterface $loggerError,
31+
private LogFormatterInterface $logAccessFormatter,
32+
private LogFormatterInterface $logErrorFormatter
33+
) {
34+
}
35+
36+
/**
37+
* {@inheritDoc}
38+
*/
39+
public function execute(Request $request, bool $flush = true): Response
40+
{
41+
$response = null;
42+
$exception = null;
43+
try {
44+
$response = $this->decorated->execute($request, $flush);
45+
} catch (HttpException $httpException) {
46+
$exception = $httpException;
47+
} catch (\Throwable $throwable) {
48+
$exception = $throwable;
49+
} finally {
50+
$this->loggerAccess->info(
51+
$this->logAccessFormatter->format($request, $response, $exception)
52+
);
53+
54+
if ($exception) {
55+
$this->loggerError->critical(
56+
$this->logErrorFormatter->format($request, $response, $exception)
57+
);
58+
59+
throw $exception;
60+
}
61+
62+
return $response;
63+
}
64+
}
65+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\Executor;
15+
16+
use Micro\Plugin\Http\Business\Formatter\LogFormatterFactoryInterface;
17+
use Micro\Plugin\Http\PluginHttpLoggerConfigurationInterface;
18+
use Micro\Plugin\Logger\LoggerFacadeInterface;
19+
20+
/**
21+
* @author Stanislau Komar <kost@micro-php.net>
22+
*/
23+
readonly class HttpExecutorLoggerAwareDecoratorFactory implements RouteExecutorFactoryInterface
24+
{
25+
public function __construct(
26+
private RouteExecutorInterface $decorated,
27+
private LoggerFacadeInterface $loggerFacade,
28+
private LogFormatterFactoryInterface $logFormatterFactory,
29+
private PluginHttpLoggerConfigurationInterface $configuration
30+
) {
31+
}
32+
33+
public function create(): RouteExecutorInterface
34+
{
35+
return new HttpExecutorLoggerAwareDecorator(
36+
$this->decorated,
37+
$this->loggerFacade->getLogger($this->configuration->getAccessLoggerName()),
38+
$this->loggerFacade->getLogger($this->configuration->getErrorLoggerName()),
39+
$this->logFormatterFactory->create($this->configuration->getAccessLogFormat()),
40+
$this->logFormatterFactory->create($this->configuration->getErrorLogFormat())
41+
);
42+
}
43+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Formatter\Format;
15+
16+
use Micro\Plugin\Http\Business\Formatter\LogFormatterInterface;
17+
use Micro\Plugin\Http\Exception\HttpException;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\HttpFoundation\Response;
20+
21+
/**
22+
* @author Stanislau Komar <kost@micro-php.net>
23+
*/
24+
abstract class AbstractFormat implements LogFormatterInterface
25+
{
26+
public function __construct(
27+
private readonly string $formatString
28+
) {
29+
}
30+
31+
public function format(Request $request, Response|null $response, ?HttpException $exception): string
32+
{
33+
$var = '{{'.$this->getVarName().'}}';
34+
35+
if (!str_contains($this->formatString, $var)) {
36+
return $this->formatString;
37+
}
38+
39+
return str_ireplace(
40+
$var,
41+
$this->formatString,
42+
$this->getVarValue($request, $response, $exception)
43+
);
44+
}
45+
46+
abstract protected function getVarValue(Request $request, Response $response, ?HttpException $exception): string;
47+
48+
abstract protected function getVarName(): string;
49+
}

0 commit comments

Comments
 (0)