Add Litlab
This commit is contained in:
17
storage/patterns_examples/singleton/Loggable.php
Normal file
17
storage/patterns_examples/singleton/Loggable.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 07:36
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace RefactoringGuru\Builder\RealWorld\singleton;
|
||||
|
||||
interface Loggable
|
||||
{
|
||||
public static function getInstance(): static;
|
||||
}
|
||||
134
storage/patterns_examples/singleton/Logger.php
Normal file
134
storage/patterns_examples/singleton/Logger.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
*
|
||||
* @author: Yevhen Odynets
|
||||
*
|
||||
* @date: 2025-07-02
|
||||
*
|
||||
* @time: 22:30
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace RefactoringGuru\Builder\RealWorld\singleton;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
use function in_array;
|
||||
use function sprintf;
|
||||
|
||||
use const FILE_APPEND;
|
||||
use const PHP_EOL;
|
||||
|
||||
class Logger implements Loggable
|
||||
{
|
||||
public const string LOG_FILE = '../storage/logs/main.txt';
|
||||
public const array ALLOWED_LOG_TYPES
|
||||
= [
|
||||
'file',
|
||||
'error_log',
|
||||
];
|
||||
private static ?Logger $instance = null;
|
||||
private ?string $logType = null;
|
||||
private ?string $dateFormat = null;
|
||||
private ?string $prefix = null;
|
||||
|
||||
private function __construct() {}
|
||||
|
||||
public static function getInstance(): static
|
||||
{
|
||||
if (self::$instance) {
|
||||
self::$instance = new static();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function getLogType(): string
|
||||
{
|
||||
if (! $this->logType) {
|
||||
throw new RuntimeException('log type is null');
|
||||
}
|
||||
|
||||
return $this->logType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setLogType(string $logType): static
|
||||
{
|
||||
if (! in_array($logType, self::ALLOWED_LOG_TYPES)) {
|
||||
throw new RuntimeException('undefined log type: ' . $logType);
|
||||
}
|
||||
|
||||
$this->logType = $logType;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function log(string $data): void
|
||||
{
|
||||
$prefix = $this->getPrefix();
|
||||
|
||||
$message = sprintf(
|
||||
'%s: %s%s',
|
||||
date($this->getDateFormat()),
|
||||
empty($prefix) ? '' : $prefix . ' ',
|
||||
$data . PHP_EOL
|
||||
);
|
||||
|
||||
if ('file' === $this->logType) {
|
||||
file_put_contents(self::LOG_FILE, $message, FILE_APPEND);
|
||||
} elseif ('error_log' === $this->logType) {
|
||||
/* @noinspection ForgottenDebugOutputInspection */
|
||||
error_log($message, 3, self::LOG_FILE);
|
||||
}
|
||||
}
|
||||
|
||||
public function getPrefix(): ?string
|
||||
{
|
||||
return $this->prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setPrefix(?string $prefix): static
|
||||
{
|
||||
$this->prefix = $prefix;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDateFormat(): string
|
||||
{
|
||||
if (! $this->dateFormat) {
|
||||
throw new RuntimeException('date format is null');
|
||||
}
|
||||
|
||||
return $this->dateFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setDateFormat(string $dateFormat): static
|
||||
{
|
||||
$this->dateFormat = $dateFormat;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function __clone() {}
|
||||
}
|
||||
|
||||
|
||||
///*$message = 'Message from delivery service';
|
||||
//
|
||||
//$logger = new Logger();
|
||||
//$dateFormat = 'Y-m-d H:i:s' . (preg_match('/^win/i', PHP_OS) ? '' : '.u');
|
||||
//$logger->setDateFormat($dateFormat)->setLogType('error_log');
|
||||
//$logger->log($message);*/
|
||||
Reference in New Issue
Block a user