Initial xentral_oss_20.3.c9ffacf

This commit is contained in:
Alex
2021-05-21 08:49:41 +02:00
parent 5406e4a551
commit 34e5ac43d9
18884 changed files with 2109867 additions and 0 deletions
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace Xentral\Components\Logger\Handler;
use Xentral\Components\Logger\Exception\InvalidArgumentException;
use Xentral\Components\Logger\LogLevel;
abstract class AbstractLogHandler implements LogHandlerInterface
{
/** @var int[] LEVEL_ORDER */
protected const LEVEL_ORDER = [
LogLevel::EMERGENCY => 8,
LogLevel::ALERT => 7,
LogLevel::CRITICAL => 6,
LogLevel::ERROR => 5,
LogLevel::WARNING => 4,
LogLevel::NOTICE => 3,
LogLevel::INFO => 2,
LogLevel::DEBUG => 1,
];
/** @var string $minimumLevel */
protected $minimumLevel;
/**
* @param string $level
*
* @return void
*/
protected function setMinimumLevel(string $level): void
{
if (!array_key_exists($level, self::LEVEL_ORDER)) {
throw new InvalidArgumentException(sprintf('Unrecognised loglevel "%s".', $level));
}
$this->minimumLevel = $level;
}
/**
* @param string $level
*
* @return bool
*/
public function canHandle(string $level): bool
{
if (!array_key_exists($level, self::LEVEL_ORDER)) {
return false;
}
return self::LEVEL_ORDER[$level] >= self::LEVEL_ORDER[$this->minimumLevel];
}
}
@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
namespace Xentral\Components\Logger\Handler;
use Xentral\Components\Database\Database;
use Xentral\Components\Logger\Context\ContextInterface;
final class DatabaseLogHandler extends AbstractLogHandler
{
/** @var Database $db */
private $db;
/**
* @param Database $database
* @param string $level
*/
public function __construct(Database $database, string $level)
{
$this->setMinimumLevel($level);
$this->db = $database;
}
/**
* @param string $level
* @param string $message
* @param ContextInterface $context
*
* @return void
*/
public function addLogEntry(string $level, string $message, ContextInterface $context): void
{
if (!$this->canHandle($level)) {
return;
}
$values = [
'level' => strtoupper($level),
'message' => $message,
'class' => null,
'method' => null,
'line' => null,
'origin_type' => null,
'origin_detail' => null,
'dump' => null,
];
$values['class'] = $context->getClass();
$values['method'] = $context->getFunction();
$values['line'] = $context->getLine();
if ($context->hasOrigin()) {
$values['origin_type'] = $context->getOriginType();
$values['origin_detail'] = $context->getOriginDetail();
}
if ($context->hasDump()) {
$values['dump'] = print_r($context->getDump(), true);
}
if ($context->hasException()) {
$values['dump'] = (string)$context->getException();
}
$sql = 'INSERT INTO `log`
(`log_time`, `level`, `message`, `class`, `method`, `line`, `origin_type`, `origin_detail`, `dump`)
VALUES
(NOW(), :level, :message, :class, :method, :line, :origin_type, :origin_detail, :dump)';
$this->db->perform($sql, $values);
}
}
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Xentral\Components\Logger\Handler;
use Xentral\Components\Logger\Context\ContextInterface;
interface LogHandlerInterface
{
/**
* @param string $level
* @param string $message
* @param ContextInterface $context
*
* @return void
*/
public function addLogEntry(
string $level,
string $message,
ContextInterface $context
): void;
/**
* @param string $level
*
* @return bool
*/
public function canHandle(string $level): bool;
}
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace Xentral\Components\Logger\Handler;
use Xentral\Components\Logger\Context\ContextInterface;
final class MemoryLogHandler extends AbstractLogHandler
{
/** @var string[] $messages */
private $messages;
/** @var ContextInterface[] $entries */
private $contexts;
/**
* @param string $level
*/
public function __construct(string $level)
{
$this->messages = [];
$this->contexts = [];
$this->setMinimumLevel($level);
}
/**
* @param string $level
* @param string $message
* @param ContextInterface $context
*
* @return void
*/
public function addLogEntry(string $level, string $message, ContextInterface $context): void
{
$this->contexts[] = $context;
$this->messages[] = sprintf('%s: %s', strtoupper($level), $message);
}
/**
* @return string[]
*/
public function getMessages(): array
{
return $this->messages;
}
/**
* @return ContextInterface[]
*/
public function getContexts(): array
{
return $this->contexts;
}
}
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Xentral\Components\Logger\Handler;
use Xentral\Components\Logger\Context\ContextInterface;
final class NullLogHandler implements LogHandlerInterface
{
/**
* @param string $level
*
* @return bool
*/
public function canHandle(string $level): bool
{
return true;
}
/**
* @param string $level
* @param string $message
* @param ContextInterface $context
*
* @return void
*/
public function addLogEntry(string $level, string $message, ContextInterface $context): void
{
}
}
@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);
namespace Xentral\Components\Logger\Handler;
use Xentral\Components\Logger\Context\ContextInterface;
final class StreamLogHandler extends AbstractLogHandler
{
/** @var string FORMAT */
private const FORMAT = '%TIME% [%ORIGIN_TYPE%:%ORIGIN_DETAIL%] %CLASS%::%METHOD%(%LINE%)'
.' %LEVEL% %MESSAGE% %DUMP%';
/** @var string $stream */
private $logFile;
/**
* @param string $logFilePath
* @param string $level
*/
public function __construct(string $logFilePath, string $level)
{
$this->logFile = $logFilePath;
$this->setMinimumLevel($level);
}
/**
* @param string $level
* @param string $message
* @param ContextInterface $context
*
* @return void
*/
public function addLogEntry(string $level, string $message, ContextInterface $context): void
{
if (!$this->canHandle($level)) {
return;
}
$dump = '';
if ($context->hasDump() && !$context->hasException()) {
$dump = print_r($context->getDump(), true);
}
if ($context->hasException()) {
$dump = (string)$context->getException();
}
$entry = self::FORMAT;
$entry = preg_replace(
[
'/%TIME%/',
'/%LEVEL%/',
'/%MESSAGE%/',
'/%CLASS%/',
'/%METHOD%/',
'/%LINE%/',
'/%ORIGIN_TYPE%/',
'/%ORIGIN_DETAIL%/',
'/%DUMP%/',
],
[
$context->getTime()->format('Y-m-d H:i:s'),
strtoupper($level),
$message,
$context->getClass(),
$context->getFunction(),
$context->getLine(),
$context->getOriginType(),
$context->getOriginDetail(),
$dump
],
$entry
);
$entry .= "\r\n";
$stream = fopen($this->logFile, 'ab+');
fwrite($stream, $entry);
fclose($stream);
}
}