Skip to content

Commit

Permalink
Merge branch 'master' into 9.x
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-ivanov committed Jun 5, 2022
2 parents e579087 + 00fc85e commit 3f14f00
Show file tree
Hide file tree
Showing 28 changed files with 122 additions and 401 deletions.
15 changes: 5 additions & 10 deletions integrations/guzzle.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use function GuzzleHttp\Promise\rejection_for;
use GuzzleHttp\Promise\Create;
use Illuminated\Console\Exceptions\RuntimeException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -12,19 +12,13 @@
*
* @see https://github.com/dmitry-ivanov/laravel-console-logger#guzzle-6-integration
* @see http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html
*
* @param \Psr\Log\LoggerInterface $logger
* @param string $type
* @param callable|null $shouldLogRequestParams
* @param callable|null $shouldLogResponseBody
* @return \Closure
*/
function iclogger_guzzle_middleware(LoggerInterface $logger, string $type = 'raw', callable $shouldLogRequestParams = null, callable $shouldLogResponseBody = null)
function iclogger_guzzle_middleware(LoggerInterface $logger, string $type = 'raw', callable $shouldLogRequestParams = null, callable $shouldLogResponseBody = null): Closure
{
return function (callable $handler) use ($logger, $type, $shouldLogRequestParams, $shouldLogResponseBody) {
return function (RequestInterface $request, array $options) use ($handler, $logger, $type, $shouldLogRequestParams, $shouldLogResponseBody) {
// Gather information about the request
$method = (string) $request->getMethod();
$method = $request->getMethod();
$uri = (string) $request->getUri();
$body = (string) $request->getBody();

Expand Down Expand Up @@ -80,6 +74,7 @@ function (ResponseInterface $response) use ($request, $logger, $type, $shouldLog
}
// Save the parsed body of response, so that it could be re-used instead of double decoding
if (!empty($context)) {
/** @noinspection PhpUndefinedFieldInspection */
$response->iclParsedBody = $context;
}
}
Expand All @@ -88,7 +83,7 @@ function (ResponseInterface $response) use ($request, $logger, $type, $shouldLog
return $response;
},
function ($reason) {
return rejection_for($reason);
return Create::rejectionFor($reason);
}
);
};
Expand Down
55 changes: 13 additions & 42 deletions src/Exceptions/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,41 @@
namespace Illuminated\Console\Exceptions;

use Illuminate\Foundation\Exceptions\Handler;
use Psr\Log\LoggerInterface;
use Monolog\Logger;
use Throwable;

class ExceptionHandler extends Handler
{
/**
* The logger instance.
*
* @var \Psr\Log\LoggerInterface
*/
private $logger;
private Logger $logger;

/**
* Time when execution started.
*
* @var float
*/
private $timeStarted;

/**
* Time when execution finished.
*
* @var float
*/
private $timeFinished;
private float $timeStarted;

/**
* Reserved memory for the shutdown execution.
*
* @see https://github.com/dmitry-ivanov/laravel-console-logger/issues/4
*
* @var string
*/
protected $reservedMemory;
protected ?string $reservedMemory;

/**
* Initialize the exception handler.
*
* @param \Psr\Log\LoggerInterface $logger
* @return void
*/
public function initialize(LoggerInterface $logger)
public function initialize(Logger $logger): void
{
$this->setLogger($logger);
$this->registerShutdownFunction();
}

/**
* Set the logger.
*
* @param \Psr\Log\LoggerInterface $logger
* @return void
*/
public function setLogger(LoggerInterface $logger)
public function setLogger(Logger $logger): void
{
$this->logger = $logger;
}
Expand All @@ -66,11 +47,8 @@ public function setLogger(LoggerInterface $logger)
*
* Note that this method doesn't decorate, but overwrite the parent method:
* @see https://github.com/dmitry-ivanov/laravel-console-logger/pull/11
*
* @param \Throwable $e
* @return void
*/
public function report(Throwable $e)
public function report(Throwable $e): void
{
$context = [
'code' => $e->getCode(),
Expand All @@ -93,11 +71,8 @@ public function report(Throwable $e)

/**
* Add Sentry support.
*
* @param \Throwable $e
* @return void
*/
private function addSentrySupport(Throwable $e)
private function addSentrySupport(Throwable $e): void
{
if (app()->bound('sentry') && $this->shouldReport($e)) {
app('sentry')->captureException($e);
Expand All @@ -106,10 +81,8 @@ private function addSentrySupport(Throwable $e)

/**
* Register the shutdown function.
*
* @return void
*/
private function registerShutdownFunction()
private function registerShutdownFunction(): void
{
$this->timeStarted = microtime(true);
$this->reservedMemory = str_repeat(' ', 20 * 1024);
Expand All @@ -119,23 +92,21 @@ private function registerShutdownFunction()

/**
* Callback for the shutdown function.
*
* @return void
*/
public function onShutdown()
public function onShutdown(): void
{
$this->reservedMemory = null;

$this->timeFinished = microtime(true);
$executionTime = round($this->timeFinished - $this->timeStarted, 3);
$timeFinished = microtime(true);
$executionTime = round($timeFinished - $this->timeStarted, 3);
$this->logger->info("Execution time: {$executionTime} sec.");

$memoryPeak = format_bytes(memory_get_peak_usage(true));
$this->logger->info("Memory peak usage: {$memoryPeak}.");

$this->logger->info('%separator%');

$handlers = (array) $this->logger->getHandlers();
$handlers = $this->logger->getHandlers();
foreach ($handlers as $handler) {
$handler->close();
}
Expand Down
14 changes: 2 additions & 12 deletions src/Exceptions/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@ class RuntimeException extends SymfonyRuntimeException
{
/**
* The context.
*
* @var array
*/
private $context;
private array $context;

/**
* Create a new instance of the exception.
*
* @param string $message
* @param array $context
* @param int $code
* @param \Exception|null $previous
* @return void
*/
public function __construct(string $message = '', array $context = [], int $code = 0, Exception $previous = null)
{
Expand All @@ -32,10 +24,8 @@ public function __construct(string $message = '', array $context = [], int $code

/**
* Get the context.
*
* @return array
*/
public function getContext()
public function getContext(): array
{
return $this->context;
}
Expand Down
Loading

0 comments on commit 3f14f00

Please sign in to comment.