Skip to content

Commit

Permalink
Extract logging logic outside Util class (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ddly authored Dec 5, 2023
1 parent d3cc995 commit e074187
Show file tree
Hide file tree
Showing 7 changed files with 369 additions and 46 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
},
"require": {
"php": ">=5.6.0",
"ext-curl": "*"
"ext-curl": "*",
"ext-json": "*",
"phpseclib/phpseclib": "^2 || ^3"
},
"require-dev": {
"php-parallel-lint/php-parallel-lint": "^1.3.2",
Expand Down
70 changes: 70 additions & 0 deletions src/Utilities/FileLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Tpay\OriginApi\Utilities;

class FileLogger
{
/** @var null|string */
private $logFilePath;

/** @param null|string $logFilePath */
public function __construct($logFilePath)
{
$this->logFilePath = $logFilePath;
}

public function log($level, $message, array $context = [])
{
$this->info($message);
}

/**
* @param string $message
*
* @throws TException
*/
public function info($message)
{
$content = json_decode($message, true);
$logText = PHP_EOL.'===========================';
$logText .= PHP_EOL.$content['title'];
$logText .= PHP_EOL.'===========================';
$logText .= PHP_EOL.$content['date'];
$logText .= PHP_EOL.'ip: '.$content['ip'];
$logText .= PHP_EOL;
$logText .= $content['message'];
$logText .= PHP_EOL;

$this->checkLogFile();
file_put_contents($this->getLogPath(), $logText, FILE_APPEND);
}

/** @return string */
private function getLogPath()
{
$logFileName = sprintf('log_%s.log', date('Y-m-d'));

if (null !== $this->logFilePath) {
$logPath = $this->logFilePath.$logFileName;
} else {
$logPath = sprintf('%s/../Logs/%s', __DIR__, $logFileName);
}

return $logPath;
}

/** @throws TException */
private function checkLogFile()
{
$logFilePath = $this->getLogPath();

if (!file_exists($logFilePath)) {
file_put_contents($logFilePath, '<?php exit; ?> '.PHP_EOL);
chmod($logFilePath, 0644);
}

if (!file_exists($logFilePath) || !is_writable($logFilePath)) {
throw new TException('Unable to create or write the log file');
}
}
}
75 changes: 75 additions & 0 deletions src/Utilities/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Tpay\OriginApi\Utilities;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class Logger
{
/** @var LoggerInterface */
private static $logger;

/** @var string */
private static $customLogPath;

public static function disableLogging()
{
self::$logger = new NullLogger();
}

/**
* @param mixed $logger
*
* @throws TException
*/
public static function setLogger($logger)
{
if (false === assert($logger instanceof LoggerInterface)) {
throw new TException(sprintf('%s is not instance of LoggerInterface', get_class($logger)));
}

self::$logger = $logger;
}

/** @param string $logPath */
public static function setLogPath($logPath)
{
self::$customLogPath = $logPath;
}

/** @return FileLogger|LoggerInterface */
public static function getLogger()
{
if (null === self::$logger) {
self::$logger = new FileLogger(self::$customLogPath);
}

return self::$logger;
}

/**
* @param string $title
* @param string $text
* @param string $logLevel
*/
public static function log($title, $text, $logLevel = 'info')
{
$ip = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : 'Empty server REMOTE_ADDR';
$content = [
'ip' => $ip,
'title' => $title,
'date' => date('Y-m-d H:i:s'),
'message' => $text,
'logLevel' => $logLevel,
];

self::getLogger()->log($logLevel, json_encode($content));
}

/** @param string $text */
public static function logLine($text)
{
self::$logger->info((string) $text);
}
}
58 changes: 13 additions & 45 deletions src/Utilities/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Tpay\OriginApi\Utilities;

use Exception;

/**
* Utility class which helps with:
* - parsing template files
Expand Down Expand Up @@ -79,22 +77,7 @@ public static function parseTemplate($templateFileName, $data = [])
*/
public static function log($title, $text)
{
$text = (string) $text;
$logFilePath = self::getLogPath();
$ip = (isset($_SERVER[static::REMOTE_ADDR])) ? $_SERVER[static::REMOTE_ADDR] : '';

$logText = PHP_EOL.'===========================';
$logText .= PHP_EOL.$title;
$logText .= PHP_EOL.'===========================';
$logText .= PHP_EOL.date('Y-m-d H:i:s');
$logText .= PHP_EOL.'ip: '.$ip;
$logText .= PHP_EOL;
$logText .= $text;
$logText .= PHP_EOL.PHP_EOL;

if (true === static::$loggingEnabled) {
file_put_contents($logFilePath, $logText, FILE_APPEND);
}
Logger::log($title, $text);
}

/**
Expand All @@ -104,11 +87,18 @@ public static function log($title, $text)
*/
public static function logLine($text)
{
$text = (string) $text;
$logFilePath = self::getLogPath();
if (true === static::$loggingEnabled) {
file_put_contents($logFilePath, PHP_EOL.$text, FILE_APPEND);
}
Logger::logLine($text);
}

/**
* @param float|int $number
* @param int $decimals
*
* @return string
*/
public static function numberFormat($number, $decimals = 2)
{
return number_format($number, $decimals, '.', '');
}

/**
Expand Down Expand Up @@ -161,26 +151,4 @@ public function setPath($path)

return $this;
}

private static function getLogPath()
{
if (false === static::$loggingEnabled) {
return;
}
$logFileName = 'log_'.date('Y-m-d').'.php';
if (!empty(static::$customLogPatch)) {
$logPath = static::$customLogPatch.$logFileName;
} else {
$logPath = dirname(__FILE__).'/../../Logs/'.$logFileName;
}
if (!file_exists($logPath)) {
file_put_contents($logPath, '<?php exit; ?> '.PHP_EOL);
chmod($logPath, 0644);
}
if (!file_exists($logPath) || !is_writable($logPath)) {
throw new Exception('Unable to create or write the log file');
}

return $logPath;
}
}
13 changes: 13 additions & 0 deletions src/Utilities/phpseclib/Crypt/RSA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tpay\OriginApi\Utilities\phpseclib\Crypt;

use RuntimeException;

if (class_exists('phpseclib3\Crypt\RSA')) {
abstract class RSA extends \phpseclib3\Crypt\RSA {}
} elseif (class_exists('phpseclib\Crypt\RSA')) {
class RSA extends \phpseclib\Crypt\RSA {}
} else {
throw new RuntimeException('Cannot find supported phpseclib3/phpseclib library');
}
28 changes: 28 additions & 0 deletions src/Utilities/phpseclib/File/X509.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Tpay\OriginApi\Utilities\phpseclib\File;

use RuntimeException;

if (class_exists('phpseclib3\File\X509')) {
class X509 extends \phpseclib3\File\X509
{
public function withSettings($publicKey, $hash, $signature)
{
return $publicKey->withHash($hash)->withPadding($signature);
}
}
} elseif (class_exists('phpseclib\File\X509')) {
class X509 extends \phpseclib\File\X509
{
public function withSettings($publicKey, $hash, $signature)
{
$publicKey->setHash($hash);
$publicKey->setSignatureMode($signature);

return $publicKey;
}
}
} else {
throw new RuntimeException('Cannot find supported phpseclib/phpseclib library');
}
Loading

0 comments on commit e074187

Please sign in to comment.