From ab673c63f415fd315d31d625197530e772107f55 Mon Sep 17 00:00:00 2001 From: Edmond <1571649+EdmondDantes@users.noreply.github.com> Date: Sun, 27 Oct 2024 17:01:43 +0200 Subject: [PATCH] + Release 5.1.0 --- CHANGELOG.md | 31 ++++++ README.md | 27 ++++- composer.json | 3 +- composer.lock | 103 ++++++++++++++++++- src/BaseException.php | 6 +- src/BaseExceptionInterface.php | 2 +- src/Errors/Error.php | 2 +- src/MonologProcessor.php | 27 +++++ src/Registry.php | 1 - src/Resource/FileSystem/FileCloseError.php | 6 +- src/Resource/FileSystem/FileLockFailed.php | 8 +- src/Resource/FileSystem/FileNotExists.php | 8 +- src/Resource/FileSystem/FileNotReadable.php | 8 +- src/Resource/FileSystem/FileNotWritable.php | 8 +- src/Resource/FileSystem/FileOpenError.php | 8 +- src/Resource/FileSystem/FileReadError.php | 8 +- src/Resource/FileSystem/FileUnLockFailed.php | 8 +- src/Resource/FileSystem/FileWriteError.php | 6 +- tests/ArraySerializerTraitTest.php | 2 +- tests/BaseExceptionTest.php | 3 - 20 files changed, 245 insertions(+), 30 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 src/MonologProcessor.php diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..3ee585a --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,31 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [5.1.0] - 2024-10-27 + +### Added + +- Added interface `Throwable` to `BaseExceptionInterface`. +- Class `Error` was extended from `ErrorException`. +- Added `MonologProcessor` for Monolog. + +### Fixed + +- Fixed the PHPDoc type hints for `PHPStan`. +- Fixed the `ResourceException` class and sibling classes. + +### Changed + +- Rename method `BaseExceptionInterface::template()` to `BaseExceptionInterface::getTemplate()`. +- Update documentation (English and Russian). +- Exceptions are no longer added to the `exception registry`. +This functionality is disabled by default. + +### Removed + diff --git a/README.md b/README.md index 8c0f6c9..649f688 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ BaseException [![PHP Composer](https://github.com/EdmondDantes/amphp-pool/action ============= -Base Exception Library for PHP 8.2+ -(The latest version: 5.0.0) +Base Exception Library for PHP 8.3+ +(The latest version: 5.1.0) Missions: @@ -87,6 +87,29 @@ List of parameters: ``` +## Monolog integration + +The processor for `Monolog` allows populating exception metadata in the log. + +```php +pushProcessor(new MonologProcessor()); + +// Put some records to the log +$log->error( + 'this is a loggable exception', + ['exception' => new LoggableException('this is a loggable exception')] +); +``` + ## Exception Container ```php diff --git a/composer.json b/composer.json index 192cd45..397e9fc 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ "phpunit/phpunit": "^11.2", "rector/rector": "^1.2", "ifcastle/codestyle": "^0.1.7", - "phpstan/phpstan": "^1.12" + "phpstan/phpstan": "^1.12", + "monolog/monolog": "^3.7" }, "scripts": { "test": "vendor/bin/phpunit --coverage-clover=coverage/clover.xml --coverage-filter=src ./tests", diff --git a/composer.lock b/composer.lock index a6512f2..a68288c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f353695b002cdd50b794ab82102789ff", + "content-hash": "7ad19fe81fa4dbf2344ed96451d6e58b", "packages": [], "packages-dev": [ { @@ -549,6 +549,107 @@ }, "time": "2024-10-26T13:56:03+00:00" }, + { + "name": "monolog/monolog", + "version": "3.7.0", + "source": { + "type": "git", + "url": "https://github.com/Seldaek/monolog.git", + "reference": "f4393b648b78a5408747de94fca38beb5f7e9ef8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/f4393b648b78a5408747de94fca38beb5f7e9ef8", + "reference": "f4393b648b78a5408747de94fca38beb5f7e9ef8", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "psr/log": "^2.0 || ^3.0" + }, + "provide": { + "psr/log-implementation": "3.0.0" + }, + "require-dev": { + "aws/aws-sdk-php": "^3.0", + "doctrine/couchdb": "~1.0@dev", + "elasticsearch/elasticsearch": "^7 || ^8", + "ext-json": "*", + "graylog2/gelf-php": "^1.4.2 || ^2.0", + "guzzlehttp/guzzle": "^7.4.5", + "guzzlehttp/psr7": "^2.2", + "mongodb/mongodb": "^1.8", + "php-amqplib/php-amqplib": "~2.4 || ^3", + "phpstan/phpstan": "^1.9", + "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-strict-rules": "^1.4", + "phpunit/phpunit": "^10.5.17", + "predis/predis": "^1.1 || ^2", + "ruflin/elastica": "^7", + "symfony/mailer": "^5.4 || ^6", + "symfony/mime": "^5.4 || ^6" + }, + "suggest": { + "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB", + "doctrine/couchdb": "Allow sending log messages to a CouchDB server", + "elasticsearch/elasticsearch": "Allow sending log messages to an Elasticsearch server via official client", + "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", + "ext-curl": "Required to send log messages using the IFTTTHandler, the LogglyHandler, the SendGridHandler, the SlackWebhookHandler or the TelegramBotHandler", + "ext-mbstring": "Allow to work properly with unicode symbols", + "ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)", + "ext-openssl": "Required to send log messages using SSL", + "ext-sockets": "Allow sending log messages to a Syslog server (via UDP driver)", + "graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server", + "mongodb/mongodb": "Allow sending log messages to a MongoDB server (via library)", + "php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib", + "rollbar/rollbar": "Allow sending log messages to Rollbar", + "ruflin/elastica": "Allow sending log messages to an Elastic Search server" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Monolog\\": "src/Monolog" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "https://seld.be" + } + ], + "description": "Sends your logs to files, sockets, inboxes, databases and various web services", + "homepage": "https://github.com/Seldaek/monolog", + "keywords": [ + "log", + "logging", + "psr-3" + ], + "support": { + "issues": "https://github.com/Seldaek/monolog/issues", + "source": "https://github.com/Seldaek/monolog/tree/3.7.0" + }, + "funding": [ + { + "url": "https://github.com/Seldaek", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/monolog/monolog", + "type": "tidelift" + } + ], + "time": "2024-06-28T09:40:51+00:00" + }, { "name": "myclabs/deep-copy", "version": "1.12.0", diff --git a/src/BaseException.php b/src/BaseException.php index 183c1f7..3ad400e 100644 --- a/src/BaseException.php +++ b/src/BaseException.php @@ -224,7 +224,7 @@ public function __construct(BaseExceptionInterface|\Throwable|array|string $exce * Returns template message. */ #[\Override] - public function template(): string + public function getTemplate(): string { return $this->template; } @@ -413,14 +413,14 @@ public function toArray(): array } // override the exception message if the template was defined - $message = $this->template() !== '' ? $this->getExceptionData()['message'] ?? '' : $this->getMessage(); + $message = $this->getTemplate() !== '' ? $this->getExceptionData()['message'] ?? '' : $this->getMessage(); return [ 'type' => static::class, 'source' => $this->getSource(), 'message' => $message, - 'template' => $this->template(), + 'template' => $this->getTemplate(), 'tags' => $this->getTags(), 'code' => $this->getCode(), 'data' => $this->getExceptionData(), diff --git a/src/BaseExceptionInterface.php b/src/BaseExceptionInterface.php index b83b761..44137db 100644 --- a/src/BaseExceptionInterface.php +++ b/src/BaseExceptionInterface.php @@ -81,7 +81,7 @@ public function getTraceAsString(): string; /** * Template message. */ - public function template(): string; + public function getTemplate(): string; /** * @return string[] diff --git a/src/Errors/Error.php b/src/Errors/Error.php index 4c84fe9..9537886 100644 --- a/src/Errors/Error.php +++ b/src/Errors/Error.php @@ -230,7 +230,7 @@ public function appendData(array $data): static } #[\Override] - public function template(): string + public function getTemplate(): string { return ''; } diff --git a/src/MonologProcessor.php b/src/MonologProcessor.php new file mode 100644 index 0000000..5c9753a --- /dev/null +++ b/src/MonologProcessor.php @@ -0,0 +1,27 @@ +context['exception'] instanceof BaseExceptionInterface) { + return $record; + } + + $exception = $record->context['exception']; + + $record->extra['exception_template'] = $exception->getTemplate(); + $record->extra['exception_data'] = $exception->getExceptionData(); + $record->extra['exception_debug'] = $exception->getDebugData(); + $record->extra['tags'] = array_merge($record->extra['tags'] ?? [], $exception->getTags()); + + return $record; + } +} \ No newline at end of file diff --git a/src/Registry.php b/src/Registry.php index acca54c..ab4be87 100644 --- a/src/Registry.php +++ b/src/Registry.php @@ -19,7 +19,6 @@ class Registry { /** * Flag for global handler. - * @var bool */ public static bool $isActive = false; diff --git a/src/Resource/FileSystem/FileCloseError.php b/src/Resource/FileSystem/FileCloseError.php index 4133d7e..74c1c7d 100644 --- a/src/Resource/FileSystem/FileCloseError.php +++ b/src/Resource/FileSystem/FileCloseError.php @@ -9,7 +9,11 @@ class FileCloseError extends ResourceCloseError implements FileSystemExceptionInterface { protected string $system = self::SYSTEM; - + + /** + * @param string|object|resource|array $resource + * @param string $type + */ public function __construct(mixed $resource, string $type = 'file') { parent::__construct($resource, $type); diff --git a/src/Resource/FileSystem/FileLockFailed.php b/src/Resource/FileSystem/FileLockFailed.php index 867fe14..ba7b5b8 100644 --- a/src/Resource/FileSystem/FileLockFailed.php +++ b/src/Resource/FileSystem/FileLockFailed.php @@ -9,8 +9,12 @@ class FileLockFailed extends ResourceLockFailed implements FileSystemExceptionInterface { protected string $system = self::SYSTEM; - - public function __construct($resource, $type = 'file') + + /** + * @param string|object|resource|array $resource + * @param string $type + */ + public function __construct(mixed $resource, string $type = 'file') { parent::__construct($resource, $type); } diff --git a/src/Resource/FileSystem/FileNotExists.php b/src/Resource/FileSystem/FileNotExists.php index 731ce8b..70d8267 100644 --- a/src/Resource/FileSystem/FileNotExists.php +++ b/src/Resource/FileSystem/FileNotExists.php @@ -9,8 +9,12 @@ class FileNotExists extends ResourceNotExists implements FileSystemExceptionInterface { protected string $system = self::SYSTEM; - - public function __construct($resource, $type = 'file') + + /** + * @param string|object|resource|array $resource + * @param string $type + */ + public function __construct(mixed $resource, string $type = 'file') { parent::__construct($resource, $type); } diff --git a/src/Resource/FileSystem/FileNotReadable.php b/src/Resource/FileSystem/FileNotReadable.php index e527232..04da013 100644 --- a/src/Resource/FileSystem/FileNotReadable.php +++ b/src/Resource/FileSystem/FileNotReadable.php @@ -9,8 +9,12 @@ class FileNotReadable extends ResourceNotReadable implements FileSystemExceptionInterface { protected string $system = self::SYSTEM; - - public function __construct($resource, string $type = 'file') + + /** + * @param string|object|resource|array $resource + * @param string $type + */ + public function __construct(mixed $resource, string $type = 'file') { parent::__construct($resource, $type); } diff --git a/src/Resource/FileSystem/FileNotWritable.php b/src/Resource/FileSystem/FileNotWritable.php index 20fab2e..6ab5bf4 100644 --- a/src/Resource/FileSystem/FileNotWritable.php +++ b/src/Resource/FileSystem/FileNotWritable.php @@ -9,8 +9,12 @@ class FileNotWritable extends ResourceNotWritable implements FileSystemExceptionInterface { protected string $system = self::SYSTEM; - - public function __construct($resource, string $type = 'file') + + /** + * @param string|object|resource|array $resource + * @param string $type + */ + public function __construct(mixed $resource, string $type = 'file') { parent::__construct($resource, $type); } diff --git a/src/Resource/FileSystem/FileOpenError.php b/src/Resource/FileSystem/FileOpenError.php index 1c6b14f..f94d0fb 100644 --- a/src/Resource/FileSystem/FileOpenError.php +++ b/src/Resource/FileSystem/FileOpenError.php @@ -9,8 +9,12 @@ class FileOpenError extends ResourceOpenError implements FileSystemExceptionInterface { protected string $system = self::SYSTEM; - - public function __construct($resource, string $type = 'file') + + /** + * @param string|object|resource|array $resource + * @param string $type + */ + public function __construct(mixed $resource, string $type = 'file') { parent::__construct($resource, $type); } diff --git a/src/Resource/FileSystem/FileReadError.php b/src/Resource/FileSystem/FileReadError.php index 2197a15..d58b57b 100644 --- a/src/Resource/FileSystem/FileReadError.php +++ b/src/Resource/FileSystem/FileReadError.php @@ -9,8 +9,12 @@ class FileReadError extends ResourceReadError implements FileSystemExceptionInterface { protected string $system = self::SYSTEM; - - public function __construct($resource, string $type = 'file') + + /** + * @param string|object|resource|array $resource + * @param string $type + */ + public function __construct(mixed $resource, string $type = 'file') { parent::__construct($resource, $type); } diff --git a/src/Resource/FileSystem/FileUnLockFailed.php b/src/Resource/FileSystem/FileUnLockFailed.php index c6cc5f1..6db9eb1 100644 --- a/src/Resource/FileSystem/FileUnLockFailed.php +++ b/src/Resource/FileSystem/FileUnLockFailed.php @@ -9,8 +9,12 @@ class FileUnLockFailed extends ResourceUnLockFailed implements FileSystemExceptionInterface { protected string $system = self::SYSTEM; - - public function __construct($resource, $type = 'file') + + /** + * @param string|object|resource|array $resource + * @param string $type + */ + public function __construct(mixed $resource, string $type = 'file') { parent::__construct($resource, $type); } diff --git a/src/Resource/FileSystem/FileWriteError.php b/src/Resource/FileSystem/FileWriteError.php index 231b522..0c095ab 100644 --- a/src/Resource/FileSystem/FileWriteError.php +++ b/src/Resource/FileSystem/FileWriteError.php @@ -10,7 +10,11 @@ class FileWriteError extends ResourceWriteError implements FileSystemExceptionIn { protected string $system = self::SYSTEM; - public function __construct($resource, $type = 'file') + /** + * @param string|object|resource|array $resource + * @param string $type + */ + public function __construct(mixed $resource, string $type = 'file') { parent::__construct($resource, $type); } diff --git a/tests/ArraySerializerTraitTest.php b/tests/ArraySerializerTraitTest.php index 20b284b..438c08d 100644 --- a/tests/ArraySerializerTraitTest.php +++ b/tests/ArraySerializerTraitTest.php @@ -142,7 +142,7 @@ public function testArray_to_errors(): void foreach ($results as $exception) { $this->assertInstanceOf(BaseExceptionInterface::class, $exception); - $this->assertEquals('test template with {code} and {exdata}', $exception->template()); + $this->assertEquals('test template with {code} and {exdata}', $exception->getTemplate()); $this->assertEquals( 'test template with ' . ($i + 10) . ' and \'' . ($i - 10) . '\'. test message' . ($i + 5), $exception->getMessage(), diff --git a/tests/BaseExceptionTest.php b/tests/BaseExceptionTest.php index 5a8a98d..b088678 100644 --- a/tests/BaseExceptionTest.php +++ b/tests/BaseExceptionTest.php @@ -30,19 +30,16 @@ class BaseExceptionTest extends \PHPUnit\Framework\TestCase { /** * Test data for the exception. - * @var array */ protected array $testData; /** * Exception info. - * @var array */ protected array $testBaseData; /** * Exception. - * @var \IfCastle\Exceptions\BaseException */ protected BaseException $BaseException;