改名
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use ReflectionMethod;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class BaseAnnotation
|
||||
* @package Snowflake\Snowflake\Annotation\Base
|
||||
*/
|
||||
abstract class BaseAnnotation extends Component
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $reflect
|
||||
* @param string $method
|
||||
* @param array $annotations
|
||||
* @return array
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function instance($reflect, $method = '', $annotations = [])
|
||||
{
|
||||
$classMethods = $reflect->getMethods(ReflectionMethod::IS_PUBLIC);
|
||||
if (!$reflect->isInstantiable()) {
|
||||
throw new Exception('Class ' . $reflect->getName() . ' cannot be instantiated.');
|
||||
}
|
||||
|
||||
$array = [];
|
||||
$object = $reflect->newInstance();
|
||||
if (!empty($method)) {
|
||||
$array = $this->resolveDocComment($reflect->getMethod($method), $object, $annotations, $array);
|
||||
} else {
|
||||
foreach ($classMethods as $classMethod) {
|
||||
$array = $this->resolveDocComment($classMethod, $object, $annotations, $array);
|
||||
}
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionMethod $function
|
||||
* @param $object
|
||||
* @param $annotations
|
||||
* @param $array
|
||||
* @return array
|
||||
* @throws
|
||||
*/
|
||||
protected function resolveDocComment($function, $object, $annotations, $array)
|
||||
{
|
||||
$comment = $function->getDocComment();
|
||||
$array = $this->getDocCommentAnnotation($annotations, $comment);
|
||||
foreach ($array as $name => $annotation) {
|
||||
foreach ($annotation as $index => $events) {
|
||||
if (!isset($events[1])) {
|
||||
continue;
|
||||
}
|
||||
if (!($_key = $this->getName($name, $events))) {
|
||||
continue;
|
||||
}
|
||||
if (isset($item[2])) {
|
||||
$handler = Snowflake::createObject($events[2]);
|
||||
} else {
|
||||
$handler = [$object, $events[1]];
|
||||
}
|
||||
if (!isset($array[$annotation])) {
|
||||
$array[$annotation] = [];
|
||||
}
|
||||
$array[$name][] = [$_key, $handler];
|
||||
}
|
||||
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $object
|
||||
* @param $events
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
protected function getOrCreate($object, $events)
|
||||
{
|
||||
if (isset($item[2])) {
|
||||
$handler = Snowflake::createObject($events[2]);
|
||||
} else {
|
||||
$handler = [$object, $events[1]];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $annotations
|
||||
* @param $comment
|
||||
* @return array
|
||||
*/
|
||||
protected function getDocCommentAnnotation($annotations, $comment)
|
||||
{
|
||||
$array = [];
|
||||
foreach ($annotations as $annotation) {
|
||||
preg_match('/@(' . $annotation . ')\((.*?)\)/', $comment, $events);
|
||||
if (!isset($events[1])) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($array[$annotation])) {
|
||||
$array[$annotation] = [];
|
||||
}
|
||||
$array[$annotation] = [$annotation, $events];
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $rule
|
||||
* @param $content
|
||||
* @param $rules
|
||||
* @return bool
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function check($rule, $content, $rules)
|
||||
{
|
||||
if (empty($rule)) {
|
||||
return true;
|
||||
}
|
||||
$explode = explode('|', $rule);
|
||||
foreach ($explode as $value) {
|
||||
$reflect = array_merge($rules[$value], [
|
||||
'value' => $content
|
||||
]);
|
||||
$validator = Snowflake::createObject($reflect);
|
||||
if (!$validator->check()) {
|
||||
throw new Exception($validator->getMessage());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
abstract public function runWith($path);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,351 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/10/7 0007
|
||||
* Time: 2:13
|
||||
*/
|
||||
|
||||
namespace Snowflake\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Http\Request;
|
||||
use HttpServer\Http\Response;
|
||||
use HttpServer\Route\Router;
|
||||
use HttpServer\Server;
|
||||
use Snowflake\Annotation\Annotation;
|
||||
use Snowflake\Cache\Memcached;
|
||||
use Snowflake\Cache\Redis;
|
||||
use Snowflake\Config;
|
||||
use Snowflake\Di\Service;
|
||||
use Snowflake\Error\ErrorHandler;
|
||||
use Snowflake\Error\Logger;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\InitException;
|
||||
use Snowflake\Jwt\Jwt;
|
||||
use Snowflake\Pool\Connection;
|
||||
use Snowflake\Pool\Redis as SRedis;
|
||||
use Snowflake\Snowflake;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Pool\Pool as SPool;
|
||||
use Database\DatabasesProviders;
|
||||
|
||||
/**
|
||||
* Class BaseApplication
|
||||
* @package Snowflake\Snowflake\Base
|
||||
* @property $json
|
||||
* @property Annotation $annotation
|
||||
* @property Event $event
|
||||
* @property Router $router
|
||||
* @property SPool $pool
|
||||
* @property Server $server
|
||||
* @property DatabasesProviders $db
|
||||
* @property Connection $connections
|
||||
* @property Memcached $memcached
|
||||
* @property Logger $logger
|
||||
* @property Jwt $jwt
|
||||
*/
|
||||
abstract class BaseApplication extends Service
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $storage = APP_PATH . '/storage';
|
||||
|
||||
public $envPath = APP_PATH . '/.env';
|
||||
|
||||
/**
|
||||
* Init constructor.
|
||||
*
|
||||
* @param array $config
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
Snowflake::init($this);
|
||||
|
||||
$this->moreComponents();
|
||||
$this->parseInt($config);
|
||||
$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
|
||||
*/
|
||||
protected function readLinesFromFile($filePath)
|
||||
{
|
||||
// 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
|
||||
*/
|
||||
protected function isComment($line)
|
||||
{
|
||||
$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
|
||||
*/
|
||||
protected function looksLikeSetter($line)
|
||||
{
|
||||
return strpos($line, '=') !== false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $config
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public function parseInt($config)
|
||||
{
|
||||
foreach ($config as $key => $value) {
|
||||
Config::set($key, $value);
|
||||
}
|
||||
if ($storage = Config::get('storage', false, 'storage')) {
|
||||
if (strpos($storage, APP_PATH) === false) {
|
||||
$storage = APP_PATH . $storage . '/';
|
||||
}
|
||||
if (!is_dir($storage)) {
|
||||
mkdir($storage);
|
||||
}
|
||||
if (!is_dir($storage) || !is_writeable($storage)) {
|
||||
throw new InitException("Directory {$storage} does not have write permission");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Logger
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function getLogger(): Logger
|
||||
{
|
||||
return $this->get('logger');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return \Redis
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function getRedis()
|
||||
{
|
||||
return $this->get('redis');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $ip
|
||||
* @return bool
|
||||
*/
|
||||
public function isLocal($ip)
|
||||
{
|
||||
return $this->getFirstLocal() == $ip;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return ErrorHandler
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->get('error');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Annotation
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function getAnnotation()
|
||||
{
|
||||
return $this->get('annotation');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Connection
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function getConnections()
|
||||
{
|
||||
return $this->get('connections');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Pool
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function getPool()
|
||||
{
|
||||
return $this->get('pool');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Response
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function getResponse()
|
||||
{
|
||||
return $this->get('response');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Request
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function getRequest()
|
||||
{
|
||||
return $this->get('request');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Config
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function getConfig()
|
||||
{
|
||||
return $this->get('config');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Router
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function getRouter()
|
||||
{
|
||||
return $this->get('router');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Event
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function getEvent()
|
||||
{
|
||||
return $this->get('event');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Jwt
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function getJwt()
|
||||
{
|
||||
return $this->get('jwt');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function moreComponents()
|
||||
{
|
||||
return $this->setComponents([
|
||||
'error' => ['class' => ErrorHandler::class],
|
||||
'event' => ['class' => Event::class],
|
||||
'annotation' => ['class' => Annotation::class],
|
||||
'connections' => ['class' => Connection::class],
|
||||
'redis_connections' => ['class' => SRedis::class],
|
||||
'pool' => ['class' => SPool::class],
|
||||
'response' => ['class' => Response::class],
|
||||
'request' => ['class' => Request::class],
|
||||
'config' => ['class' => Config::class],
|
||||
'logger' => ['class' => Logger::class],
|
||||
'router' => ['class' => Router::class],
|
||||
'redis' => ['class' => Redis::class],
|
||||
'jwt' => ['class' => Jwt::class]
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/3/30 0030
|
||||
* Time: 14:10
|
||||
*/
|
||||
|
||||
namespace Snowflake\Abstracts;
|
||||
|
||||
use Exception;
|
||||
use Snowflake\Error\Logger;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class BaseObject
|
||||
* @method defer()
|
||||
* @package Snowflake\Snowflake\Base
|
||||
* @method afterInit
|
||||
* @method initialization
|
||||
*/
|
||||
class BaseObject implements Configure
|
||||
{
|
||||
|
||||
/**
|
||||
* BaseAbstract constructor.
|
||||
*
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct($config = [])
|
||||
{
|
||||
if (!empty($config) && is_array($config)) {
|
||||
Snowflake::configure($this, $config);
|
||||
}
|
||||
$this->init();
|
||||
}
|
||||
|
||||
public function init()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function className()
|
||||
{
|
||||
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
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
$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 $name
|
||||
* @param $arguments
|
||||
*
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
if (!method_exists($this, $name)) {
|
||||
throw new Exception("Not find " . get_called_class() . "::($name)");
|
||||
} else {
|
||||
$result = $this->$name(...$arguments);
|
||||
if (method_exists($this, 'defer')) {
|
||||
$this->defer();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $model
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addError($message, $model = 'app')
|
||||
{
|
||||
if ($message instanceof Exception) {
|
||||
$this->error($message->getMessage(), $message->getFile(), $message->getLine());
|
||||
} else {
|
||||
if (!is_string($message)) {
|
||||
$message = json_encode($message, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
$this->error($message);
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param string $method
|
||||
* @param string $file
|
||||
* @throws
|
||||
*/
|
||||
public function debug($message, string $method = __METHOD__, string $file = __FILE__)
|
||||
{
|
||||
if (!is_string($message)) {
|
||||
$message = print_r($message, true);
|
||||
}
|
||||
echo "\033[35m[DEBUG][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
|
||||
echo PHP_EOL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param string $method
|
||||
* @param string $file
|
||||
* @throws
|
||||
*/
|
||||
public function info($message, string $method = __METHOD__, string $file = __FILE__)
|
||||
{
|
||||
if (!is_string($message)) {
|
||||
$message = print_r($message, true);
|
||||
}
|
||||
echo "\033[34m[INFO][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
|
||||
echo PHP_EOL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param string $method
|
||||
* @param string $file
|
||||
* @throws
|
||||
*/
|
||||
public function success($message, string $method = __METHOD__, string $file = __FILE__)
|
||||
{
|
||||
if (!is_string($message)) {
|
||||
$message = print_r($message, true);
|
||||
}
|
||||
echo "\033[36m[SUCCESS][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
|
||||
echo PHP_EOL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param string $method
|
||||
* @param string $file
|
||||
* @throws
|
||||
*/
|
||||
public function warning($message, string $method = __METHOD__, string $file = __FILE__)
|
||||
{
|
||||
if (!is_string($message)) {
|
||||
$message = print_r($message, true);
|
||||
}
|
||||
echo "\033[33m[SUCCESS][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
|
||||
echo PHP_EOL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param string|null $method
|
||||
* @param string|null $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function error($message, $method = null, $file = null)
|
||||
{
|
||||
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);
|
||||
}
|
||||
echo "\033[41;37m[ERROR][" . date('Y-m-d H:i:s') . ']: ' . (empty($method) ? '' : $method . ': ') . $message . "\033[0m";
|
||||
echo PHP_EOL;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/3/30 0030
|
||||
* Time: 14:28
|
||||
*/
|
||||
|
||||
namespace Snowflake\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Component
|
||||
* @package Snowflake\Snowflake\Base
|
||||
*/
|
||||
class Component extends BaseObject
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $_events = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $name [事件名称]
|
||||
* @param $callback [回调函数]
|
||||
* @param array $param [函数参数]
|
||||
*
|
||||
* {
|
||||
* 事件名, 回调, 参数
|
||||
* }
|
||||
*/
|
||||
public function on($name, $callback, $param = [])
|
||||
{
|
||||
if (isset($this->_events[$name])) {
|
||||
array_push($this->_events[$name], [$callback, $param]);
|
||||
} else {
|
||||
$this->_events[$name][] = [$callback, $param];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $callback
|
||||
* @return bool
|
||||
*/
|
||||
public function hasEvent($name, $callback = null)
|
||||
{
|
||||
if (!isset($this->_events[$name])) {
|
||||
return false;
|
||||
}
|
||||
if (!is_array($this->_events[$name])) {
|
||||
return false;
|
||||
}
|
||||
foreach ($this->_events[$name] as $event) {
|
||||
[$_callback, $param] = $event;
|
||||
if ($_callback === $callback) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $event
|
||||
* @param array $params
|
||||
* @param bool $isRemove
|
||||
* @throws Exception
|
||||
*/
|
||||
public function trigger($name, $event = null, $params = [], $isRemove = false)
|
||||
{
|
||||
$aEvents = Snowflake::app()->event;
|
||||
if (isset($this->_events[$name])) {
|
||||
$events = $this->_events[$name];
|
||||
foreach ($events as $key => $_event) {
|
||||
if (!empty($event)) {
|
||||
$_event = $event;
|
||||
}
|
||||
call_user_func($_event, ...$params);
|
||||
if ($isRemove) {
|
||||
unset($this->_events[$name][$key]);
|
||||
$aEvents->of($name, $_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
$aEvents->trigger($name, $event);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $handler
|
||||
* @return mixed
|
||||
*/
|
||||
public function off($name, $handler = NULL)
|
||||
{
|
||||
$aEvents = Snowflake::app()->event;
|
||||
if (!isset($this->_events[$name])) {
|
||||
return $aEvents->of($name, $handler);
|
||||
}
|
||||
|
||||
if (empty($handler)) {
|
||||
unset($this->_events[$name]);
|
||||
|
||||
return $aEvents->of($name, $handler);
|
||||
}
|
||||
|
||||
foreach ($this->_events[$name] as $key => $val) {
|
||||
if ($val[0] != $handler) {
|
||||
continue;
|
||||
}
|
||||
unset($this->_events[$name][$key]);
|
||||
|
||||
break;
|
||||
}
|
||||
return $aEvents->of($name, $handler);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function offAll()
|
||||
{
|
||||
$this->_events = [];
|
||||
$aEvents = Snowflake::app()->event;
|
||||
$aEvents->clean();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
if (property_exists($this, $name)) {
|
||||
$this->$name = $value;
|
||||
} else {
|
||||
parent::__set($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
if (property_exists($this, $name)) {
|
||||
return $this->$name ?? null;
|
||||
} else {
|
||||
return parent::__get($name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/5/24 0024
|
||||
* Time: 11:50
|
||||
*/
|
||||
|
||||
namespace Snowflake;
|
||||
|
||||
use Exception;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Abstracts\Component;
|
||||
|
||||
|
||||
/**
|
||||
* Class Config
|
||||
* @package Snowflake\Snowflake\Base
|
||||
*/
|
||||
class Config extends Component
|
||||
{
|
||||
|
||||
const ERROR_MESSAGE = 'The not find %s in app configs.';
|
||||
|
||||
protected $data;
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return mixed
|
||||
*/
|
||||
public function setData($key, $value)
|
||||
{
|
||||
return $this->data[$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param bool $try
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public static function get($key, $try = FALSE, $default = null)
|
||||
{
|
||||
$explode = explode('.', $key);
|
||||
$instance = Snowflake::app()->config->getData();
|
||||
foreach ($explode as $index => $value) {
|
||||
if (empty($value)) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($instance[$value])) {
|
||||
if ($try) {
|
||||
throw new ConfigException(sprintf(self::ERROR_MESSAGE, $key));
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
$instance = $instance[$value];
|
||||
if (!is_array($instance) && $index + 1 < count($explode)) {
|
||||
throw new ConfigException(sprintf(self::ERROR_MESSAGE, $key));
|
||||
}
|
||||
}
|
||||
return empty($instance) ? $default : $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function set($key, $value)
|
||||
{
|
||||
$config = Snowflake::app()->config;
|
||||
return $config->setData($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param bool $must_not_null
|
||||
* @return bool
|
||||
*/
|
||||
public static function has($key, $must_not_null = false)
|
||||
{
|
||||
$config = Snowflake::app()->config;
|
||||
if (!isset($config->data[$key])) {
|
||||
return false;
|
||||
}
|
||||
$config = $config->data[$key];
|
||||
if ($must_not_null === false) {
|
||||
return true;
|
||||
}
|
||||
return !empty($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$this->data[$name] = $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/3/30 0030
|
||||
* Time: 14:11
|
||||
*/
|
||||
|
||||
namespace Snowflake\Abstracts;
|
||||
|
||||
/**
|
||||
* Interface Configure
|
||||
* @package Snowflake\Snowflake\Base
|
||||
*/
|
||||
interface Configure
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Abstracts;
|
||||
|
||||
|
||||
use Snowflake\Core\Dtl;
|
||||
|
||||
interface IListener
|
||||
{
|
||||
|
||||
|
||||
public function handler(Dtl $dtl);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Abstracts;
|
||||
|
||||
|
||||
/**
|
||||
* Class Listener
|
||||
* @package Snowflake\Abstracts
|
||||
* 监听的名称
|
||||
*/
|
||||
abstract class Listener extends Component implements IListener
|
||||
{
|
||||
|
||||
protected $trigger = '';
|
||||
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Coroutine\Channel;
|
||||
|
||||
/**
|
||||
* Class Pool
|
||||
* @package Snowflake\Snowflake\Pool
|
||||
*/
|
||||
abstract class Pool extends Component
|
||||
{
|
||||
|
||||
/** @var Channel[] */
|
||||
private $_items = [];
|
||||
|
||||
public $max = 60;
|
||||
|
||||
private $is_ticker = false;
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param false $isMaster
|
||||
* @param int $max
|
||||
*/
|
||||
public function initConnections($name, $isMaster = false, $max = 60)
|
||||
{
|
||||
$name = $this->name($name, $isMaster);
|
||||
if (isset($this->_items[$name]) &&
|
||||
$this->_items[$name] instanceof Channel) {
|
||||
return;
|
||||
}
|
||||
$this->_items[$name] = new Channel($max);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param int $timeout
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function get($name, $timeout = -1)
|
||||
{
|
||||
if ($this->is_ticker) {
|
||||
Coroutine::sleep(1);
|
||||
}
|
||||
if ($timeout != -1) {
|
||||
$client = $this->_items[$name]->pop($timeout);
|
||||
} else {
|
||||
$client = $this->_items[$name]->pop(60);
|
||||
}
|
||||
if (!$this->checkCanUse(...$client)) {
|
||||
unset($client);
|
||||
return [0, null];
|
||||
} else {
|
||||
return $client;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $cds
|
||||
* @param false $isMaster
|
||||
* @return string
|
||||
*/
|
||||
public function name($cds, $isMaster = false)
|
||||
{
|
||||
return hash('sha1', $cds . ($isMaster ? 'master' : 'slave'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $time
|
||||
* @param $client
|
||||
* @return mixed
|
||||
* 检查连接可靠性
|
||||
* @throws Exception
|
||||
*/
|
||||
public function checkCanUse($time, $client)
|
||||
{
|
||||
throw new Exception('Undefined system processing function.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function desc($name)
|
||||
{
|
||||
throw new Exception('Undefined system processing function.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param $isMaster
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getConnection(array $config, $isMaster)
|
||||
{
|
||||
throw new Exception('Undefined system processing function.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function hasItem($name)
|
||||
{
|
||||
return $this->hasLength($name) > 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function hasLength($name)
|
||||
{
|
||||
if (!isset($this->_items[$name])) {
|
||||
return 0;
|
||||
}
|
||||
return $this->_items[$name]->length();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $client
|
||||
*/
|
||||
public function push($name, $client)
|
||||
{
|
||||
if (!isset($this->_items[$name])) {
|
||||
return;
|
||||
}
|
||||
if (!$this->_items[$name]->isFull()) {
|
||||
$this->_items[$name]->push([time(), $client]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*/
|
||||
public function clean($name)
|
||||
{
|
||||
if (!isset($this->_items[$name])) {
|
||||
return;
|
||||
}
|
||||
while ([$time, $client] = $this->_items[$name]->pop(0.001)) {
|
||||
unset($client);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Abstracts;
|
||||
|
||||
|
||||
use Snowflake\Application;
|
||||
|
||||
interface Provider
|
||||
{
|
||||
|
||||
public function onImport(Application $application);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Abstracts;
|
||||
|
||||
|
||||
use Snowflake\Application;
|
||||
|
||||
/**
|
||||
* Class Providers
|
||||
* @package Snowflake\Abstracts
|
||||
*/
|
||||
abstract class Providers extends Component implements Provider
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user