This commit is contained in:
as2252258@163.com
2021-08-09 00:15:03 +08:00
parent 8c74ed4d00
commit 94a9184bf0
8 changed files with 705 additions and 804 deletions
+1 -1
View File
@@ -211,7 +211,7 @@ class Connection extends Component
*/ */
private function connections(): \Snowflake\Pool\Connection private function connections(): \Snowflake\Pool\Connection
{ {
return Snowflake::app()->getMysqlFromPool(); return Snowflake::getDi()->get(\Snowflake\Pool\Connection::class);
} }
+12 -36
View File
@@ -11,6 +11,7 @@ namespace Snowflake\Abstracts;
use Annotation\Annotation as SAnnotation; use Annotation\Annotation as SAnnotation;
use Database\Connection;
use Exception; use Exception;
use HttpServer\Client\Http2; use HttpServer\Client\Http2;
use HttpServer\Http\HttpHeaders; use HttpServer\Http\HttpHeaders;
@@ -24,20 +25,19 @@ use HttpServer\Shutdown;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Kafka\KafkaProvider; use Kafka\KafkaProvider;
use ReflectionException; use ReflectionException;
use Rpc\Producer;
use Rpc\Service;
use Server\ServerManager; use Server\ServerManager;
use Snowflake\Aop; use Snowflake\Aop;
use Snowflake\Async; use Snowflake\Async;
use Snowflake\Cache\Redis; use Snowflake\Cache\Redis;
use Snowflake\Di\Service; use Snowflake\Di\LocalService;
use Snowflake\Error\ErrorHandler; use Snowflake\Error\ErrorHandler;
use Snowflake\Error\Logger; use Snowflake\Error\Logger;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Exception\InitException; use Snowflake\Exception\InitException;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Jwt\Jwt; use Snowflake\Jwt\Jwt;
use Snowflake\Pool\Connection;
use Snowflake\Pool\Pool;
use Snowflake\Pool\Redis as SRedis;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Table; use Swoole\Table;
@@ -269,7 +269,7 @@ abstract class BaseApplication extends Component
*/ */
public function get($name): mixed public function get($name): mixed
{ {
return di(Service::class)->get($name); return di(LocalService::class)->get($name);
} }
@@ -329,26 +329,6 @@ abstract class BaseApplication extends Component
} }
/**
* @return Connection
* @throws Exception
*/
public function getMysqlFromPool(): Connection
{
return $this->get('connections');
}
/**
* @return SRedis
* @throws Exception
*/
public function getRedisFromPool(): SRedis
{
return $this->get('redis_connections');
}
/** /**
* @param $name * @param $name
* @return Table * @return Table
@@ -433,7 +413,7 @@ abstract class BaseApplication extends Component
* @return \Rpc\Producer * @return \Rpc\Producer
* @throws Exception * @throws Exception
*/ */
public function getRpc(): \Rpc\Producer public function getRpc(): Producer
{ {
return $this->get('rpc'); return $this->get('rpc');
} }
@@ -446,7 +426,7 @@ abstract class BaseApplication extends Component
*/ */
private function setComponents($array): void private function setComponents($array): void
{ {
di(Service::class)->setComponents($array); di(LocalService::class)->setComponents($array);
} }
@@ -458,7 +438,7 @@ abstract class BaseApplication extends Component
*/ */
public function set($id, $definition): void public function set($id, $definition): void
{ {
di(Service::class)->set($id, $definition); di(LocalService::class)->set($id, $definition);
} }
@@ -470,7 +450,7 @@ abstract class BaseApplication extends Component
*/ */
public function has($id): bool public function has($id): bool
{ {
return di(Service::class)->has($id); return di(LocalService::class)->has($id);
} }
@@ -481,27 +461,23 @@ abstract class BaseApplication extends Component
{ {
$this->setComponents([ $this->setComponents([
'error' => ['class' => ErrorHandler::class], 'error' => ['class' => ErrorHandler::class],
'connections' => ['class' => Connection::class],
'redis_connections' => ['class' => SRedis::class],
'config' => ['class' => Config::class], 'config' => ['class' => Config::class],
'logger' => ['class' => Logger::class], 'logger' => ['class' => Logger::class],
'annotation' => ['class' => SAnnotation::class], 'annotation' => ['class' => SAnnotation::class],
'router' => ['class' => Router::class], 'router' => ['class' => Router::class],
'event' => ['class' => Event::class], 'event' => ['class' => Event::class],
'redis' => ['class' => Redis::class], 'redis' => ['class' => Redis::class],
'databases' => ['class' => \Database\Connection::class], 'databases' => ['class' => Connection::class],
'aop' => ['class' => Aop::class], 'aop' => ['class' => Aop::class],
'input' => ['class' => HttpParams::class], 'input' => ['class' => HttpParams::class],
'header' => ['class' => HttpHeaders::class], 'header' => ['class' => HttpHeaders::class],
'jwt' => ['class' => Jwt::class], 'jwt' => ['class' => Jwt::class],
'async' => ['class' => Async::class], 'async' => ['class' => Async::class],
'kafka-container' => ['class' => KafkaProvider::class], 'kafka-container' => ['class' => KafkaProvider::class],
'filter' => ['class' => HttpFilter::class],
'goto' => ['class' => BaseGoto::class],
'response' => ['class' => Response::class], 'response' => ['class' => Response::class],
'request' => ['class' => Request::class], 'request' => ['class' => Request::class],
'rpc' => ['class' => \Rpc\Producer::class], 'rpc' => ['class' => Producer::class],
'rpc-service' => ['class' => \Rpc\Service::class], 'rpc-service' => ['class' => Service::class],
'http2' => ['class' => Http2::class], 'http2' => ['class' => Http2::class],
'shutdown' => ['class' => Shutdown::class], 'shutdown' => ['class' => Shutdown::class],
]); ]);
-1
View File
@@ -35,7 +35,6 @@ use Snowflake\Pool\Pool;
* @property Request $request * @property Request $request
* @property DatabasesProviders $db * @property DatabasesProviders $db
* @property Async $async * @property Async $async
* @property Connection $connections
* @property Logger $logger * @property Logger $logger
* @property Jwt $jwt * @property Jwt $jwt
* @property SAnnotation $annotation * @property SAnnotation $annotation
+5 -5
View File
@@ -19,11 +19,11 @@ use Snowflake\Core\Json;
use Snowflake\Events\EventProvider; use Snowflake\Events\EventProvider;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Snowflake\Pool\Redis as PoolRedis;
/** /**
* Class Redis * Class Redis
* @package Snowflake\Snowflake\Cache * @package Snowflake\Snowflake\Cache
* @see \Redis
* @mixin \Redis * @mixin \Redis
*/ */
class Redis extends Component class Redis extends Component
@@ -42,7 +42,7 @@ class Redis extends Component
*/ */
public function init() public function init()
{ {
$connections = Snowflake::app()->getRedisFromPool(); $connections = Snowflake::getDi()->get(PoolRedis::class);
$config = $this->get_config(); $config = $this->get_config();
@@ -114,7 +114,7 @@ SCRIPT;
*/ */
public function release() public function release()
{ {
$connections = Snowflake::app()->getRedisFromPool(); $connections = Snowflake::getDi()->get(PoolRedis::class);
$connections->release($this->get_config(), true); $connections->release($this->get_config(), true);
} }
@@ -125,7 +125,7 @@ SCRIPT;
*/ */
public function destroy() public function destroy()
{ {
$connections = Snowflake::app()->getRedisFromPool(); $connections = Snowflake::getDi()->get(PoolRedis::class);
$connections->destroy($this->get_config(), true); $connections->destroy($this->get_config(), true);
} }
@@ -135,7 +135,7 @@ SCRIPT;
*/ */
public function proxy(): \Redis public function proxy(): \Redis
{ {
$connections = Snowflake::app()->getRedisFromPool(); $connections = Snowflake::getDi()->get(PoolRedis::class);
$config = $this->get_config(); $config = $this->get_config();
+86
View File
@@ -0,0 +1,86 @@
<?php
namespace Snowflake\Di;
use Annotation\Inject;
use Snowflake\Abstracts\Component;
use Snowflake\Snowflake;
/**
* 服务定位器
*/
class LocalService extends Component
{
private array $_components = [];
private array $_definition = [];
/**
* @param $name
* @param $define
*/
public function set($name, $define)
{
unset($this->_components[$name]);
$this->_definition[$name] = $define;
}
/**
* @throws \Exception
*/
public function get(string $name, $throwException = true)
{
if (isset($this->_components[$name])) {
return $this->_components[$name];
}
if (isset($this->_definitions[$name])) {
$definition = $this->_definitions[$name];
if (is_object($definition) && !$definition instanceof \Closure) {
return $this->_components[$name] = $definition;
}
return $this->_components[$name] = Snowflake::createObject($definition);
} else if ($throwException) {
throw new \Exception("Unknown component ID: $name");
}
return null;
}
/**
* @param array $components
*/
public function setComponents(array $components)
{
foreach ($components as $name => $component) {
$this->set($name, $component);
}
}
/**
* @param $id
* @return bool
*/
public function has($id): bool
{
return isset($this->_components[$id], $this->_definition[$id]);
}
/**
* @param $id
*/
public function remove($id): void
{
unset($this->_components[$id], $this->_definition[$id]);
}
}
-165
View File
@@ -1,165 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/25 0025
* Time: 18:29
*/
declare(strict_types=1);
namespace Snowflake\Di;
use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Component;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake;
/**
* Class Service
* @package Snowflake\Snowflake\Di
*/
class Service extends Component
{
private array $_components = [];
private array $_definition = [];
private array $_ids = [];
protected array $_alias = [];
/**
* @param $id
* @param bool $try
* @return mixed
* @throws Exception
*/
public function get($id, bool $try = true): mixed
{
if (isset($this->_components[$id])) {
return $this->_components[$id];
}
$config = $this->_getConfig($id, $try);
if (!is_object($config)) {
$config = Snowflake::createObject($config);
}
return $this->_components[$id] = $config;
}
/**
* @param $id
* @param $try
* @return mixed
* @throws ComponentException
*/
private function _getConfig($id, $try): mixed
{
$config = $this->_definition[$id] ?? $this->_alias[$id] ?? null;
if (is_null($config)) {
if ($try === false) {
return null;
}
throw new ComponentException("Unknown component ID: $id");
}
return $config;
}
/**
* @param string $className
* @param string $alias
*/
public function setAlias(string $className, string $alias)
{
$this->_alias[$className] = $alias;
}
/**
* @param $id
* @param $definition
*
* @return mixed
* @throws Exception
*/
public function set($id, $definition): void
{
if ($definition === NULL) {
$this->remove($id);
return;
}
$this->_ids[] = $id;
unset($this->_components[$id]);
if (is_object($definition)) {
$this->_components[$id] = $definition;
$this->_definition[$id] = $definition;
} else if (!is_array($definition)) {
throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
} else {
$this->_definition[$id] = $definition;
}
}
/**
* @param $id
* @return bool
*/
#[Pure] public function has($id): bool
{
return in_array($id, $this->_ids);
}
/**
* @param array $data
* @throws Exception
*/
public function setComponents(array $data)
{
foreach ($data as $key => $val) {
$this->set($key, $val);
}
}
/**
* @param $name
* @return mixed
* @throws Exception
*/
public function __get($name): mixed
{
if ($this->has($name)) {
return $this->get($name);
}
return parent::__get($name);
}
/**
* @param $id
* @return bool
*/
public function remove($id): bool
{
$component = $this->_components[$id];
$className = $component::class;
unset($component, $this->_components[$id]);
unset($this->_definition[$id]);
if (isset($this->_alias[$id])) {
unset($this->_components[$this->_alias[$id]]);
unset($this->_definition[$this->_alias[$id]]);
unset($this->_alias[$id]);
}
Snowflake::getDi()->unset($className);
return $this->has($id);
}
}
+10
View File
@@ -23,6 +23,9 @@ trait JwtHelper
private string $secret = ''; private string $secret = '';
private string $scene = 'default';
private string $encrypt = 'sha256'; private string $encrypt = 'sha256';
@@ -81,6 +84,13 @@ mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY=
$this->timeout = $timeout; $this->timeout = $timeout;
} }
public function setScene(string $scene)
{
$this->scene = $scene;
}
/** /**
* @param string $timeout * @param string $timeout
*/ */
+8 -13
View File
@@ -84,12 +84,11 @@ class Snowflake
/** /**
* @param $alias * @param $alias
* @param array $array * @param array $array
* @return mixed
* @throws Exception * @throws Exception
*/ */
public static function set($alias, array $array = []): mixed public static function set($alias, array $array = [])
{ {
return static::app()->set($alias, $array); static::app()->set($alias, $array);
} }
@@ -189,22 +188,18 @@ class Snowflake
*/ */
public static function createObject($className, array $construct = []): mixed public static function createObject($className, array $construct = []): mixed
{ {
if (is_object($className)) { if (is_callable($className, TRUE)) {
return $className; return call_user_func($className, $construct);
} } else if (is_string($className) && class_exists($className)) {
if (is_string($className)) {
return static::$container->get($className, $construct); return static::$container->get($className, $construct);
} else if (is_array($className)) { } else if (is_array($className) && isset($class['class'])) {
if (!isset($className['class']) || empty($className['class'])) {
throw new Exception('Object configuration must be an array containing a "class" element.');
}
$class = $className['class']; $class = $className['class'];
unset($className['class']); unset($className['class']);
return static::$container->newObject($class, $construct, $className); return static::$container->newObject($class, $construct, $className);
} else if (is_callable($className, TRUE)) { } else if (is_callable($className, TRUE)) {
return call_user_func($className, $construct); return call_user_func($className, $construct);
} else if (is_callable($className, TRUE)) {
return call_user_func($className, $construct);
} else { } else {
throw new Exception('Unsupported configuration type: ' . gettype($className)); throw new Exception('Unsupported configuration type: ' . gettype($className));
} }