From 4d2fc8af52016da0bb35375b79daa3f7ee371c5e Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Wed, 25 Aug 2021 01:50:38 +0800 Subject: [PATCH] modify --- core/Abstracts/BaseObject.php | 346 +++++++++++++++++----------------- 1 file changed, 173 insertions(+), 173 deletions(-) diff --git a/core/Abstracts/BaseObject.php b/core/Abstracts/BaseObject.php index 52d357d7..c0de861a 100644 --- a/core/Abstracts/BaseObject.php +++ b/core/Abstracts/BaseObject.php @@ -11,7 +11,6 @@ namespace Kiri\Abstracts; use Exception; use JetBrains\PhpStorm\Pure; -use Kiri\Error\Logger; use Kiri\Kiri; use Swoole\Coroutine; @@ -22,208 +21,209 @@ use Swoole\Coroutine; class BaseObject implements Configure { - /** - * BaseAbstract constructor. - * - * @param array $config - * @throws Exception - */ - public function __construct(array $config = []) - { - if (!empty($config) && is_array($config)) { - Kiri::configure($this, $config); - } - } + /** + * BaseAbstract constructor. + * + * @param array $config + * @throws Exception + */ + public function __construct(array $config = []) + { + if (!empty($config) && is_array($config)) { + Kiri::configure($this, $config); + } + } - /** - * @throws Exception - */ - public function init() - { - } + /** + * @throws Exception + */ + public function init() + { + } - /** - * @param array|callable $callback - * @param object $scope - */ - public function async_create(array|callable $callback, object $scope) - { - Coroutine::create($callback, $scope); - } + /** + * @param array|callable $callback + * @param object $scope + */ + public function async_create(array|callable $callback, object $scope) + { + Coroutine::create($callback, $scope); + } - /** - * @return string - */ - #[Pure] public static function className(): string - { - return static::class; - } + /** + * @return string + */ + #[Pure] public static function className(): string + { + return static::class; + } - /** - * @param $name - * @param $value - * - * @throws Exception - */ - public function __set($name, $value) - { - $method = 'set' . ucfirst($name); - if (method_exists($this, $method)) { - $this->{$method}($value); - } else { - throw new Exception('The set name ' . $name . ' not find in class ' . static::class); - } - } + /** + * @param $name + * @param $value + * + * @throws Exception + */ + public function __set($name, $value) + { + $method = 'set' . ucfirst($name); + if (method_exists($this, $method)) { + $this->{$method}($value); + } else { + throw new Exception('The set name ' . $name . ' not find in class ' . static::class); + } + } - /** - * @param $name - * - * @return mixed - * @throws Exception - */ - public function __get($name): mixed - { - $method = 'get' . ucfirst($name); - if (method_exists($this, $method)) { - return $this->$method(); - } else { - throw new Exception('The get name ' . $name . ' not find in class ' . static::class); - } - } + /** + * @param $name + * + * @return mixed + * @throws Exception + */ + public function __get($name): mixed + { + $method = 'get' . ucfirst($name); + if (method_exists($this, $method)) { + return $this->$method(); + } else { + throw new Exception('The get name ' . $name . ' not find in class ' . static::class); + } + } - /** - * @param $message - * @param string $model - * @return bool - * @throws Exception - */ - public function addError($message, string $model = 'app'): bool - { - if ($message instanceof \Throwable) { - $Throwable = $message; - $this->error(jTraceEx($message)); + /** + * @param $message + * @param string $model + * @return bool + * @throws Exception + */ + public function addError($message, string $model = 'app'): bool + { + if ($message instanceof \Throwable) { + $Throwable = $message; + $this->error(jTraceEx($message)); - $message = 'Error: ' . $Throwable->getMessage() . PHP_EOL; - $message .= 'File: ' . $Throwable->getFile() . PHP_EOL; - $message .= 'Line: ' . $Throwable->getLine(); - } else { - if (!is_string($message)) { - $message = json_encode($message, JSON_UNESCAPED_UNICODE); - } - $this->error($message); - } - $this->logger()->error($message, $model); - return FALSE; - } + $message = 'Error: ' . $Throwable->getMessage() . PHP_EOL; + $message .= 'File: ' . $Throwable->getFile() . PHP_EOL; + $message .= 'Line: ' . $Throwable->getLine(); + } else { + if (!is_string($message)) { + $message = json_encode($message, JSON_UNESCAPED_UNICODE); + } + $this->error($message); + } + $this->logger()->error($model, [$message]); + return FALSE; + } - /** - * @return Logger - * @throws Exception - */ - private function logger(): Logger - { - return Kiri::app()->getLogger(); - } + /** + * @return Logger + * @throws Exception + */ + private function logger(): Logger + { + return Kiri::getDi()->get(Logger::class); + } - /** - * @param mixed $message - * @param string $method - * @param string $file - * @throws Exception - */ - public function debug(mixed $message, string $method = __METHOD__, string $file = __FILE__) - { - if (!is_string($message)) { - $message = print_r($message, true); - } - $message = "\033[35m[" . date('Y-m-d H:i:s') . '][DEBUG]: ' . $message . "\033[0m"; - $message .= PHP_EOL; + /** + * @param mixed $message + * @param string $method + * @param string $file + * @throws Exception + */ + public function debug(mixed $message, string $method = __METHOD__, string $file = __FILE__) + { + if (!is_string($message)) { + $message = print_r($message, true); + } + $message = "\033[35m[" . date('Y-m-d H:i:s') . '][DEBUG]: ' . $message . "\033[0m"; + $message .= PHP_EOL; - $this->logger()->output($message); - } + $this->logger()->debug(Logger::DEBUG, [$message]); + } - /** - * @param mixed $message - * @param string $method - * @param string $file - * @throws Exception - */ - public function info(mixed $message, string $method = __METHOD__, string $file = __FILE__) - { - if (!is_string($message)) { - $message = print_r($message, true); - } - $message = "\033[34m[" . date('Y-m-d H:i:s') . '][INFO]: ' . $message . "\033[0m"; - $message .= PHP_EOL; + /** + * @param mixed $message + * @param string $method + * @param string $file + * @throws Exception + */ + public function info(mixed $message, string $method = __METHOD__, string $file = __FILE__) + { + if (!is_string($message)) { + $message = print_r($message, true); + } + $message = "\033[34m[" . date('Y-m-d H:i:s') . '][INFO]: ' . $message . "\033[0m"; + $message .= PHP_EOL; - $this->logger()->output($message); - } + $this->logger()->info(Logger::NOTICE, [$message]); + } - /** - * @param mixed $message - * @param string $method - * @param string $file - * @throws Exception - */ - public function success(mixed $message, string $method = __METHOD__, string $file = __FILE__) - { - if (!is_string($message)) { - $message = print_r($message, true); - } + /** + * @param mixed $message + * @param string $method + * @param string $file + * @throws Exception + */ + public function success(mixed $message, string $method = __METHOD__, string $file = __FILE__) + { + if (!is_string($message)) { + $message = print_r($message, true); + } - $message = "\033[36m[" . date('Y-m-d H:i:s') . '][SUCCESS]: ' . $message . "\033[0m"; - $message .= PHP_EOL; + $message = "\033[36m[" . date('Y-m-d H:i:s') . '][SUCCESS]: ' . $message . "\033[0m"; + $message .= PHP_EOL; - $this->logger()->output($message); - } + $this->logger()->notice(Logger::NOTICE, [$message]); + } - /** - * @param mixed $message - * @param string $method - * @param string $file - * @throws Exception - */ - public function warning(mixed $message, string $method = __METHOD__, string $file = __FILE__) - { - if (!is_string($message)) { - $message = print_r($message, true); - } + /** + * @param mixed $message + * @param string $method + * @param string $file + * @throws Exception + */ + public function warning(mixed $message, string $method = __METHOD__, string $file = __FILE__) + { + if (!is_string($message)) { + $message = print_r($message, true); + } - $message = "\033[33m[" . date('Y-m-d H:i:s') . '][WARNING]: ' . $message . "\033[0m"; - $message .= PHP_EOL; + $message = "\033[33m[" . date('Y-m-d H:i:s') . '][WARNING]: ' . $message . "\033[0m"; + $message .= PHP_EOL; - $this->logger()->output($message); - } + $this->logger()->critical(Logger::NOTICE, [$message]); + } - /** - * @param mixed $message - * @param null $method - * @param null $file - * @throws Exception - */ - public function error(mixed $message, $method = null, $file = null) - { - if ($message instanceof \Throwable) { - $message = $message->getMessage() . " on line " . $message->getLine() . " at file " . $message->getFile(); - } - $content = (empty($method) ? '' : $method . ': ') . $message; + /** + * @param mixed $message + * @param null $method + * @param null $file + * @throws Exception + */ + public function error(mixed $message, $method = null, $file = null) + { + if ($message instanceof \Throwable) { + $message = $message->getMessage() . " on line " . $message->getLine() . " at file " . $message->getFile(); + } + $content = (empty($method) ? '' : $method . ': ') . $message; - $message = "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $content . "\033[0m"; + $message = "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $content . "\033[0m"; - if (!empty($file)) { - $message .= PHP_EOL . "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $file . "\033[0m"; - } - $this->logger()->output($message . PHP_EOL, 'error'); - } + if (!empty($file)) { + $message .= PHP_EOL . "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $file . "\033[0m"; + } + + $this->logger()->critical(Logger::ERROR, [$message]); + } }