-
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.
[Patch] Log Record Updates for Laravel 10 (#36)
* Introduces LogRecordFactory class to which allows a Log Record to be built differently depending on mongolog version that host laravel application is running. * Introduces ContentProcessingStrategy as a Singleton to manage diverse content handlers * Implements StringContentHandler, ArrayContentHandler, and LogRecordContentHandler and register them with ContentProcessingStrategy singleton * Enhance test coverage for ContentProcessingStrategy & ScrubMessageTest * Updates Composer to upgrade to v3 mongolog for Illuminate\Contracts v10 * Updates to Composer to sustain support for to v2 mongolog for Illuminate\Contracts v9 * Exposes the regex repository through scrubber service to scrubber class. * Bug fixes * Remove support for illuminate\contracts 8 * Updates README credit adding @majchrosoft && @lucaxue 🖖🏼 🖖🏼 🙏🏼 👏🏼 --------- Co-authored-by: yordadev <yordadev@users.noreply.github.com>
- Loading branch information
Showing
20 changed files
with
634 additions
and
165 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
43 changes: 43 additions & 0 deletions
43
src/Strategies/ContentProcessingStrategy/ContentProcessingStrategy.php
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,43 @@ | ||
<?php | ||
|
||
namespace YorCreative\Scrubber\Strategies\ContentProcessingStrategy; | ||
|
||
use Illuminate\Support\Collection; | ||
use Monolog\LogRecord; | ||
use RuntimeException; | ||
|
||
class ContentProcessingStrategy | ||
{ | ||
protected Collection $handlers; | ||
|
||
public function __construct() | ||
{ | ||
$this->handlers = new Collection(); | ||
} | ||
|
||
public function setHandler(ProcessHandlerContract $handler): void | ||
{ | ||
$this->handlers->push($handler); | ||
} | ||
|
||
public function processContent(mixed $content): string|array|LogRecord | ||
{ | ||
$index = $this->detectHandlerIndex($content); | ||
|
||
return is_null($index) | ||
? throw new RuntimeException('Cannot process content: '.json_encode($content)) | ||
: $this->getHandlers()->get($index)->processContent($content); | ||
} | ||
|
||
private function detectHandlerIndex(mixed $content): ?int | ||
{ | ||
return $this->getHandlers()->search(function (ProcessHandlerContract $handler) use ($content) { | ||
return $handler->canProcess($content); | ||
}); | ||
} | ||
|
||
public function getHandlers(): Collection | ||
{ | ||
return $this->handlers; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Strategies/ContentProcessingStrategy/Handlers/ArrayContentHandler.php
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,22 @@ | ||
<?php | ||
|
||
namespace YorCreative\Scrubber\Strategies\ContentProcessingStrategy\Handlers; | ||
|
||
use Monolog\LogRecord; | ||
use YorCreative\Scrubber\Strategies\ContentProcessingStrategy\ProcessHandlerContract; | ||
use YorCreative\Scrubber\Strategies\ContentProcessingStrategy\Traits\ProcessArrayTrait; | ||
|
||
class ArrayContentHandler implements ProcessHandlerContract | ||
{ | ||
use ProcessArrayTrait; | ||
|
||
public function canProcess(mixed $content): bool | ||
{ | ||
return is_array($content); | ||
} | ||
|
||
public function processContent(mixed $content): string|array|LogRecord | ||
{ | ||
return $this->processArray($content); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Strategies/ContentProcessingStrategy/Handlers/LogRecordContentHandler.php
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,41 @@ | ||
<?php | ||
|
||
namespace YorCreative\Scrubber\Strategies\ContentProcessingStrategy\Handlers; | ||
|
||
use Monolog\LogRecord; | ||
use YorCreative\Scrubber\Strategies\ContentProcessingStrategy\ContentProcessingStrategy; | ||
use YorCreative\Scrubber\Strategies\ContentProcessingStrategy\ProcessHandlerContract; | ||
use YorCreative\Scrubber\Strategies\ContentProcessingStrategy\Traits\ProcessArrayTrait; | ||
use YorCreative\Scrubber\Support\LogRecordFactory; | ||
|
||
class LogRecordContentHandler implements ProcessHandlerContract | ||
{ | ||
use ProcessArrayTrait; | ||
|
||
public function canProcess(mixed $content): bool | ||
{ | ||
return $content instanceof LogRecord; | ||
} | ||
|
||
public function processContent(mixed $content): string|array|LogRecord | ||
{ | ||
$logRecordArr = $content->toArray(); | ||
|
||
$logRecordArr['message'] = empty($message = $logRecordArr['message']) | ||
? $message | ||
: app(ContentProcessingStrategy::class)->processContent($logRecordArr['message']); | ||
|
||
$logRecordArr['context'] = empty($context = $logRecordArr['context']) | ||
? [] | ||
: app(ContentProcessingStrategy::class)->processContent($context); | ||
|
||
return LogRecordFactory::buildRecord( | ||
$logRecordArr['datetime'], | ||
$logRecordArr['channel'], | ||
$logRecordArr['level'], | ||
$logRecordArr['message'], | ||
$logRecordArr['context'], | ||
$logRecordArr['extra'] | ||
); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Strategies/ContentProcessingStrategy/Handlers/StringContentHandler.php
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,22 @@ | ||
<?php | ||
|
||
namespace YorCreative\Scrubber\Strategies\ContentProcessingStrategy\Handlers; | ||
|
||
use Monolog\LogRecord; | ||
use YorCreative\Scrubber\Services\ScrubberService; | ||
use YorCreative\Scrubber\Strategies\ContentProcessingStrategy\ProcessHandlerContract; | ||
|
||
class StringContentHandler implements ProcessHandlerContract | ||
{ | ||
public function canProcess(mixed $content): bool | ||
{ | ||
return is_string($content); | ||
} | ||
|
||
public function processContent(mixed $content): string|array|LogRecord | ||
{ | ||
ScrubberService::autoSanitize($content); | ||
|
||
return $content; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Strategies/ContentProcessingStrategy/ProcessHandlerContract.php
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,12 @@ | ||
<?php | ||
|
||
namespace YorCreative\Scrubber\Strategies\ContentProcessingStrategy; | ||
|
||
use Monolog\LogRecord; | ||
|
||
interface ProcessHandlerContract | ||
{ | ||
public function canProcess(mixed $content): bool; | ||
|
||
public function processContent(mixed $content): string|array|LogRecord; | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Strategies/ContentProcessingStrategy/Traits/ProcessArrayTrait.php
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,42 @@ | ||
<?php | ||
|
||
namespace YorCreative\Scrubber\Strategies\ContentProcessingStrategy\Traits; | ||
|
||
use YorCreative\Scrubber\Services\ScrubberService; | ||
|
||
trait ProcessArrayTrait | ||
{ | ||
public function processArrayRecursively(array $content): array | ||
{ | ||
foreach ($content as $key => $value) { | ||
if (null !== $value) { | ||
if (is_array($value)) { | ||
$content[$key] = $this->processArray($value); | ||
} elseif (is_object($value) && ! method_exists($value, '__toString')) { | ||
$content[$key] = $this->processArray((array) $value); | ||
} else { | ||
$value = (string) $value; | ||
|
||
ScrubberService::autoSanitize($value); | ||
|
||
$content[$key] = $value; | ||
} | ||
} | ||
} | ||
|
||
return $content; | ||
} | ||
|
||
public function processArray(array $content): array | ||
{ | ||
$jsonContent = ScrubberService::encodeRecord($content); | ||
if ('' === $jsonContent) { | ||
// failed to convert array to JSON, so process array recursively | ||
return $this->processArrayRecursively($content); | ||
} | ||
|
||
ScrubberService::autoSanitize($jsonContent); | ||
|
||
return ScrubberService::decodeRecord($jsonContent); | ||
} | ||
} |
Oops, something went wrong.