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

387 lines
7.3 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
*/
namespace Snowflake\Abstracts;
use Exception;
2020-09-11 19:37:32 +08:00
use HttpServer\Client\Client;
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-10-28 15:54:09 +08:00
use Kafka\Producer;
2020-08-31 01:27:08 +08:00
use Snowflake\Annotation\Annotation;
2020-09-03 15:23:11 +08:00
use Snowflake\Cache\Memcached;
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;
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;
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
* @property $json
* @property Annotation $annotation
* @property Event $event
* @property Router $router
2020-08-31 13:49:40 +08:00
* @property SPool $pool
2020-09-08 15:21:58 +08:00
* @property \Redis|Redis $redis
2020-09-02 11:46:19 +08:00
* @property Server $server
2020-08-31 13:58:40 +08:00
* @property DatabasesProviders $db
2020-08-31 01:27:08 +08:00
* @property Connection $connections
2020-09-03 15:23:11 +08:00
* @property Memcached $memcached
2020-08-31 12:38:32 +08:00
* @property Logger $logger
2020-09-01 13:36:57 +08:00
* @property Jwt $jwt
2020-09-10 15:49:42 +08:00
* @property BaseGoto $goto
2020-09-11 19:37:32 +08:00
* @property Client $client
2020-10-28 15:54:09 +08:00
* @property Producer $kafka
2020-08-31 01:27:08 +08:00
*/
abstract class BaseApplication extends Service
{
/**
* @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();
Component::__construct($config);
}
/**
* @return mixed
*/
public function enableEnvConfig()
{
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-09-11 19:37:32 +08:00
protected function readLinesFromFile(string $filePath)
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-09-11 19:37:32 +08:00
protected function isComment(string $line)
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
*/
2020-09-11 19:37:32 +08:00
protected function looksLikeSetter(string $line)
2020-08-31 01:27:08 +08:00
{
return strpos($line, '=') !== false;
}
/**
* @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')) {
if (strpos($storage, APP_PATH) === false) {
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;
}
$event = Snowflake::app()->event;
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
*/
public function clone($name)
{
return clone $this->get($name);
}
/**
*
* @throws Exception
*/
public function initErrorHandler()
{
$this->get('error')->register();
}
/**
* @return mixed
*/
public function getLocalIps()
{
return swoole_get_local_ip();
}
/**
* @return mixed
*/
public function getFirstLocal()
{
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-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-09-01 13:42:20 +08:00
public function getRedis()
2020-08-31 13:49:40 +08:00
{
return $this->get('redis');
}
2020-08-31 01:27:08 +08:00
/**
* @param $ip
* @return bool
*/
public function isLocal($ip)
{
return $this->getFirstLocal() == $ip;
}
2020-09-01 13:39:50 +08:00
/**
* @return ErrorHandler
* @throws ComponentException
*/
public function getError()
{
return $this->get('error');
}
/**
2020-09-01 13:41:18 +08:00
* @return Annotation
2020-09-01 13:39:50 +08:00
* @throws ComponentException
*/
public function getAnnotation()
{
return $this->get('annotation');
}
/**
2020-09-01 13:41:18 +08:00
* @return Connection
2020-09-01 13:39:50 +08:00
* @throws ComponentException
*/
public function getConnections()
{
return $this->get('connections');
}
/**
2020-09-01 13:41:18 +08:00
* @return Pool
2020-09-01 13:39:50 +08:00
* @throws ComponentException
*/
public function getPool()
{
return $this->get('pool');
}
/**
2020-09-01 13:41:18 +08:00
* @return Response
2020-09-01 13:39:50 +08:00
* @throws ComponentException
*/
public function getResponse()
{
return $this->get('response');
}
/**
2020-09-01 13:41:18 +08:00
* @return Request
2020-09-01 13:39:50 +08:00
* @throws ComponentException
*/
public function getRequest()
{
return $this->get('request');
}
/**
2020-09-01 13:41:18 +08:00
* @return Config
2020-09-01 13:39:50 +08:00
* @throws ComponentException
*/
public function getConfig()
{
return $this->get('config');
}
/**
2020-09-01 13:41:18 +08:00
* @return Router
2020-09-01 13:39:50 +08:00
* @throws ComponentException
*/
public function getRouter()
{
return $this->get('router');
}
2020-09-02 15:45:52 +08:00
/**
* @return Event
* @throws ComponentException
*/
public function getEvent()
{
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
*/
public function getJwt()
{
return $this->get('jwt');
}
2020-08-31 01:27:08 +08:00
/**
* @throws Exception
*/
protected function moreComponents()
{
return $this->setComponents([
'error' => ['class' => ErrorHandler::class],
'event' => ['class' => Event::class],
'annotation' => ['class' => Annotation::class],
2020-09-11 19:37:32 +08:00
'client' => ['class' => Client::class],
'http2' => ['class' => Http2::class],
2020-08-31 01:27:08 +08:00
'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],
'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],
'goto' => ['class' => BaseGoto::class]
2020-08-31 01:27:08 +08:00
]);
}
}