This commit is contained in:
2020-12-15 14:04:02 +08:00
parent c2d9250be9
commit a37df0ce7a
16 changed files with 225 additions and 162 deletions
+40 -30
View File
@@ -11,17 +11,15 @@ namespace Snowflake\Abstracts;
use Exception;
use HttpServer\Client\Client;
use HttpServer\Client\HttpClient;
use HttpServer\Client\Curl;
use HttpServer\Client\Http2;
use HttpServer\Client\IClient;
use HttpServer\Http\Request;
use HttpServer\Http\Response;
use HttpServer\Route\Router;
use HttpServer\Server;
use JetBrains\PhpStorm\Pure;
use Kafka\Producer;
use Snowflake\Annotation\Annotation;
use Annotation\Annotation as SAnnotation;
use Snowflake\Cache\Memcached;
use Snowflake\Cache\Redis;
use Snowflake\Di\Service;
@@ -51,6 +49,7 @@ use Database\DatabasesProviders;
* @property Memcached $memcached
* @property Logger $logger
* @property Jwt $jwt
* @property SAnnotation $attributes
* @property Http2 $http2
* @property BaseGoto $goto
* @property Producer $kafka
@@ -82,14 +81,14 @@ abstract class BaseApplication extends Service
$this->initErrorHandler();
$this->enableEnvConfig();
Component::__construct($config);
parent::__construct($config);
}
/**
* @return mixed
* @return array
*/
public function enableEnvConfig()
public function enableEnvConfig(): array
{
if (!file_exists($this->envPath)) {
return [];
@@ -112,7 +111,7 @@ abstract class BaseApplication extends Service
*
* @return array
*/
protected function readLinesFromFile(string $filePath)
protected function readLinesFromFile(string $filePath): array
{
// Read file into an array of lines with auto-detected line endings
$autodetect = ini_get('auto_detect_line_endings');
@@ -130,7 +129,7 @@ abstract class BaseApplication extends Service
*
* @return bool
*/
protected function isComment(string $line)
protected function isComment(string $line): bool
{
$line = ltrim($line);
@@ -144,9 +143,9 @@ abstract class BaseApplication extends Service
*
* @return bool
*/
protected function looksLikeSetter(string $line)
#[Pure] protected function looksLikeSetter(string $line): bool
{
return strpos($line, '=') !== false;
return str_contains($line, '=');
}
@@ -161,7 +160,7 @@ abstract class BaseApplication extends Service
Config::set($key, $value);
}
if ($storage = Config::get('storage', false, 'storage')) {
if (strpos($storage, APP_PATH) === false) {
if (!str_contains($storage, APP_PATH)) {
$storage = APP_PATH . $storage . '/';
}
if (!is_dir($storage)) {
@@ -204,7 +203,7 @@ abstract class BaseApplication extends Service
* @return mixed
* @throws ComponentException
*/
public function clone($name)
public function clone($name): mixed
{
return clone $this->get($name);
}
@@ -221,7 +220,7 @@ abstract class BaseApplication extends Service
/**
* @return mixed
*/
public function getLocalIps()
public function getLocalIps(): mixed
{
return swoole_get_local_ip();
}
@@ -229,7 +228,7 @@ abstract class BaseApplication extends Service
/**
* @return mixed
*/
public function getFirstLocal()
public function getFirstLocal(): mixed
{
return current($this->getLocalIps());
}
@@ -249,7 +248,7 @@ abstract class BaseApplication extends Service
* @return Producer
* @throws ComponentException
*/
public function getKafka()
public function getKafka(): Producer
{
return $this->get('kafka');
}
@@ -259,7 +258,7 @@ abstract class BaseApplication extends Service
* @return \Redis|Redis
* @throws ComponentException
*/
public function getRedis()
public function getRedis(): Redis|\Redis
{
return $this->get('redis');
}
@@ -268,7 +267,7 @@ abstract class BaseApplication extends Service
* @param $ip
* @return bool
*/
public function isLocal($ip)
public function isLocal($ip): bool
{
return $this->getFirstLocal() == $ip;
}
@@ -278,7 +277,7 @@ abstract class BaseApplication extends Service
* @return ErrorHandler
* @throws ComponentException
*/
public function getError()
public function getError(): ErrorHandler
{
return $this->get('error');
}
@@ -288,7 +287,7 @@ abstract class BaseApplication extends Service
* @return Annotation
* @throws ComponentException
*/
public function getAnnotation()
public function getAnnotation(): Annotation
{
return $this->get('annotation');
}
@@ -298,7 +297,7 @@ abstract class BaseApplication extends Service
* @return Connection
* @throws ComponentException
*/
public function getConnections()
public function getConnections(): Connection
{
return $this->get('connections');
}
@@ -308,7 +307,7 @@ abstract class BaseApplication extends Service
* @return Pool
* @throws ComponentException
*/
public function getPool()
public function getPool(): Pool
{
return $this->get('pool');
}
@@ -317,7 +316,7 @@ abstract class BaseApplication extends Service
* @return Response
* @throws ComponentException
*/
public function getResponse()
public function getResponse(): Response
{
return $this->get('response');
}
@@ -326,7 +325,7 @@ abstract class BaseApplication extends Service
* @return Request
* @throws ComponentException
*/
public function getRequest()
public function getRequest(): Request
{
return $this->get('request');
}
@@ -336,7 +335,7 @@ abstract class BaseApplication extends Service
* @return Config
* @throws ComponentException
*/
public function getConfig()
public function getConfig(): Config
{
return $this->get('config');
}
@@ -346,7 +345,7 @@ abstract class BaseApplication extends Service
* @return Router
* @throws ComponentException
*/
public function getRouter()
public function getRouter(): Router
{
return $this->get('router');
}
@@ -356,7 +355,7 @@ abstract class BaseApplication extends Service
* @return Event
* @throws ComponentException
*/
public function getEvent()
public function getEvent(): Event
{
return $this->get('event');
}
@@ -366,18 +365,28 @@ abstract class BaseApplication extends Service
* @return Jwt
* @throws ComponentException
*/
public function getJwt()
public function getJwt(): Jwt
{
return $this->get('jwt');
}
/**
* @return SAnnotation
* @throws ComponentException
*/
public function getAttributes(): SAnnotation
{
return $this->get('attributes');
}
/**
* @throws Exception
*/
protected function moreComponents()
protected function moreComponents(): void
{
return $this->setComponents([
$this->setComponents([
'error' => ['class' => ErrorHandler::class],
'event' => ['class' => Event::class],
'annotation' => ['class' => Annotation::class],
@@ -388,6 +397,7 @@ abstract class BaseApplication extends Service
'request' => ['class' => Request::class],
'config' => ['class' => Config::class],
'logger' => ['class' => Logger::class],
'attributes' => ['class' => SAnnotation::class],
'router' => ['class' => Router::class],
'redis' => ['class' => Redis::class],
'jwt' => ['class' => Jwt::class],
+26 -24
View File
@@ -7,6 +7,7 @@ namespace Snowflake;
use Exception;
use HttpServer\IInterface\Task;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Abstracts\Config;
use Snowflake\Core\JSON;
@@ -21,6 +22,8 @@ use Swoole\WebSocket\Server;
defined('DB_ERROR_BUSY') or define('DB_ERROR', 'The database is busy. Please try again later.');
defined('SELECT_IS_NULL') or define('SELECT_IS_NULL', 'Query data does not exist, please check the relevant conditions.');
defined('PARAMS_IS_NULL') or define('PARAMS_IS_NULL', 'Required items cannot be empty, please add.');
defined('CONTROLLER_PATH') or define('CONTROLLER_PATH', APP_PATH . 'app/Http/Controllers/');
defined('SOCKET_PATH') or define('SOCKET_PATH', APP_PATH . 'app/Websocket/');
class Snowflake
{
@@ -46,16 +49,16 @@ class Snowflake
/**
* @return mixed
*/
public static function app()
public static function app(): Application
{
return static::$service;
}
/**
* @param $name
* @return mixed
* @return bool
*/
public static function has($name)
public static function has($name): bool
{
return static::$service->has($name);
}
@@ -67,7 +70,7 @@ class Snowflake
*/
public static function setAlias($className, $id)
{
return static::$service->setAlias($className, $id);
static::$service->setAlias($className, $id);
}
@@ -79,7 +82,7 @@ class Snowflake
* @throws ReflectionException
* @throws Exception
*/
public static function createObject($className, $construct = [])
public static function createObject($className, $construct = []): mixed
{
if (is_string($className)) {
return static::$container->get($className, $construct);
@@ -101,7 +104,7 @@ class Snowflake
* @return string
* @throws Exception
*/
public static function getStoragePath()
public static function getStoragePath(): string
{
$default = APP_PATH . 'storage' . DIRECTORY_SEPARATOR;
$path = Config::get('storage', false, $default);
@@ -115,7 +118,7 @@ class Snowflake
/**
* @return bool
*/
public static function inCoroutine()
public static function inCoroutine(): bool
{
return Coroutine::getCid() > 0;
}
@@ -124,7 +127,7 @@ class Snowflake
/**
* @return Container
*/
public static function getDi()
public static function getDi(): Container
{
return static::$container;
}
@@ -135,7 +138,7 @@ class Snowflake
* @return false|int|mixed
* @throws Exception
*/
public static function setProcessId($workerId)
public static function setProcessId($workerId): mixed
{
return self::writeFile(storage('socket.sock'), $workerId);
}
@@ -146,7 +149,7 @@ class Snowflake
* @return false|int|mixed
* @throws Exception
*/
public static function setWorkerId($workerId)
public static function setWorkerId($workerId): mixed
{
if (empty($workerId)) {
return $workerId;
@@ -171,9 +174,9 @@ class Snowflake
* @param $fileName
* @param $content
* @param null $is_append
* @return false|int|mixed
* @return mixed
*/
public static function writeFile($fileName, $content, $is_append = null)
public static function writeFile($fileName, $content, $is_append = null): mixed
{
$params = [$fileName, (string)$content];
if ($is_append !== null) {
@@ -188,7 +191,7 @@ class Snowflake
* @param $config
* @return mixed
*/
public static function configure($object, $config)
public static function configure($object, $config): mixed
{
foreach ($config as $key => $value) {
if (!property_exists($object, $key)) {
@@ -214,7 +217,7 @@ class Snowflake
* @return Server|null
* @throws
*/
public static function getWebSocket()
#[Pure] public static function getWebSocket(): ?Server
{
$server = static::app()->server->getServer();
if (!($server instanceof Server)) {
@@ -228,7 +231,7 @@ class Snowflake
* @return false|string
* @throws Exception
*/
public static function getMasterPid()
public static function getMasterPid(): bool|string
{
$default = APP_PATH . 'storage/server.pid';
$server = Config::get('settings.pid_file', false, $default);
@@ -239,10 +242,10 @@ class Snowflake
/**
* @param int $fd
* @param $data
* @return false|mixed
* @return mixed
* @throws Exception
*/
public static function push(int $fd, $data)
public static function push(int $fd, $data): mixed
{
$server = static::getWebSocket();
if (empty($server)) {
@@ -281,9 +284,8 @@ class Snowflake
/**
* @param $process
* @return mixed|void
*/
public static function shutdown($process)
public static function shutdown($process): void
{
static::app()->server->getServer()->shutdown();
if ($process instanceof Process) {
@@ -296,7 +298,7 @@ class Snowflake
* @param $tmp
* @return string
*/
public static function rename($tmp)
public static function rename($tmp): string
{
$hash = md5_file($tmp['tmp_name']);
@@ -315,9 +317,9 @@ class Snowflake
public static function isMac()
{
$output = strtolower(PHP_OS | PHP_OS_FAMILY);
if (strpos('mac', $output) !== false) {
if (str_contains('mac', $output)) {
return true;
} else if (strpos('darwin', $output) !== false) {
} else if (str_contains('darwin', $output)) {
return true;
} else {
return false;
@@ -327,7 +329,7 @@ class Snowflake
/**
* @return bool
*/
public static function isLinux()
public static function isLinux(): bool
{
if (!static::isMac()) {
return true;
@@ -340,7 +342,7 @@ class Snowflake
* @return mixed
* @throws Exception
*/
public static function reload()
public static function reload(): mixed
{
return Process::kill((int)Snowflake::getMasterPid(), SIGUSR1);
}