Files
kiri-core/System/Abstracts/BaseObject.php
T

221 lines
4.7 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/30 0030
* Time: 14:10
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-11-18 15:09:25 +08:00
2020-08-31 01:27:08 +08:00
namespace Snowflake\Abstracts;
use Exception;
2020-12-17 14:12:44 +08:00
2021-02-22 19:07:34 +08:00
use JetBrains\PhpStorm\Pure;
2020-08-31 01:27:08 +08:00
use Snowflake\Error\Logger;
2021-02-23 11:15:47 +08:00
use Snowflake\Exception\ComponentException;
2020-08-31 01:27:08 +08:00
use Snowflake\Snowflake;
/**
* Class BaseObject
* @method defer()
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Base
2020-08-31 01:27:08 +08:00
* @method afterInit
* @method initialization
*/
class BaseObject implements Configure
{
/**
* BaseAbstract constructor.
*
* @param array $config
*/
public function __construct($config = [])
{
2020-09-02 15:38:04 +08:00
if (!empty($config) && is_array($config)) {
Snowflake::configure($this, $config);
}
2020-08-31 01:27:08 +08:00
$this->init();
}
public function init()
{
}
/**
* @return string
*/
2021-02-22 19:07:34 +08:00
#[Pure] public static function className(): string
2020-08-31 01:27:08 +08:00
{
return get_called_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 {
$this->error('set ' . $name . ' not exists ' . get_called_class());
throw new Exception('The set name ' . $name . ' not find in class ' . get_class($this));
}
}
/**
* @param $name
*
* @return mixed
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function __get($name): mixed
2020-08-31 01:27:08 +08:00
{
$method = 'get' . ucfirst($name);
if (method_exists($this, $method)) {
return $this->$method();
} else {
throw new Exception('The get name ' . $name . ' not find in class ' . get_class($this));
}
}
/**
* @param $message
* @param string $model
* @return bool
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function addError($message, $model = 'app'): bool
2020-08-31 01:27:08 +08:00
{
2020-11-06 16:47:17 +08:00
if ($message instanceof \Throwable) {
2021-02-20 15:47:23 +08:00
$format = 'Error: ' . $message->getMessage() . PHP_EOL;
$format .= 'File: ' . $message->getFile() . PHP_EOL;
$format .= 'Line: ' . $message->getLine();
$this->error($format);
2020-08-31 01:27:08 +08:00
} else {
if (!is_string($message)) {
$message = json_encode($message, JSON_UNESCAPED_UNICODE);
}
$this->error($message);
}
2020-11-26 11:37:10 +08:00
$logger = Snowflake::app()->getLogger();
$logger->error($message, $model);
2020-08-31 01:27:08 +08:00
return FALSE;
}
/**
2020-10-14 11:13:26 +08:00
* @param mixed $message
2020-08-31 01:27:08 +08:00
* @param string $method
* @param string $file
2021-02-23 11:15:47 +08:00
* @throws ComponentException
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function debug(mixed $message, string $method = __METHOD__, string $file = __FILE__)
2020-08-31 01:27:08 +08:00
{
if (!is_string($message)) {
$message = print_r($message, true);
}
2021-02-23 11:09:31 +08:00
$message = "\033[35m[DEBUG][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
$message .= PHP_EOL;
2021-02-23 11:15:47 +08:00
$socket = Snowflake::app()->getLogger();
$socket->output($message);
2020-08-31 01:27:08 +08:00
}
/**
2020-10-14 11:13:26 +08:00
* @param mixed $message
2020-08-31 01:27:08 +08:00
* @param string $method
* @param string $file
2021-02-23 11:15:47 +08:00
* @throws ComponentException
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function info(mixed $message, string $method = __METHOD__, string $file = __FILE__)
2020-08-31 01:27:08 +08:00
{
if (!is_string($message)) {
$message = print_r($message, true);
}
2021-02-23 11:09:31 +08:00
$message = "\033[34m[INFO][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
$message .= PHP_EOL;
2021-02-23 11:15:47 +08:00
$socket = Snowflake::app()->getLogger();
$socket->output($message);
2020-08-31 01:27:08 +08:00
}
/**
2020-10-14 11:13:26 +08:00
* @param mixed $message
2020-08-31 01:27:08 +08:00
* @param string $method
* @param string $file
2021-02-23 11:15:47 +08:00
* @throws ComponentException
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function success(mixed $message, string $method = __METHOD__, string $file = __FILE__)
2020-08-31 01:27:08 +08:00
{
if (!is_string($message)) {
$message = print_r($message, true);
}
2021-02-23 11:09:31 +08:00
$message = "\033[36m[SUCCESS][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
$message .= PHP_EOL;
2021-02-23 11:15:47 +08:00
$socket = Snowflake::app()->getLogger();
$socket->output($message);
2020-08-31 01:27:08 +08:00
}
/**
2020-10-14 11:13:26 +08:00
* @param mixed $message
2020-08-31 01:27:08 +08:00
* @param string $method
* @param string $file
2021-02-23 11:15:47 +08:00
* @throws ComponentException
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function warning(mixed $message, string $method = __METHOD__, string $file = __FILE__)
2020-08-31 01:27:08 +08:00
{
if (!is_string($message)) {
$message = print_r($message, true);
}
2021-02-23 11:09:31 +08:00
$message = "\033[33m[WARNING][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
$message .= PHP_EOL;
2021-02-23 11:15:47 +08:00
$socket = Snowflake::app()->getLogger();
$socket->output($message);
2020-08-31 01:27:08 +08:00
}
/**
2020-09-18 18:03:24 +08:00
* @param mixed $message
2020-10-29 18:17:25 +08:00
* @param null $method
* @param null $file
2021-02-23 11:15:47 +08:00
* @throws ComponentException
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function error(mixed $message, $method = null, $file = null)
2020-08-31 01:27:08 +08:00
{
if (!empty($file)) {
echo "\033[41;37m[ERROR][" . date('Y-m-d H:i:s') . ']: ' . $file . "\033[0m";
echo PHP_EOL;
}
if (!is_string($message)) {
$message = print_r($message, true);
}
2021-02-20 16:34:20 +08:00
$length = strlen('[ERROR][2021-02-20 08:32:02]:');
2021-02-20 16:36:13 +08:00
$message = (empty($method) ? '' : $method . ': ') . $message;
2021-02-20 16:34:20 +08:00
2021-02-23 11:09:31 +08:00
$message = "\033[41;37m[ERROR][" . date('Y-m-d H:i:s') . ']: ' . PHP_EOL .
2021-02-20 16:34:20 +08:00
str_pad($message, $length, ' ', STR_PAD_LEFT) . "\033[0m";
2021-02-23 11:09:31 +08:00
$message .= PHP_EOL;
2021-02-23 11:15:47 +08:00
$socket = Snowflake::app()->getLogger();
$socket->output($message);
2020-08-31 01:27:08 +08:00
}
}