-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Contributte\Middlewares; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class LoggingMiddleware implements IMiddleware | ||
{ | ||
|
||
/** @var LoggerInterface */ | ||
private $logger; | ||
|
||
public function __construct(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface | ||
{ | ||
$uri = $request->getUri(); | ||
[$user, $password] = explode(':', $uri->getUserInfo()); | ||
$loggableUri = (string) $uri->withUserInfo($user, ''); // Remove password for security reasons | ||
$this->logger->info($loggableUri); | ||
|
||
return $next($request, $response); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* Test: LoggingMiddleware | ||
*/ | ||
|
||
use Contributte\Middlewares\LoggingMiddleware; | ||
use Contributte\Psr7\Psr7ResponseFactory; | ||
use Contributte\Psr7\Psr7ServerRequestFactory; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Tester\Assert; | ||
|
||
require_once __DIR__ . '/../bootstrap.php'; | ||
|
||
test(function (): void { | ||
$logger = new class implements LoggerInterface | ||
{ | ||
|
||
/** @var string */ | ||
private $url = ''; | ||
|
||
/** | ||
* @param string $message | ||
* @param mixed[] $context | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
*/ | ||
public function emergency($message, array $context = []): void | ||
{ | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @param mixed[] $context | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
*/ | ||
public function alert($message, array $context = []): void | ||
{ | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @param mixed[] $context | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
*/ | ||
public function critical($message, array $context = []): void | ||
{ | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @param mixed[] $context | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
*/ | ||
public function error($message, array $context = []): void | ||
{ | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @param mixed[] $context | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
*/ | ||
public function warning($message, array $context = []): void | ||
{ | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @param mixed[] $context | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
*/ | ||
public function notice($message, array $context = []): void | ||
{ | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @param mixed[] $context | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
*/ | ||
public function info($message, array $context = []): void | ||
{ | ||
$this->url = $message; | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @param mixed[] $context | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
*/ | ||
public function debug($message, array $context = []): void | ||
{ | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* @param mixed[] $context | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
*/ | ||
public function log($level, $message, array $context = []): void | ||
{ | ||
} | ||
|
||
public function get(): string | ||
{ | ||
return $this->url; | ||
} | ||
|
||
}; | ||
|
||
$response = Psr7ResponseFactory::fromGlobal(); | ||
$middleware = new LoggingMiddleware($logger); | ||
$response = $middleware( | ||
Psr7ServerRequestFactory::fromSuperGlobal()->withNewUri('https://user:password@example.com/foo/bar'), | ||
$response, | ||
function (ServerRequestInterface $psr7Request, ResponseInterface $psr7Response): ResponseInterface { | ||
return $psr7Response; | ||
} | ||
); | ||
|
||
Assert::same('https://user@example.com/foo/bar', $logger->get()); | ||
}); |