Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
hungnguyenhp committed Sep 19, 2021
1 parent 7816ede commit d5c541d
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 62 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 2018-2058 Nguyen An Hung <[email protected]> - https://nguyenanhung.com
Copyright (C) 2018 - 2068 Nguyen An Hung <[email protected]> - https://nguyenanhung.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -652,7 +652,7 @@ Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:

nguyenanhung/my-debug Copyright (C) 2018-2058 Nguyen An Hung <[email protected]> - https://nguyenanhung.com
nguyenanhung/my-debug Copyright (C) 2018 - 2068 Nguyen An Hung <[email protected]> - https://nguyenanhung.com
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
Expand Down
6 changes: 3 additions & 3 deletions src/Benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct()
*
* @return void
*/
public function mark($name)
public function mark(string $name)
{
$this->marker[$name] = microtime(TRUE);
}
Expand All @@ -75,7 +75,7 @@ public function mark($name)
* an '{elapsed_string}' if $point1 is empty
* or an empty string if $point1 is not found.
*/
public function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
public function elapsed_time(string $point1 = '', string $point2 = '', int $decimals = 4): string
{
if ($point1 === '') {
return '{elapsed_time}';
Expand All @@ -100,7 +100,7 @@ public function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
*
* @return string
*/
public function memory_usage()
public function memory_usage(): string
{
return round(memory_get_usage() / 1024 / 1024, 2) . 'MB';
}
Expand Down
68 changes: 34 additions & 34 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ class Logger implements Project
/** @var bool Cấu hình trạng thái Debug, TRUE nếu cấu hình Debug được bật */
private $DEBUG = false;

/** @var null|string Cấu hình Level lưu Log theo tiêu chuẩn RFC 5424 */
private $globalLoggerLevel = null;
/** @var string|null Cấu hình Level lưu Log theo tiêu chuẩn RFC 5424 */
private $globalLoggerLevel;

/** @var null|string Đường dẫn thư mục lưu trữ Log, VD: /your/to/path */
/** @var string|null Đường dẫn thư mục lưu trữ Log, VD: /your/to/path */
private $loggerPath = 'logs';

/** @var null|string Tương tự với $loggerPath, mặc định dùng để lưu tên class phát sinh log */
private $loggerSubPath = null;
/** @var string|null Tương tự với $loggerPath, mặc định dùng để lưu tên class phát sinh log */
private $loggerSubPath;

/** @var null|string Filename lưu log, khuyến nghị theo chuẩn Log-Y-m-d.log, VD: Log-2018-10-17.log */
private $loggerFilename = null;
/** @var string|null Filename lưu log, khuyến nghị theo chuẩn Log-Y-m-d.log, VD: Log-2018-10-17.log */
private $loggerFilename;

/** @var null|string Logger Date Format, VD: Y-m-d H:i:s u */
private $loggerDateFormat = null;
/** @var string|null Logger Date Format, VD: Y-m-d H:i:s u */
private $loggerDateFormat;

/** @var null|string Logger Line Format, VD: "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n" */
private $loggerLineFormat = null;
/** @var string|null Logger Line Format, VD: "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n" */
private $loggerLineFormat;

/**
* Logger constructor.
Expand All @@ -75,7 +75,7 @@ public function __construct()
* @copyright: 713uk13m <[email protected]>
* @time : 08/17/2021 59:07
*/
public function getDebugStatus()
public function getDebugStatus(): bool
{
return $this->DEBUG;
}
Expand All @@ -90,7 +90,7 @@ public function getDebugStatus()
* @copyright: 713uk13m <[email protected]>
* @time : 08/17/2021 59:17
*/
public function setDebugStatus($debug = false)
public function setDebugStatus(bool $debug = false): Logger
{
$this->DEBUG = $debug;

Expand Down Expand Up @@ -123,7 +123,7 @@ public function getGlobalLoggerLevel()
* @see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#log-levels
* @see https://tools.ietf.org/html/rfc5424
*/
public function setGlobalLoggerLevel($globalLoggerLevel = null)
public function setGlobalLoggerLevel(string $globalLoggerLevel = null): Logger
{
if (!empty($globalLoggerLevel) && is_string($globalLoggerLevel)) {
$this->globalLoggerLevel = strtolower($globalLoggerLevel);
Expand Down Expand Up @@ -168,7 +168,7 @@ public function getLoggerSubPath()
* @copyright: 713uk13m <[email protected]>
* @time : 08/17/2021 01:36
*/
public function setLoggerPath($loggerPath = '')
public function setLoggerPath(string $loggerPath = ''): Logger
{
if (!empty($loggerPath)) {
$this->loggerPath = trim($loggerPath);
Expand All @@ -187,7 +187,7 @@ public function setLoggerPath($loggerPath = '')
* @copyright: 713uk13m <[email protected]>
* @time : 08/17/2021 02:19
*/
public function setLoggerSubPath($loggerSubPath = '')
public function setLoggerSubPath(string $loggerSubPath = ''): Logger
{
if (!empty($loggerSubPath)) {
$this->loggerSubPath = trim($loggerSubPath);
Expand Down Expand Up @@ -219,7 +219,7 @@ public function getLoggerFilename()
* @copyright: 713uk13m <[email protected]>
* @time : 08/17/2021 03:20
*/
public function setLoggerFilename($loggerFilename = '')
public function setLoggerFilename(string $loggerFilename = ''): Logger
{
if (!empty($loggerFilename)) {
$this->loggerFilename = trim($loggerFilename);
Expand All @@ -246,7 +246,7 @@ public function getLoggerDateFormat()
/**
* Function setLoggerDateFormat - Hàm quy định Date Format cho file Log
*
* @param null $loggerDateFormat Logger Date Format, VD: Y-m-d H:i:s u
* @param string|null $loggerDateFormat Logger Date Format, VD: Y-m-d H:i:s u
*
* @return $this
* @author : 713uk13m <[email protected]>
Expand All @@ -256,7 +256,7 @@ public function getLoggerDateFormat()
* @see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#customizing-the-log-format
* @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter/LineFormatter.php
*/
public function setLoggerDateFormat($loggerDateFormat = null)
public function setLoggerDateFormat(string $loggerDateFormat = null): Logger
{
if (!empty($loggerDateFormat) && is_string($loggerDateFormat)) {
$this->loggerDateFormat = $loggerDateFormat;
Expand Down Expand Up @@ -293,7 +293,7 @@ public function getLoggerLineFormat()
* @see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#customizing-the-log-format
* @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter/LineFormatter.php
*/
public function setLoggerLineFormat($loggerLineFormat = null)
public function setLoggerLineFormat($loggerLineFormat = null): Logger
{
if (!empty($loggerLineFormat) && is_string($loggerLineFormat)) {
$this->loggerLineFormat = $loggerLineFormat;
Expand All @@ -320,11 +320,11 @@ public function setLoggerLineFormat($loggerLineFormat = null)
*
* @example log('info', 'test', 'Log Test', [])
*/
public function log($level = '', $name = 'log', $msg = 'My Message', $context = array())
public function log(string $level = '', string $name = 'log', string $msg = 'My Message', array $context = array())
{
$level = strtolower(trim($level));
if ($this->DEBUG == true) {
if (!class_exists('\Monolog\Logger')) {
if ($this->DEBUG === true) {
if (!class_exists(MonoLogger::class)) {
if (function_exists('log_message')) {
$errorMsg = 'Không tồn tại class Monolog';
log_message('error', $errorMsg);
Expand All @@ -345,7 +345,7 @@ public function log($level = '', $name = 'log', $msg = 'My Message', $context =
// Là 1 string
is_string($this->globalLoggerLevel) &&
// Và thuộc list Level được quy định
in_array($this->globalLoggerLevel, $listLevel)
in_array($this->globalLoggerLevel, $listLevel, true)
) {
// If valid globalLoggerLevel -> use globalLoggerLevel
$useLevel = strtolower($this->globalLoggerLevel);
Expand Down Expand Up @@ -394,9 +394,9 @@ public function log($level = '', $name = 'log', $msg = 'My Message', $context =
}
if (is_array($context)) {
return $logger->$level($msg, $context);
} else {
return $logger->$level($msg . json_encode($context));
}

return $logger->$level($msg . json_encode($context));
} catch (Exception $e) {
if (function_exists('log_message')) {
log_message('error', 'Error Message: ' . $e->getMessage());
Expand All @@ -423,7 +423,7 @@ public function log($level = '', $name = 'log', $msg = 'My Message', $context =
* @copyright: 713uk13m <[email protected]>
* @time : 08/17/2021 07:35
*/
public function debug($name = 'log', $msg = 'My Message', $context = array())
public function debug(string $name = 'log', string $msg = 'My Message', array $context = array())
{
return $this->log('debug', $name, $msg, $context);
}
Expand All @@ -441,7 +441,7 @@ public function debug($name = 'log', $msg = 'My Message', $context = array())
* @copyright: 713uk13m <[email protected]>
* @time : 08/17/2021 07:35
*/
public function info($name = 'log', $msg = 'My Message', $context = array())
public function info(string $name = 'log', string $msg = 'My Message', array $context = array())
{
return $this->log('info', $name, $msg, $context);
}
Expand All @@ -459,7 +459,7 @@ public function info($name = 'log', $msg = 'My Message', $context = array())
* @copyright: 713uk13m <[email protected]>
* @time : 08/17/2021 07:35
*/
public function notice($name = 'log', $msg = 'My Message', $context = array())
public function notice(string $name = 'log', string $msg = 'My Message', array $context = array())
{
return $this->log('notice', $name, $msg, $context);
}
Expand All @@ -477,7 +477,7 @@ public function notice($name = 'log', $msg = 'My Message', $context = array())
* @copyright: 713uk13m <[email protected]>
* @time : 08/17/2021 07:35
*/
public function warning($name = 'log', $msg = 'My Message', $context = array())
public function warning(string $name = 'log', string $msg = 'My Message', array $context = array())
{
return $this->log('warning', $name, $msg, $context);
}
Expand All @@ -495,7 +495,7 @@ public function warning($name = 'log', $msg = 'My Message', $context = array())
* @copyright: 713uk13m <[email protected]>
* @time : 08/17/2021 07:35
*/
public function error($name = 'log', $msg = 'My Message', $context = array())
public function error(string $name = 'log', string $msg = 'My Message', array $context = array())
{
return $this->log('error', $name, $msg, $context);
}
Expand All @@ -513,7 +513,7 @@ public function error($name = 'log', $msg = 'My Message', $context = array())
* @copyright: 713uk13m <[email protected]>
* @time : 08/17/2021 07:35
*/
public function critical($name = 'log', $msg = 'My Message', $context = array())
public function critical(string $name = 'log', string $msg = 'My Message', array $context = array())
{
return $this->log('critical', $name, $msg, $context);
}
Expand All @@ -531,7 +531,7 @@ public function critical($name = 'log', $msg = 'My Message', $context = array())
* @copyright: 713uk13m <[email protected]>
* @time : 08/17/2021 07:35
*/
public function alert($name = 'log', $msg = 'My Message', $context = array())
public function alert(string $name = 'log', string $msg = 'My Message', array $context = array())
{
return $this->log('alert', $name, $msg, $context);
}
Expand All @@ -549,7 +549,7 @@ public function alert($name = 'log', $msg = 'My Message', $context = array())
* @copyright: 713uk13m <[email protected]>
* @time : 08/17/2021 07:35
*/
public function emergency($name = 'log', $msg = 'My Message', $context = array())
public function emergency(string $name = 'log', string $msg = 'My Message', array $context = array())
{
return $this->log('emergency', $name, $msg, $context);
}
Expand Down
24 changes: 12 additions & 12 deletions src/Manager/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace nguyenanhung\MyDebug\Manager;

use Exception;
use Iterator;
use SplFileInfo;
use DateTime;
use Symfony\Component\Filesystem\Filesystem;
Expand All @@ -31,7 +32,7 @@
*/
class File extends Filesystem
{
const VERSION = '1.0.0';
const VERSION = '3.0.1';

/** @var null|array Mảng dữ liệu chứa các thuộc tính cần quét */
private $scanInclude = ['*.log', '*.txt'];
Expand All @@ -57,7 +58,7 @@ public function __construct()
* @time : 9/27/18 18:32
*
*/
public function getVersion()
public function getVersion(): string
{
return self::VERSION;
}
Expand All @@ -66,8 +67,8 @@ public function getVersion()
* Hàm quét thư mục và list ra danh sách các file con
*
* @param string $path Đường dẫn thư mục cần quét, VD: /your/to/path
* @param null|array $include Mảng dữ liệu chứa các thuộc tính cần quét
* @param null|array $exclude Mảng dữ liệu chứa các thuộc tính bỏ qua không quét
* @param array|null $include Mảng dữ liệu chứa các thuộc tính cần quét
* @param array|null $exclude Mảng dữ liệu chứa các thuộc tính bỏ qua không quét
*
* @return \Iterator
* @see https://github.com/theseer/DirectoryScanner/blob/master/samples/sample.php
Expand All @@ -76,7 +77,7 @@ public function getVersion()
* @time : 10/17/18 10:19
*
*/
public function directoryScanner($path = '', $include = NULL, $exclude = NULL)
public function directoryScanner(string $path = '', array $include = null, array $exclude = null): Iterator
{
$scanner = new DirectoryScanner();
if (is_array($include) && !empty($include)) {
Expand All @@ -102,7 +103,7 @@ public function directoryScanner($path = '', $include = NULL, $exclude = NULL)
* @time : 10/17/18 10:23
*
*/
public function setInclude($include = array())
public function setInclude(array $include = array())
{
$this->scanInclude = $include;
}
Expand All @@ -116,7 +117,7 @@ public function setInclude($include = array())
* @time : 10/17/18 10:23
*
*/
public function setExclude($exclude = array())
public function setExclude(array $exclude = array())
{
$this->scanExclude = $exclude;
}
Expand All @@ -132,7 +133,7 @@ public function setExclude($exclude = array())
* @copyright: 713uk13m <[email protected]>
* @time : 07/30/2020 59:36
*/
public function cleanLog($path = '', $dayToDelete = 3)
public function cleanLog(string $path = '', int $dayToDelete = 3)
{
try {
$getDir = $this->directoryScanner($path, $this->scanInclude, $this->scanExclude);
Expand All @@ -157,14 +158,13 @@ public function cleanLog($path = '', $dayToDelete = 3)
}

return $result;
}
catch (Exception $e) {
} catch (Exception $e) {
if (function_exists('log_message')) {
log_message('error', 'Error Message: ' . $e->getMessage());
log_message('error', 'Error Trace As String: ' . $e->getTraceAsString());
}

return FALSE;
return false;
}
}

Expand All @@ -178,7 +178,7 @@ public function cleanLog($path = '', $dayToDelete = 3)
* @copyright: 713uk13m <[email protected]>
* @time : 07/30/2020 03:15
*/
public function scanAndCleanLog($listFolder = array(), $dayToDelete = 3)
public function scanAndCleanLog(array $listFolder = array(), int $dayToDelete = 3)
{
if (empty($listFolder)) {
echo "Không có mảng dữ liệu cần quét";
Expand Down
5 changes: 3 additions & 2 deletions src/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
*/
interface Project
{
const VERSION = '3.0.0';
const VERSION = '3.0.1';

/**
* Hàm lấy thông tin phiên bản Packages
*
* @return string Phiên bản hiện tại của Packages, VD: 0.1.1
* @return string Phiên bản hiện tại của Packages, VD: 2.0.1
*
* @author : 713uk13m <[email protected]>
* @copyright: 713uk13m <[email protected]>
* @time : 9/27/18 18:32
Expand Down
Loading

0 comments on commit d5c541d

Please sign in to comment.