This commit is contained in:
2021-08-02 16:38:50 +08:00
parent fd7415b7f1
commit a095c94e21
15 changed files with 1593 additions and 1513 deletions
+4 -19
View File
@@ -12,7 +12,6 @@ namespace Database;
use Database\Traits\QueryTrait; use Database\Traits\QueryTrait;
use Exception; use Exception;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Snowflake\Snowflake;
/** /**
* Class ActiveQuery * Class ActiveQuery
@@ -107,22 +106,12 @@ class ActiveQuery extends Component implements ISqlBuilder
if (is_string($name)) { if (is_string($name)) {
$name = explode(',', $name); $name = explode(',', $name);
} }
foreach ($name as $key => $val) { foreach ($name as $val) {
array_push($this->with, $val); array_push($this->with, $val);
} }
return $this; return $this;
} }
/**
* @param bool $isArray
* @return $this
*/
public function asArray(bool $isArray = TRUE): static
{
$this->asArray = $isArray;
return $this;
}
/** /**
* @param $sql * @param $sql
@@ -137,20 +126,16 @@ class ActiveQuery extends Component implements ISqlBuilder
/** /**
* @return ActiveRecord|array|null * @return ActiveRecord|null
* @throws Exception * @throws Exception
*/ */
public function first(): ActiveRecord|array|null public function first(): ActiveRecord|null
{ {
$data = $this->execute($this->builder->one())->one(); $data = $this->execute($this->builder->one())->one();
if (empty($data)) { if (empty($data)) {
return NULL; return NULL;
} }
$newModel = $this->modelClass::populate($data); return $this->modelClass::populate($data);
if ($this->asArray) {
return $newModel->toArray();
}
return $newModel;
} }
+22 -6
View File
@@ -43,7 +43,7 @@ use validator\Validator;
* @property bool $isCreate * @property bool $isCreate
* @method rules() * @method rules()
* @method static tableName() * @method static tableName()
* @property Application $application * @property Application $container
*/ */
abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
{ {
@@ -106,7 +106,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
/** /**
* @return Application * @return Application
*/ */
#[Pure] private function getApplication(): Application #[Pure] private function getContainer(): Application
{ {
return Snowflake::app(); return Snowflake::app();
} }
@@ -322,14 +322,30 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
/** /**
* @return ActiveQuery * @return ActiveQuery
* @throws ReflectionException
* @throws NotFindClassException
*/ */
public static function find(): ActiveQuery public static function find(): ActiveQuery
{ {
return Snowflake::createObject(ActiveQuery::class, [static::class]); return new ActiveQuery(get_called_class());
} }
/**
* @return ActiveQuery
*/
public static function query(): ActiveQuery
{
return new ActiveQuery(get_called_class());
}
/**
* @throws ConfigException
*/
protected function getConnection()
{
return Config::get('connections.' . static::$ab_name, null, true);
}
/** /**
* @param null $condition * @param null $condition
* @param array $attributes * @param array $attributes
@@ -710,7 +726,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
#[Event(ActiveRecord::AFTER_SAVE)] #[Event(self::AFTER_SAVE)]
public function afterSave($attributes, $changeAttributes): bool public function afterSave($attributes, $changeAttributes): bool
{ {
return true; return true;
+10
View File
@@ -10,6 +10,7 @@ use HttpServer\Http\HttpParams;
use HttpServer\Http\Request; use HttpServer\Http\Request;
use HttpServer\Http\Response; use HttpServer\Http\Response;
use Snowflake\Abstracts\TraitApplication; use Snowflake\Abstracts\TraitApplication;
use Snowflake\Application;
/** /**
* Class WebController * Class WebController
@@ -21,6 +22,15 @@ class Controller
use TraitApplication; use TraitApplication;
/**
* @param Application $container
*/
public function __construct(protected Application $container)
{
}
/** /**
* inject request * inject request
* *
+5 -3
View File
@@ -63,21 +63,22 @@ abstract class BaseApplication extends Service
/** /**
* Init constructor. * Init constructor.
* *
* @param array $config
* *
* @throws * @throws
*/ */
public function __construct(array $config = []) public function __construct()
{ {
Snowflake::init($this); Snowflake::init($this);
$config = sweep(APP_PATH . '/config');
$this->moreComponents(); $this->moreComponents();
$this->parseInt($config); $this->parseInt($config);
$this->parseEvents($config); $this->parseEvents($config);
$this->initErrorHandler(); $this->initErrorHandler();
$this->enableEnvConfig(); $this->enableEnvConfig();
parent::__construct($config); parent::__construct();
} }
@@ -458,6 +459,7 @@ abstract class BaseApplication extends Service
'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],
'aop' => ['class' => Aop::class], 'aop' => ['class' => Aop::class],
'input' => ['class' => HttpParams::class], 'input' => ['class' => HttpParams::class],
'header' => ['class' => HttpHeaders::class], 'header' => ['class' => HttpHeaders::class],
+3 -2
View File
@@ -16,15 +16,15 @@ use HttpServer\Route\Router;
use HttpServer\Server; use HttpServer\Server;
use HttpServer\Shutdown; use HttpServer\Shutdown;
use Kafka\Producer; use Kafka\Producer;
use Rpc\Producer as RPCProducer;
use Snowflake\Async; use Snowflake\Async;
use Snowflake\Cache\Redis; use Snowflake\Cache\Redis;
use Snowflake\Channel; use Snowflake\Channel;
use Snowflake\Error\Logger; use Snowflake\Error\Logger;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Jwt\Jwt; use Snowflake\Jwt\Jwt;
use Snowflake\Pool\Pool;
use Snowflake\Pool\Connection; use Snowflake\Pool\Connection;
use Rpc\Producer as RPCProducer; use Snowflake\Pool\Pool;
/** /**
* Trait TraitApplication * Trait TraitApplication
@@ -45,6 +45,7 @@ use Rpc\Producer as RPCProducer;
* @property BaseGoto $goto * @property BaseGoto $goto
* @property Producer $kafka * @property Producer $kafka
* @property Client $client * @property Client $client
* @property \Database\Connection $databases
* @property Curl $curl * @property Curl $curl
* @property \Snowflake\Crontab\Producer $crontab * @property \Snowflake\Crontab\Producer $crontab
* @property HttpFilter $filter * @property HttpFilter $filter
+2 -7
View File
@@ -25,12 +25,7 @@ use Snowflake\Snowflake;
*/ */
class Redis extends Component class Redis extends Component
{ {
public string $host = '127.0.0.1';
public string $auth = 'xl.2005113426';
public int $port = 6973;
public int $databases = 0;
public int $timeout = -1;
public string $prefix = 'idd';
/** /**
* @throws Exception * @throws Exception
@@ -54,7 +49,7 @@ class Redis extends Component
$config = $this->get_config(); $config = $this->get_config();
$length = (int)env('REDIS.POOL_LENGTH', 100); $length = Config::get('connections.pool.max',10);
$connections->initConnections('Redis:' . $config['host'], true, $length); $connections->initConnections('Redis:' . $config['host'], true, $length);
} }
+132
View File
@@ -0,0 +1,132 @@
<?php
namespace Snowflake\Di;
use JetBrains\PhpStorm\Pure;
use ReflectionClass;
use ReflectionMethod;
trait Attributes
{
private array $_classTarget = [];
private array $_classMethodNote = [];
private array $_classMethod = [];
private array $_classPropertyNote = [];
private array $_classProperty = [];
/**
* @param ReflectionClass $class
*/
protected function setTargetNote(ReflectionClass $class)
{
$className = $class->getName();
if (!isset($this->_classTarget[$className])) {
$this->_classTarget[$className] = [];
}
foreach ($class->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$this->_classTarget[$className][] = $attribute->newInstance();
}
}
/**
* @param mixed $class
* @return array
*/
public function getTargetNote(mixed $class): array
{
if (!is_string($class)) {
$class = $class::class;
}
return $this->_classTarget[$class] ?? [];
}
/**
* @param ReflectionClass $class
*/
protected function setMethodNote(ReflectionClass $class)
{
$className = $class->getName();
$this->_classMethodNote[$className] = $this->_classMethod[$className] = [];
foreach ($class->getMethods(ReflectionMethod::IS_FINAL | ReflectionMethod::IS_PRIVATE
| ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED | ReflectionMethod::IS_ABSTRACT
) as $ReflectionMethod) {
$this->_classMethod[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
foreach ($ReflectionMethod->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$this->_classMethodNote[$className][] = $attribute->newInstance();
}
}
}
/**
* @param ReflectionClass $class
* @return array
*/
#[Pure] public function getMethodNote(ReflectionClass $class): array
{
return $this->_classMethodNote[$class->getName()] ?? [];
}
/**
* @param ReflectionClass $class
*/
protected function setPropertyNote(ReflectionClass $class)
{
$className = $class->getName();
$this->_classProperty[$className] = $this->_classPropertyNote[$className] = [];
foreach ($class->getProperties(\ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PUBLIC |
\ReflectionProperty::IS_PROTECTED) as $ReflectionMethod) {
$this->_classProperty[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
foreach ($ReflectionMethod->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$this->_classPropertyNote[$className][] = $attribute->newInstance();
}
}
}
/**
* @param ReflectionClass $class
* @return array
*/
#[Pure] public function getMethods(ReflectionClass $class): array
{
return $this->_classMethod[$class->getName()] ?? [];
}
/**
* @param ReflectionClass $class
* @return \ReflectionProperty[]
*/
#[Pure] public function getProperty(ReflectionClass $class): array
{
return $this->_classProperty[$class->getName()] ?? [];
}
/**
* @param ReflectionClass $class
* @return array
*/
#[Pure] public function getPropertyNote(ReflectionClass $class): array
{
return $this->_classPropertyNote[$class->getName()] ?? [];
}
}
+111 -277
View File
@@ -10,14 +10,13 @@ declare(strict_types=1);
namespace Snowflake\Di; namespace Snowflake\Di;
use Annotation\Inject; use Annotation\Inject;
use Annotation\Target;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure;
use ReflectionClass; use ReflectionClass;
use ReflectionException; use ReflectionException;
use ReflectionMethod; use ReflectionMethod;
use ReflectionProperty; use ReflectionProperty;
use Snowflake\Abstracts\BaseObject; use Snowflake\Abstracts\BaseObject;
use Snowflake\Abstracts\Configure;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -28,63 +27,32 @@ use Snowflake\Snowflake;
class Container extends BaseObject class Container extends BaseObject
{ {
use Attributes;
/** /**
* @var array * @var array
* *
* instance class by className * instance class by className
*/ */
private static array $_singletons = []; private array $_singletons = [];
/** /**
* @var array * @var array
* *
* class new instance construct parameter * class new instance construct parameter
*/ */
private static array $_constructs = []; private array $_constructs = [];
/** /**
* @var array * @var array
* *
* implements \ReflectClass * implements \ReflectClass
*/ */
private static array $_reflection = []; private array $_reflection = [];
/** /** @var array */
* @var ReflectionProperty[] private array $_parameters = [];
*/
private static array $_reflectionProperty = [];
/**
* @var ReflectionMethod[]
*/
private static array $_reflectionMethod = [];
/**
* @var ReflectionClass[]
*/
private static array $_reflectionClass = [];
/**
* @var array
*/
private static array $_propertyAttributes = [];
/**
* @var array
*/
private static array $_methodsAttributes = [];
/**
* @var array
*/
private static array $_targetAttributes = [];
private static array $_attributeMapping = [];
/** /**
@@ -92,7 +60,7 @@ class Container extends BaseObject
* *
* The construct parameter * The construct parameter
*/ */
private static array $_param = []; private array $_param = [];
/** /**
* @param $class * @param $class
@@ -106,50 +74,25 @@ class Container extends BaseObject
*/ */
public function get($class, array $constrict = [], array $config = []): mixed public function get($class, array $constrict = [], array $config = []): mixed
{ {
if (isset(static::$_singletons[$class])) { if (!isset($this->_singletons[$class])) {
return static::$_singletons[$class]; $this->_singletons[$class] = $this->resolve($class, $constrict, $config);
} else if (!isset(static::$_constructs[$class])) {
return $this->resolve($class, $constrict, $config);
}
$definition = static::$_constructs[$class];
if (is_callable($definition, TRUE)) {
return call_user_func($definition, $this, $constrict, $config);
} else if (is_array($definition)) {
return static::$_singletons[$class] = $this->resolveDefinition($definition, $class, $config, $constrict);
} else if (is_object($definition)) {
return static::$_singletons[$class] = $definition;
} else {
throw new NotFindClassException($class);
} }
return $this->_singletons[$class];
} }
/** /**
* @param $definition
* @param $class * @param $class
* @param $config * @param array $constrict
* @param $constrict * @param array $config
* @return mixed * @return object
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
private function resolveDefinition($definition, $class, $config, $constrict): mixed public function newObject($class, array $constrict = [], array $config = []): object
{ {
if (!isset($definition['class'])) { return $this->resolve($class, $constrict, $config);
throw new NotFindClassException($class);
}
$_className = $definition['class'];
unset($definition['class']);
$definition = $this->mergeParam($class, $constrict);
if ($_className === $class) {
$object = $this->resolve($class, $definition, $config);
} else {
$object = $this->get($class, $definition, $config);
}
return $object;
} }
@@ -163,65 +106,44 @@ class Container extends BaseObject
*/ */
private function resolve($class, $constrict, $config): object private function resolve($class, $constrict, $config): object
{ {
/** @var ReflectionClass $reflect */ $reflect = $this->resolveDependencies($class);
[$reflect, $dependencies] = $this->resolveDependencies($class, $constrict);
if (empty($reflect) || !$reflect->isInstantiable()) { if (empty($reflect) || !$reflect->isInstantiable()) {
throw new NotFindClassException($class); throw new NotFindClassException($class);
} }
$object = $this->setConfig($config, $reflect, $dependencies, $class);
return $this->setClassAnnotation($reflect, $object); $object = $this->setConfig($config, $reflect, $constrict);
}
/**
* @param $reflect
* @param $object
* @return mixed
* @throws Exception
*/
private function setClassAnnotation($reflect, $object): mixed
{
static::$_reflectionClass[$reflect->getName()] = [];
foreach ($reflect->getAttributes() as $attribute) {
static::$_reflectionClass[$reflect->getName()][] = $attribute->newInstance();
}
return $this->propertyInject($reflect, $object); return $this->propertyInject($reflect, $object);
} }
/** /**
* @param $config * @param $config
* @param $reflect * @param $reflect
* @param $dependencies * @param $dependencies
* @param $class * @return object
* @return mixed * @throws NotFindClassException
* @throws ReflectionException
*/ */
private function setConfig($config, $reflect, $dependencies, $class): mixed private function setConfig($config, $reflect, $dependencies): object
{ {
if (empty($config) || !is_array($config)) {
$object = $this->newInstance($reflect, $dependencies); $object = $this->newInstance($reflect, $dependencies);
} else if (!empty($dependencies) && $reflect->implementsInterface(Configure::class)) { return $this->onAfterInit($object, $config);
$dependencies[count($dependencies) - 1] = $config;
$object = $this->newInstance($reflect, $dependencies);
} else {
static::$_param[$class] = $config;
$object = $this->onAfterInit($this->newInstance($reflect, $dependencies), $config);
}
return $object;
} }
/** /**
* @param $reflect * @param ReflectionClass $reflect
* @param $dependencies * @param $dependencies
* @return mixed * @return object
* @throws ReflectionException
* @throws NotFindClassException
*/ */
private function newInstance($reflect, $dependencies): mixed private function newInstance(ReflectionClass $reflect, $dependencies): object
{ {
if (!empty($dependencies)) { $parameters = $this->getMethodParameters($reflect, '__construct');
return $reflect->newInstanceArgs($dependencies); $parameters = $this->mergeParam($parameters, $dependencies);
if (!empty($parameters)) {
return $reflect->newInstanceArgs($parameters);
} }
return $reflect->newInstance(); return $reflect->newInstance();
} }
@@ -235,10 +157,7 @@ class Container extends BaseObject
*/ */
public function propertyInject(ReflectionClass $reflect, $object): mixed public function propertyInject(ReflectionClass $reflect, $object): mixed
{ {
if (!isset(static::$_propertyAttributes[$reflect->getName()])) { foreach ($this->getPropertyNote($reflect) as $property => $inject) {
return $object;
}
foreach (static::$_propertyAttributes[$reflect->getName()] as $property => $inject) {
/** @var Inject $inject */ /** @var Inject $inject */
$inject->execute($object, $property); $inject->execute($object, $property);
} }
@@ -246,102 +165,15 @@ class Container extends BaseObject
} }
/**
* @param ReflectionClass $class
*/
private function resolveMethodAttribute(ReflectionClass $class)
{
if ($class->isAbstract() || $class->isTrait()) {
return;
}
foreach ($class->getMethods() as $method) {
if ($method->isStatic()) {
continue;
}
$this->setReflectionMethod($class, $method);
foreach ($method->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$this->setMethodsAttributes($class, $method, $attribute->newInstance());
}
}
}
/**
* @param ReflectionClass $class
* @param ReflectionMethod $method
*/
private function setReflectionMethod(ReflectionClass $class, ReflectionMethod $method)
{
if (!isset(static::$_attributeMapping[$class->getName()])) {
static::$_attributeMapping[$class->getName()] = [];
}
static::$_attributeMapping[$class->getName()][$method->getName()][] = $method;
}
/**
* @param ReflectionClass $attribute
* @param ReflectionMethod $method
* @param $object
*/
private function setMethodsAttributes(ReflectionClass $attribute, ReflectionMethod $method, $object)
{
if (!isset(static::$_methodsAttributes[$attribute->getName()])) {
static::$_methodsAttributes[$attribute->getName()] = [];
}
static::$_methodsAttributes[$attribute->getName()][$method->getName()][] = $object;
}
/**
* @param ReflectionClass $class
*/
private function resolveTargetAttribute(ReflectionClass $class)
{
if ($class->isAbstract() || $class->isTrait()) {
return;
}
foreach ($class->getAttributes() as $method) {
if ($method->getName() == Target::class) {
continue;
}
static::$_targetAttributes[$class->getName()] = $method->newInstance();
}
}
/**
* @param $className
* @param $method
* @return ReflectionMethod|null
*/
public function getReflectionMethod($className, $method): ?ReflectionMethod
{
return static::$_reflectionMethod[$className][$method] ?? null;
}
/**
* @param $className
* @return ReflectionMethod|null
*/
public function getTargetAnnotation($className): ?ReflectionMethod
{
return static::$_targetAttributes[$className] ?? null;
}
/** /**
* @param $className * @param $className
* @param $method * @param $method
* @return array * @return array
* @throws ReflectionException
*/ */
public function getMethodAttribute($className, $method = null): array public function getMethodAttribute($className, $method = null): array
{ {
$methods = static::$_methodsAttributes[$className] ?? []; $methods = $this->getMethodNote($this->getReflect($className));
if (!empty($method)) { if (!empty($method)) {
return $methods[$method] ?? []; return $methods[$method] ?? [];
} }
@@ -353,17 +185,18 @@ class Container extends BaseObject
* @param string $class * @param string $class
* @param string|null $property * @param string|null $property
* @return ReflectionProperty|ReflectionProperty[]|null * @return ReflectionProperty|ReflectionProperty[]|null
* @throws ReflectionException
*/ */
public function getClassReflectionProperty(string $class, string $property = null): ReflectionProperty|null|array public function getClassReflectionProperty(string $class, string $property = null): ReflectionProperty|null|array
{ {
if (!isset(static::$_reflectionProperty[$class])) { $lists = $this->getProperty($this->getReflect($class));
if (empty($lists)) {
return null; return null;
} }
$properties = static::$_reflectionProperty[$class];
if (!empty($property)) { if (!empty($property)) {
return $properties[$property] ?? null; return $lists[$property] ?? null;
} }
return $properties; return $lists;
} }
@@ -381,75 +214,78 @@ class Container extends BaseObject
return $object; return $object;
} }
/** /**
* @param $class * @param $class
* @param array $dependencies * @return ReflectionClass
* @throws ReflectionException
*/
private function resolveDependencies($class): ReflectionClass
{
$this->_reflection[$class] = new ReflectionClass($class);
if (!$this->_reflection[$class]->isInstantiable()) {
throw new ReflectionException('Class ' . $class . ' cannot be instantiated');
}
$this->setPropertyNote($this->_reflection[$class]);
$this->setTargetNote($this->_reflection[$class]);
$this->setMethodNote($this->_reflection[$class]);
if (!is_null($construct = $this->_reflection[$class]->getConstructor())) {
$this->_constructs[$class] = $construct;
}
return $this->_reflection[$class];
}
/**
* @param ReflectionClass|string $class
* @return ReflectionMethod[]
*/
#[Pure] public function getReflectMethods(ReflectionClass|string $class): array
{
return $this->getMethods($class);
}
/**
* @param ReflectionClass|string $class
* @param string $method
* @return ReflectionMethod|null
*/
#[Pure] public function getReflectMethod(ReflectionClass|string $class, string $method): ?ReflectionMethod
{
return $this->getReflectMethods($class)[$method] ?? null;
}
/**
* @param ReflectionClass|string $class
* @param string $method
* @return array|null * @return array|null
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
private function resolveDependencies($class, array $dependencies = []): ?array public function getMethodParameters(ReflectionClass|string $class, string $method): ?array
{ {
if (!isset(static::$_reflection[$class])) { if (isset($this->_parameters[$class]) && isset($this->_parameters[$class][$method])) {
if (!($reflect = new ReflectionClass($class))->isInstantiable()) { return $this->_parameters[$class][$method];
throw new ReflectionException('Class ' . $class . ' cannot be instantiated');
} }
static::$_reflection[$class] = $reflect; $reflectMethod = $this->getReflectMethod($class, $method);
$this->resolveTargetAttribute(static::$_reflection[$class]); if (!($reflectMethod instanceof ReflectionMethod)) {
$this->resolveMethodAttribute(static::$_reflection[$class]); throw new ReflectionException("Class does not have a function $class::$method");
$this->scanProperty(static::$_reflection[$class]);
} }
if (!is_null($construct = static::$_reflection[$class]->getConstructor())) { if ($reflectMethod->getNumberOfParameters() < 1) {
foreach ($this->resolveMethodParam($construct) as $key => $item) { return [];
$dependencies[$key] = $item;
} }
} $this->_parameters[$class][$method] = $params = [];
return [static::$_reflection[$class], $dependencies]; foreach ($reflectMethod->getParameters() as $key => $parameter) {
}
/**
* @param ReflectionClass $reflectionClass
* @return void
*/
private function scanProperty(ReflectionClass $reflectionClass): void
{
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC |
ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED
);
$className = $reflectionClass->getName();
foreach ($properties as $property) {
$targets = $property->getAttributes(Inject::class);
if (count($targets) < 1) {
continue;
}
static::$_reflectionProperty[$className][$property->getName()] = $property;
static::$_propertyAttributes[$className][$property->getName()] = $targets[0]->newInstance();
}
}
/**
* @param ReflectionMethod|null $method
* @return array
* @throws NotFindClassException
* @throws ReflectionException
*/
private function resolveMethodParam(?ReflectionMethod $method): array
{
$array = [];
foreach ($method->getParameters() as $parameter) {
if ($parameter->isDefaultValueAvailable()) { if ($parameter->isDefaultValueAvailable()) {
$array[] = $parameter->getDefaultValue(); $params[$key] = $parameter->getDefaultValue();
} else { } else {
$type = $parameter->getType(); $type = $parameter->getType();
if (is_string($type) && class_exists($type)) { if (is_string($type) && class_exists($type)) {
$type = Snowflake::createObject($type); $type = Snowflake::getDi()->get($type);
} }
$array[] = match ($parameter->getType()) { $params[$key] = match ($parameter->getType()) {
'string' => '', 'string' => '',
'int', 'float' => 0, 'int', 'float' => 0,
'', null, 'object', 'mixed' => NULL, '', null, 'object', 'mixed' => NULL,
@@ -458,23 +294,21 @@ class Container extends BaseObject
}; };
} }
} }
return $array; return $this->_parameters[$class][$method] = $params;
} }
/** /**
* @param $class * @param $class
* @return ReflectionClass|null * @return ReflectionClass|null
* @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
public function getReflect($class): ?ReflectionClass public function getReflect($class): ?ReflectionClass
{ {
$reflect = static::$_reflection[$class] ?? null; if (!isset($this->_reflection[$class])) {
if (!is_null($reflect)) { return $this->resolveDependencies($class);
return $reflect;
} }
return $this->resolveDependencies($class)[0]; return $this->_reflection[$class];
} }
/** /**
@@ -488,8 +322,8 @@ class Container extends BaseObject
$class = $class::class; $class = $class::class;
} }
unset( unset(
static::$_reflection[$class], static::$_singletons[$class], $this->_reflection[$class], $this->_singletons[$class],
static::$_param[$class], static::$_constructs[$class] $this->_param[$class], $this->_constructs[$class]
); );
} }
@@ -498,10 +332,10 @@ class Container extends BaseObject
*/ */
public function flush(): static public function flush(): static
{ {
static::$_reflection = []; $this->_reflection = [];
static::$_singletons = []; $this->_singletons = [];
static::$_param = []; $this->_param = [];
static::$_constructs = []; $this->_constructs = [];
return $this; return $this;
} }
@@ -513,12 +347,12 @@ class Container extends BaseObject
*/ */
private function mergeParam($class, $newParam): array private function mergeParam($class, $newParam): array
{ {
if (empty(static::$_param[$class])) { if (empty($this->_param[$class])) {
return $newParam; return $newParam;
} else if (empty($newParam)) { } else if (empty($newParam)) {
return static::$_param[$class]; return $this->_param[$class];
} }
$old = static::$_param[$class]; $old = $this->_param[$class];
foreach ($newParam as $key => $val) { foreach ($newParam as $key => $val) {
$old[$key] = $val; $old[$key] = $val;
} }
+24 -14
View File
@@ -45,23 +45,33 @@ class Service extends Component
if (isset($this->_components[$id])) { if (isset($this->_components[$id])) {
return $this->_components[$id]; return $this->_components[$id];
} }
if (!isset($this->_definition[$id]) && !isset($this->_alias[$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) { if ($try === false) {
return null; return null;
} }
throw new ComponentException("Unknown component ID: $id"); throw new ComponentException("Unknown component ID: $id");
} }
if (isset($this->_definition[$id])) { return $config;
$config = $this->_definition[$id];
if (is_object($config)) {
return $this->_components[$id] = $config;
}
} else {
$config = $this->_alias[$id];
}
return $this->_components[$id] = Snowflake::createObject($config);
} }
/** /**
* @param string $className * @param string $className
* @param string $alias * @param string $alias
@@ -84,17 +94,17 @@ class Service extends Component
$this->remove($id); $this->remove($id);
return; return;
} }
$this->_ids[] = $id; $this->_ids[] = $id;
unset($this->_components[$id]); unset($this->_components[$id]);
if (is_object($definition) || is_callable($definition, TRUE)) { if (is_object($definition)) {
$this->_components[$id] = $definition;
$this->_definition[$id] = $definition; $this->_definition[$id] = $definition;
} else if (!is_array($definition)) { } else if (!is_array($definition)) {
throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition)); throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
} } else {
$this->_definition[$id] = $definition; $this->_definition[$id] = $definition;
} }
}
/** /**
* @param $id * @param $id
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace Snowflake\Events;
class EventDispatch
{
private EventListener $eventListener;
/**
* @param $event
* @param array $params
*/
public function emit($event, array $params = [])
{
$events = $this->eventListener->getEventListeners($event);
if (empty($events)) {
return;
}
while ($events->valid()) {
/** @var EventDispatchInterface $interface */
$interface = $events->current();
$interface->onHandler();
if ($interface->stopPagination()) {
break;
}
$events->next();
}
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Snowflake\Events;
/**
*
*/
interface EventDispatchInterface
{
public function getZOrder(): int;
public function onHandler(): void;
public function stopPagination(): bool;
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace Snowflake\Events;
use SplPriorityQueue;
class EventListener
{
/** @var SplPriorityQueue[] */
private array $_events = [];
/**
* @param $event
* @param EventDispatchInterface $handler
*/
public function on($event, EventDispatchInterface $handler)
{
if (!isset($this->_events[$event])) {
$this->_events[$event] = new SplPriorityQueue();
}
$this->_events[$event]->insert($handler, $handler->getZOrder());
}
/**
* @param $event
* @return SplPriorityQueue|null
*/
public function getEventListeners($event): ?SplPriorityQueue
{
return $this->_events[$event] ?? null;
}
}
+8
View File
@@ -0,0 +1,8 @@
<?php
namespace Snowflake\Events;
class EventProviders
{
}
+6 -6
View File
@@ -202,7 +202,7 @@ class Snowflake
$class = $className['class']; $class = $className['class'];
unset($className['class']); unset($className['class']);
return static::$container->get($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 { } else {
@@ -250,7 +250,7 @@ class Snowflake
*/ */
public static function setManagerId($workerId): mixed public static function setManagerId($workerId): mixed
{ {
if (empty($workerId) || static::isDcoker()) { if (empty($workerId) || static::isDocker()) {
return $workerId; return $workerId;
} }
@@ -267,7 +267,7 @@ class Snowflake
*/ */
public static function setProcessId($workerId): mixed public static function setProcessId($workerId): mixed
{ {
if (empty($workerId) || static::isDcoker()) { if (empty($workerId) || static::isDocker()) {
return $workerId; return $workerId;
} }
@@ -280,7 +280,7 @@ class Snowflake
/** /**
* @return bool * @return bool
*/ */
public static function isDcoker(): bool public static function isDocker(): bool
{ {
$output = shell_exec('[ -f /.dockerenv ] && echo yes || echo no'); $output = shell_exec('[ -f /.dockerenv ] && echo yes || echo no');
if (trim($output) === 'yes') { if (trim($output) === 'yes') {
@@ -297,7 +297,7 @@ class Snowflake
*/ */
public static function setWorkerId($workerId): mixed public static function setWorkerId($workerId): mixed
{ {
if (empty($workerId) || static::isDcoker()) { if (empty($workerId) || static::isDocker()) {
return $workerId; return $workerId;
} }
@@ -314,7 +314,7 @@ class Snowflake
*/ */
public static function setTaskId($workerId): mixed public static function setTaskId($workerId): mixed
{ {
if (empty($workerId) || static::isDcoker()) { if (empty($workerId) || static::isDocker()) {
return $workerId; return $workerId;
} }