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

519 lines
9.5 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/10/7 0007
* Time: 2:13
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace Snowflake\Abstracts;
use Exception;
2020-09-11 19:37:32 +08:00
use HttpServer\Client\Http2;
2020-08-31 01:27:08 +08:00
use HttpServer\Http\Request;
use HttpServer\Http\Response;
use HttpServer\Route\Router;
use HttpServer\Server;
2020-12-17 14:12:44 +08:00
2020-12-29 16:32:19 +08:00
use HttpServer\Service\Http;
use HttpServer\Service\Packet;
use HttpServer\Service\Receive;
use HttpServer\Service\Websocket;
2021-02-22 10:57:12 +08:00
use JetBrains\PhpStorm\Pure;
2020-10-28 15:54:09 +08:00
use Kafka\Producer;
2020-12-15 14:04:02 +08:00
use Annotation\Annotation as SAnnotation;
2021-01-04 18:21:37 +08:00
use Snowflake\Async;
2020-08-31 13:49:40 +08:00
use Snowflake\Cache\Redis;
2020-08-31 01:27:08 +08:00
use Snowflake\Di\Service;
use Snowflake\Error\ErrorHandler;
use Snowflake\Error\Logger;
use Snowflake\Exception\ComponentException;
2020-08-31 01:54:20 +08:00
use Snowflake\Exception\InitException;
2020-09-01 13:36:57 +08:00
use Snowflake\Jwt\Jwt;
2020-08-31 01:27:08 +08:00
use Snowflake\Pool\Connection;
2021-02-22 17:44:24 +08:00
use Snowflake\Pool\ObjectPool;
2020-09-03 15:23:11 +08:00
use Snowflake\Pool\Redis as SRedis;
2020-08-31 01:27:08 +08:00
use Snowflake\Snowflake;
use Snowflake\Event;
2020-08-31 13:49:40 +08:00
use Snowflake\Pool\Pool as SPool;
2020-08-31 13:58:40 +08:00
use Database\DatabasesProviders;
2021-02-22 10:57:12 +08:00
use Swoole\Table;
2020-08-31 01:27:08 +08:00
/**
* Class BaseApplication
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Base
2020-08-31 01:27:08 +08:00
*/
abstract class BaseApplication extends Service
{
2021-02-08 17:01:03 +08:00
use TraitApplication;
2021-02-26 19:53:28 +08:00
2021-03-01 18:18:07 +08:00
private int $state = SWOOLE_WORKER_IDLE;
2021-02-26 19:53:28 +08:00
private int $taskNumber = 0;
2020-08-31 01:27:08 +08:00
/**
* @var string
*/
2020-10-28 16:01:36 +08:00
public string $storage = APP_PATH . '/storage';
2020-08-31 01:27:08 +08:00
2020-10-28 16:01:36 +08:00
public string $envPath = APP_PATH . '/.env';
2020-08-31 01:27:08 +08:00
/**
* Init constructor.
*
* @param array $config
*
* @throws
*/
public function __construct(array $config = [])
{
Snowflake::init($this);
$this->moreComponents();
$this->parseInt($config);
2020-09-07 17:08:58 +08:00
$this->parseEvents($config);
2020-08-31 01:27:08 +08:00
$this->initErrorHandler();
$this->enableEnvConfig();
2020-12-15 14:04:02 +08:00
parent::__construct($config);
2020-08-31 01:27:08 +08:00
}
2021-02-26 19:53:28 +08:00
/**
* @return bool
2021-03-01 17:36:34 +08:00
* @throws ComponentException
2021-02-26 19:53:28 +08:00
*/
public function isRun(): bool
{
2021-03-01 17:35:42 +08:00
$this->print_task_is_idle(__METHOD__);
2021-03-01 18:18:07 +08:00
return $this->state == SWOOLE_WORKER_BUSY;
2021-02-26 19:53:28 +08:00
}
2021-03-01 17:22:37 +08:00
/**
* @return $this
*/
public function stateInit(): static
2021-02-26 20:07:36 +08:00
{
$this->taskNumber = 0;
2021-03-01 18:18:07 +08:00
$this->state = SWOOLE_WORKER_IDLE;
2021-02-28 17:20:59 +08:00
return $this;
2021-02-26 20:07:36 +08:00
}
2021-03-01 17:22:37 +08:00
/**
* @return $this
2021-03-01 17:30:22 +08:00
* @throws ComponentException
2021-03-01 17:22:37 +08:00
*/
public function decrement(): static
2021-02-26 19:53:28 +08:00
{
$this->taskNumber -= 1;
if ($this->taskNumber <= 0) {
$this->taskNumber = 0;
2021-03-01 18:18:07 +08:00
$this->state = SWOOLE_WORKER_IDLE;
2021-03-01 17:36:34 +08:00
} else {
2021-03-01 18:18:07 +08:00
$this->state = SWOOLE_WORKER_BUSY;
2021-02-26 19:53:28 +08:00
}
2021-03-01 17:33:01 +08:00
return $this->print_task_is_idle(__METHOD__);
2021-03-01 17:30:22 +08:00
}
/**
2021-03-01 17:33:01 +08:00
* @param $method
* @return BaseApplication
2021-03-01 17:30:22 +08:00
* @throws ComponentException
*/
2021-03-01 17:33:01 +08:00
private function print_task_is_idle($method): static
2021-03-01 17:30:22 +08:00
{
2021-03-01 18:18:07 +08:00
$this->warning(sprintf('%s %s:%d state %d has number %d', $method, Snowflake::getEnvironmental(), env('worker'), $this->state, $this->taskNumber));
2021-02-28 17:20:59 +08:00
return $this;
2021-02-26 19:53:28 +08:00
}
2021-03-01 17:22:37 +08:00
/**
* @return $this
2021-03-01 17:30:22 +08:00
* @throws ComponentException
2021-03-01 17:22:37 +08:00
*/
public function increment(): static
2021-02-26 19:53:28 +08:00
{
$this->taskNumber += 1;
2021-02-26 20:04:16 +08:00
if ($this->taskNumber < 1) {
2021-02-26 19:53:28 +08:00
$this->taskNumber = 0;
2021-03-01 18:18:07 +08:00
$this->state = SWOOLE_WORKER_IDLE;
2021-02-26 19:53:28 +08:00
} else {
2021-03-01 18:18:07 +08:00
$this->state = SWOOLE_WORKER_BUSY;
2021-02-26 19:53:28 +08:00
}
2021-03-01 17:33:01 +08:00
return $this->print_task_is_idle(__METHOD__);
2021-02-26 19:53:28 +08:00
}
2020-08-31 01:27:08 +08:00
/**
2020-12-15 14:04:02 +08:00
* @return array
2020-08-31 01:27:08 +08:00
*/
2020-12-15 14:04:02 +08:00
public function enableEnvConfig(): array
2020-08-31 01:27:08 +08:00
{
if (!file_exists($this->envPath)) {
return [];
}
$lines = $this->readLinesFromFile($this->envPath);
foreach ($lines as $line) {
if (!$this->isComment($line) && $this->looksLikeSetter($line)) {
[$key, $value] = explode('=', $line);
putenv(trim($key) . '=' . trim($value));
}
}
return $lines;
}
/**
* Read lines from the file, auto detecting line endings.
*
* @param string $filePath
*
* @return array
*/
2020-12-15 14:04:02 +08:00
protected function readLinesFromFile(string $filePath): array
2020-08-31 01:27:08 +08:00
{
// Read file into an array of lines with auto-detected line endings
$autodetect = ini_get('auto_detect_line_endings');
ini_set('auto_detect_line_endings', '1');
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
ini_set('auto_detect_line_endings', $autodetect);
return $lines;
}
/**
* Determine if the line in the file is a comment, e.g. begins with a #.
*
* @param string $line
*
* @return bool
*/
2020-12-15 14:04:02 +08:00
protected function isComment(string $line): bool
2020-08-31 01:27:08 +08:00
{
$line = ltrim($line);
return isset($line[0]) && $line[0] === '#';
}
/**
* Determine if the given line looks like it's setting a variable.
*
* @param string $line
*
* @return bool
*/
2021-02-22 10:57:12 +08:00
#[Pure] protected function looksLikeSetter(string $line): bool
2020-08-31 01:27:08 +08:00
{
2020-12-15 14:04:02 +08:00
return str_contains($line, '=');
2020-08-31 01:27:08 +08:00
}
/**
* @param $config
*
* @throws
*/
public function parseInt($config)
{
foreach ($config as $key => $value) {
Config::set($key, $value);
}
2020-08-31 01:54:20 +08:00
if ($storage = Config::get('storage', false, 'storage')) {
2020-12-15 14:04:02 +08:00
if (!str_contains($storage, APP_PATH)) {
2020-08-31 02:08:23 +08:00
$storage = APP_PATH . $storage . '/';
2020-08-31 01:54:20 +08:00
}
if (!is_dir($storage)) {
2020-08-31 02:08:57 +08:00
mkdir($storage);
2020-08-31 01:54:20 +08:00
}
if (!is_dir($storage) || !is_writeable($storage)) {
throw new InitException("Directory {$storage} does not have write permission");
}
2020-08-31 01:27:08 +08:00
}
}
2020-09-07 17:08:58 +08:00
/**
* @param $config
*
* @throws
*/
public function parseEvents($config)
{
if (!isset($config['events']) || !is_array($config['events'])) {
return;
}
2021-02-20 13:02:58 +08:00
$event = Snowflake::app()->getEvent();
2020-09-07 17:08:58 +08:00
foreach ($config['events'] as $key => $value) {
if (is_string($value)) {
if (!class_exists($value)) {
throw new InitException("Class {$value} does not exists.");
}
$value = Snowflake::createObject($value);
} else if (is_array($value) && !is_callable($value, true)) {
throw new InitException("Class does not hav callback.");
}
$event->on($key, $value);
}
}
2020-08-31 01:27:08 +08:00
/**
* @param $name
* @return mixed
* @throws ComponentException
*/
2020-12-15 14:04:02 +08:00
public function clone($name): mixed
2020-08-31 01:27:08 +08:00
{
return clone $this->get($name);
}
/**
*
* @throws Exception
*/
public function initErrorHandler()
{
$this->get('error')->register();
}
/**
* @return mixed
*/
2020-12-15 14:04:02 +08:00
public function getLocalIps(): mixed
2020-08-31 01:27:08 +08:00
{
return swoole_get_local_ip();
}
/**
* @return mixed
*/
2020-12-15 14:04:02 +08:00
public function getFirstLocal(): mixed
2020-08-31 01:27:08 +08:00
{
return current($this->getLocalIps());
}
2020-08-31 12:38:32 +08:00
/**
* @return Logger
* @throws ComponentException
*/
public function getLogger(): Logger
{
return $this->get('logger');
}
2020-10-28 16:10:23 +08:00
/**
* @return Producer
* @throws ComponentException
*/
2020-12-15 14:04:02 +08:00
public function getKafka(): Producer
2020-10-28 16:10:23 +08:00
{
return $this->get('kafka');
}
2020-08-31 13:49:40 +08:00
/**
2020-09-08 15:21:58 +08:00
* @return \Redis|Redis
2020-08-31 13:49:40 +08:00
* @throws ComponentException
*/
2020-12-15 14:04:02 +08:00
public function getRedis(): Redis|\Redis
2020-08-31 13:49:40 +08:00
{
return $this->get('redis');
}
2020-08-31 01:27:08 +08:00
/**
* @param $ip
* @return bool
*/
2020-12-15 14:04:02 +08:00
public function isLocal($ip): bool
2020-08-31 01:27:08 +08:00
{
return $this->getFirstLocal() == $ip;
}
2020-09-01 13:39:50 +08:00
/**
* @return ErrorHandler
* @throws ComponentException
*/
2020-12-15 14:04:02 +08:00
public function getError(): ErrorHandler
2020-09-01 13:39:50 +08:00
{
return $this->get('error');
}
/**
2020-09-01 13:41:18 +08:00
* @return Connection
2020-09-01 13:39:50 +08:00
* @throws ComponentException
*/
2020-12-15 14:04:02 +08:00
public function getConnections(): Connection
2020-09-01 13:39:50 +08:00
{
return $this->get('connections');
}
/**
2021-01-12 18:13:02 +08:00
* @return SPool
2020-09-01 13:39:50 +08:00
* @throws ComponentException
*/
2021-01-12 18:13:02 +08:00
public function getPool(): SPool
2020-09-01 13:39:50 +08:00
{
return $this->get('pool');
}
/**
2020-09-01 13:41:18 +08:00
* @return Response
2020-09-01 13:39:50 +08:00
* @throws ComponentException
*/
2020-12-15 14:04:02 +08:00
public function getResponse(): Response
2020-09-01 13:39:50 +08:00
{
return $this->get('response');
}
/**
2020-09-01 13:41:18 +08:00
* @return Request
2020-09-01 13:39:50 +08:00
* @throws ComponentException
*/
2020-12-15 14:04:02 +08:00
public function getRequest(): Request
2020-09-01 13:39:50 +08:00
{
return $this->get('request');
}
2021-02-22 10:57:12 +08:00
/**
* @param $name
* @return Table
* @throws ComponentException
*/
public function getTable($name): Table
{
return $this->get($name);
}
2020-09-01 13:39:50 +08:00
/**
2020-09-01 13:41:18 +08:00
* @return Config
2020-09-01 13:39:50 +08:00
* @throws ComponentException
*/
2020-12-15 14:04:02 +08:00
public function getConfig(): Config
2020-09-01 13:39:50 +08:00
{
return $this->get('config');
}
/**
2020-09-01 13:41:18 +08:00
* @return Router
2020-09-01 13:39:50 +08:00
* @throws ComponentException
*/
2020-12-15 14:04:02 +08:00
public function getRouter(): Router
2020-09-01 13:39:50 +08:00
{
return $this->get('router');
}
2020-09-02 15:45:52 +08:00
/**
* @return Event
* @throws ComponentException
*/
2020-12-15 14:04:02 +08:00
public function getEvent(): Event
2020-09-02 15:45:52 +08:00
{
return $this->get('event');
}
2020-09-01 13:39:50 +08:00
/**
2020-09-01 13:41:18 +08:00
* @return Jwt
2020-09-01 13:39:50 +08:00
* @throws ComponentException
*/
2020-12-15 14:04:02 +08:00
public function getJwt(): Jwt
2020-09-01 13:39:50 +08:00
{
return $this->get('jwt');
}
2020-12-29 16:32:19 +08:00
/**
* @return Server
* @throws ComponentException
*/
public function getServer(): Server
{
return $this->get('server');
}
/**
* @return Http|Packet|Receive|Websocket|null
* @throws ComponentException
*/
2021-01-28 11:27:53 +08:00
public function getSwoole(): Packet|Websocket|Receive|Http|null
2020-12-29 16:32:19 +08:00
{
return $this->getServer()->getServer();
}
2020-12-15 14:04:02 +08:00
/**
* @return SAnnotation
* @throws ComponentException
*/
public function getAttributes(): SAnnotation
{
return $this->get('attributes');
}
2021-01-04 18:21:37 +08:00
/**
* @return Async
* @throws ComponentException
*/
public function getAsync(): Async
{
return $this->get('async');
}
2021-02-22 17:44:24 +08:00
/**
* @return ObjectPool
* @throws ComponentException
*/
public function getObject(): ObjectPool
{
return $this->get('object');
}
2020-08-31 01:27:08 +08:00
/**
* @throws Exception
*/
2020-12-15 14:04:02 +08:00
protected function moreComponents(): void
2020-08-31 01:27:08 +08:00
{
2020-12-15 14:04:02 +08:00
$this->setComponents([
2020-08-31 01:27:08 +08:00
'error' => ['class' => ErrorHandler::class],
'event' => ['class' => Event::class],
'connections' => ['class' => Connection::class],
2020-09-03 15:23:11 +08:00
'redis_connections' => ['class' => SRedis::class],
2020-08-31 13:49:40 +08:00
'pool' => ['class' => SPool::class],
2020-08-31 01:27:08 +08:00
'response' => ['class' => Response::class],
'request' => ['class' => Request::class],
'config' => ['class' => Config::class],
'logger' => ['class' => Logger::class],
2020-12-15 14:04:02 +08:00
'attributes' => ['class' => SAnnotation::class],
2020-08-31 01:27:08 +08:00
'router' => ['class' => Router::class],
2020-08-31 13:49:40 +08:00
'redis' => ['class' => Redis::class],
2020-09-10 15:49:42 +08:00
'jwt' => ['class' => Jwt::class],
2021-01-04 18:21:37 +08:00
'async' => ['class' => Async::class],
2021-02-22 17:44:24 +08:00
'object' => ['class' => ObjectPool::class],
2020-11-01 05:12:31 +08:00
'goto' => ['class' => BaseGoto::class],
2020-11-17 10:44:29 +08:00
'http2' => ['class' => Http2::class],
2020-08-31 01:27:08 +08:00
]);
}
}