This commit is contained in:
2021-02-22 17:44:24 +08:00
parent 3b09b9a308
commit 2d1f83cf09
34 changed files with 732 additions and 222 deletions
+12
View File
@@ -33,6 +33,7 @@ use Snowflake\Exception\ComponentException;
use Snowflake\Exception\InitException;
use Snowflake\Jwt\Jwt;
use Snowflake\Pool\Connection;
use Snowflake\Pool\ObjectPool;
use Snowflake\Pool\Redis as SRedis;
use Snowflake\Snowflake;
use Snowflake\Event;
@@ -404,6 +405,16 @@ abstract class BaseApplication extends Service
}
/**
* @return ObjectPool
* @throws ComponentException
*/
public function getObject(): ObjectPool
{
return $this->get('object');
}
/**
* @throws Exception
*/
@@ -424,6 +435,7 @@ abstract class BaseApplication extends Service
'redis' => ['class' => Redis::class],
'jwt' => ['class' => Jwt::class],
'async' => ['class' => Async::class],
'object' => ['class' => ObjectPool::class],
'goto' => ['class' => BaseGoto::class],
'http2' => ['class' => Http2::class],
]);
+6 -5
View File
@@ -49,11 +49,12 @@ abstract class Pool extends Component
}
/**
* @param $name
* @return array
* @throws Exception
*/
/**
* @param $name
* @param array $callback
* @return array
* @throws Exception
*/
protected function get($name, array $callback): mixed
{
if (!Context::inCoroutine()) {
+4 -3
View File
@@ -97,13 +97,14 @@ class Application extends BaseApplication
* @param Input $argv
* @return bool|string
* @throws Exception
* @throws NotFindClassException
* @throws \ReflectionException
*/
public function start(Input $argv): bool|string
{
$this->set('input', $argv);
try {
fire(Event::SERVER_BEFORE_START);
$this->set('input', $argv);
$manager = Snowflake::app()->get('console');
$manager->setParameters($argv);
$class = $manager->search();
+33 -24
View File
@@ -6,6 +6,7 @@ namespace Snowflake;
use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\BaseObject;
use Snowflake\Core\ArrayAccess;
@@ -45,6 +46,7 @@ class Event extends BaseObject
const SERVER_MANAGER_STOP = 'SERVER:EVENT:MANAGER:START';
const SERVER_WORKER_STOP = 'SERVER:EVENT:WORKER:STOP';
const SERVER_WORKER_START = 'SERVER:EVENT:WORKER:START';
const SERVER_AFTER_WORKER_START = 'SERVER:EVENT:AFTER:WORKER:START';
const SERVER_BEFORE_START = 'SERVER:EVENT:BEFORE:START';
const SERVER_TASK_START = 'SERVER:EVENT:TASK:START';
const SERVER_WORKER_EXIT = 'SERVER:EVENT:WORKER:EXIT';
@@ -193,31 +195,14 @@ class Event extends BaseObject
return false;
}
if (!empty($handler) && $this->exists($name, $handler)) {
[$handler, $defaultParameter] = $this->get($name, $handler);
if (!empty($parameter)) {
$defaultParameter = ArrayAccess::merge($defaultParameter, $parameter);
}
if (!is_array($defaultParameter)) {
$defaultParameter = [$defaultParameter];
}
$result = call_user_func($handler, ...$defaultParameter);
if ($is_remove) {
$this->of($name, $handler);
}
return $result;
$events = [$this->get($name, $handler)];
} else {
$events = $this->_events[$name];
}
foreach ($this->_events[$name] as $index => $event) {
try {
[$handler, $defaultParameter] = $event;
if (!empty($parameter)) {
$defaultParameter = ArrayAccess::merge($defaultParameter, $parameter);
}
if (!is_array($defaultParameter)) {
$defaultParameter = [$defaultParameter];
}
call_user_func($handler, ...$defaultParameter);
} catch (\Throwable $exception) {
$this->error($exception);
foreach ($events as $index => $event) {
$meta = $this->mergeParams($event[1], $parameter);
if (call_user_func($event[0], ...$meta) === false) {
return false;
}
}
if ($is_remove) {
@@ -227,4 +212,28 @@ class Event extends BaseObject
}
/**
* @param $defaultParameter
* @param $parameter
* @return array
*/
#[Pure] private function mergeParams($defaultParameter, $parameter = []): array
{
if (empty($defaultParameter)) {
$defaultParameter = $parameter;
} else {
if (!is_array($parameter)) {
$parameter = [];
}
foreach ($parameter as $key => $value) {
$defaultParameter[] = $value;
}
}
if (!is_array($defaultParameter)) {
$defaultParameter = [$defaultParameter];
}
return $defaultParameter;
}
}
@@ -0,0 +1,35 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/24 0024
* Time: 17:32
*/
declare(strict_types=1);
namespace Snowflake\Exception;
use Throwable;
/**
* Class NotFindClassException
* @package Snowflake\Snowflake\Exception
*/
class NotFindPropertyException extends \Exception
{
/**
* NotFindClassException constructor.
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(string $message = "", int $code = 0, Throwable $previous = null)
{
$message = "No class named `$message` was found, please check if the class name is correct";
parent::__construct($message, 404, $previous);
}
}
+6 -5
View File
@@ -151,11 +151,12 @@ class Connection extends Pool
}
/**
* @param array $config
* @return mixed
*/
public function createClient(string $name, array $config): mixed
/**
* @param string $name
* @param array $config
* @return PDO
*/
public function createClient(string $name, array $config): PDO
{
$this->printClients($config['cds'], $name, true);
// TODO: Implement createClient() method.
+58
View File
@@ -0,0 +1,58 @@
<?php
namespace Snowflake\Pool;
use Exception;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
* Class ObjectPool
* @package Snowflake\Pool
*/
class ObjectPool extends \Snowflake\Abstracts\Pool
{
/**
* @param array $config
* @param bool $isMaster
* @return mixed
* @throws Exception
*/
public function getConnection(array $config, bool $isMaster): mixed
{
return $this->get($config[0], $config);
}
/**
* @param string $name
* @param array $config
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
*/
public function createClient(string $name, array $config): mixed
{
// TODO: Implement createClient() method.
return Snowflake::createObject(array_shift($config));
}
/**
* @param string $name
* @param $object
*/
public function release(string $name, mixed $object)
{
if (method_exists($object, 'clean')) {
$object->clean();
}
$this->push($name, $object);
}
}
+5 -6
View File
@@ -5,7 +5,6 @@ declare(strict_types=1);
namespace Snowflake\Pool;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake;
@@ -38,9 +37,9 @@ class Pool extends \Snowflake\Abstracts\Pool
}
public function createClient(string $name, array $config): mixed
{
// TODO: Implement createClient() method.
return null;
}
public function createClient(string $name, array $config): mixed
{
// TODO: Implement createClient() method.
return null;
}
}
+7 -6
View File
@@ -49,12 +49,13 @@ class Redis extends Pool
}
/**
* @param array $config
* @return SRedis
* @throws RedisConnectException
*/
public function createClient(string $name, array $config): mixed
/**
* @param string $name
* @param array $config
* @return SRedis
* @throws RedisConnectException
*/
public function createClient(string $name, array $config): SRedis
{
$this->printClients($config['host'], $name, true);
$redis = new SRedis();