modify plugin name

This commit is contained in:
2022-06-22 16:29:42 +08:00
parent e98c67e0c3
commit c72c5d4e0d
5 changed files with 117 additions and 197 deletions
+2 -2
View File
@@ -13,11 +13,11 @@ namespace Database\Base;
use ArrayIterator; use ArrayIterator;
use Database\ActiveQuery; use Database\ActiveQuery;
use Database\ModelInterface; use Database\ModelInterface;
use Kiri\ToArray;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use ReturnTypeWillChange; use Kiri\ReturnTypeWillChange;
use Kiri\ToArray;
use Traversable; use Traversable;
/** /**
+84 -126
View File
@@ -17,8 +17,6 @@ use ArrayAccess;
use Closure; use Closure;
use Database\ActiveQuery; use Database\ActiveQuery;
use Database\Connection; use Database\Connection;
use Database\HasMany;
use Database\HasOne;
use Database\ModelInterface; use Database\ModelInterface;
use Database\Mysql\Columns; use Database\Mysql\Columns;
use Database\Relation; use Database\Relation;
@@ -30,9 +28,9 @@ use Kiri\Abstracts\Component;
use Kiri\Annotation\Annotation; use Kiri\Annotation\Annotation;
use Kiri\Error\StdoutLoggerInterface; use Kiri\Error\StdoutLoggerInterface;
use Kiri\Exception\NotFindClassException; use Kiri\Exception\NotFindClassException;
use ReturnTypeWillChange;
use Kiri\ToArray; use Kiri\ToArray;
use ReflectionException; use ReflectionException;
use ReturnTypeWillChange;
use validator\Validator; use validator\Validator;
/** /**
@@ -109,59 +107,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
/**
* @param string $name
* @param mixed $value
* @return mixed
* @throws ReflectionException
*/
private function _setter(string $name, mixed $value): mixed
{
$method = di(Setter::class)->getSetter(static::class, $name);
if (!empty($method)) {
$value = $this->{$method}($value);
}
return $value;
}
/**
* @param string $name
* @param $value
* @return mixed
* @throws ReflectionException
*/
private function _getter(string $name, $value): mixed
{
$data = di(Getter::class)->getGetter(static::class, $name);
if (empty($data)) {
return $this->_relater($name, $value);
}
return $this->{$data}($value);
}
/**
* @param string $name
* @param $value
* @return mixed
* @throws ReflectionException
* @throws Exception
*/
private function _relater(string $name, $value): mixed
{
$data = di(Relate::class)->getRelate(static::class, $name);
if (!empty($data)) {
$data = $this->{$data}();
if ($data instanceof HasBase) {
return $data->get();
}
return $data;
}
return $this->_decode($name, $value);
}
/** /**
* @param $data * @param $data
* @return Model * @return Model
@@ -310,10 +255,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function getPrimaryValue(): ?int public function getPrimaryValue(): ?int
{ {
if (!$this->hasPrimary()) { if ($this->hasPrimary()) {
return NULL; return $this->getAttribute($this->primary);
} }
return $this->getAttribute($this->primary); return null;
} }
/** /**
@@ -407,8 +352,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
/** /**
* @param $condition * @param $condition
* @return static|null * @return static|null
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
public static function first($condition): ?static public static function first($condition): ?static
@@ -428,10 +371,11 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
/** /**
* @return Connection * @return Connection
* @throws Exception
*/ */
public function getConnection(): Connection public function getConnection(): Connection
{ {
return Kiri::app()->get($this->connection); return Kiri::service()->get($this->connection);
} }
@@ -484,7 +428,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function setAttribute($name, $value): mixed public function setAttribute($name, $value): mixed
{ {
return $this->_attributes[$name] = $this->_setter($name, $value); if (method_exists($this, 'set' . ucfirst($name) . 'Attribute')) {
$value = $this->{'set' . ucfirst($name) . 'Attribute'}($value);
}
return $this->_attributes[$name] = $value;
} }
/** /**
@@ -495,7 +442,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function setOldAttribute($name, $value): mixed public function setOldAttribute($name, $value): mixed
{ {
return $this->_oldAttributes[$name] = $this->_setter($name, $value); if (method_exists($this, 'set' . ucfirst($name) . 'Attribute')) {
$value = $this->{'set' . ucfirst($name) . 'Attribute'}($value);
}
return $this->_oldAttributes[$name] = $value;
} }
/** /**
@@ -687,9 +637,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function getAttribute(string $name) public function getAttribute(string $name)
{ {
if ($this->hasAnnotation($name)) {
return $this->runAnnotation($name, $this->_attributes[$name]);
}
return $this->_attributes[$name] ?? NULL; return $this->_attributes[$name] ?? NULL;
} }
@@ -866,10 +813,11 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function __set($name, $value): void public function __set($name, $value): void
{ {
if (method_exists($this, 'set' . ucfirst($name))) { $method = 'set' . ucfirst($name) . 'Attribute';
$this->{'set' . ucfirst($name)}($value); if (method_exists($this, $method)) {
$this->_attributes[$name] = $this->{$method}($value);
} else { } else {
$this->_attributes[$name] = $this->_setter($name, $value); $this->_attributes[$name] = $value;
} }
} }
@@ -881,13 +829,70 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/ */
public function __get($name): mixed public function __get($name): mixed
{ {
$method = 'get' . ucfirst($name); if (isset($this->_attributes[$name])) {
if (method_exists($this, $method)) { return $this->withPropertyOverride($name);
return $this->{$method}();
} }
$value = $this->_attributes[$name] ?? NULL; return $this->getRelateValue($name);
}
return $this->_getter($name, $value);
/**
* @param $name
* @param null $value
* @return mixed
* @throws Exception
*/
protected function withPropertyOverride($name, $value = null): mixed
{
if (is_null($value)) {
$value = $this->_attributes[$name] ?? NULL;
}
$method = 'get' . ucfirst($name) . 'Attribute';
if (!method_exists($this, $method)) {
return $this->_decode($name, $value);
}
return $this->{$method}($value);
}
/**
* @param $name
* @return bool
*/
protected function hasRelateMethod($name): bool
{
return method_exists($this, 'get' . ucfirst($name));
}
/**
* @param $name
* @return mixed|null
*/
protected function withRelate($name): mixed
{
$response = $this->getRelateValue($name);
if ($response instanceof ToArray) {
$response = $response->toArray();
}
return $response;
}
/**
* @param $name
* @return mixed
*/
protected function getRelateValue($name): mixed
{
if (!$this->hasRelateMethod($name)) {
return null;
}
$response = $this->{'get' . ucfirst($name)}();
if ($response instanceof HasBase) {
$response = $response->get();
}
return $response;
} }
@@ -897,7 +902,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
private function _decode($name, $value): mixed protected function _decode($name, $value): mixed
{ {
return $this->getColumns()->_decode($name, $value); return $this->getColumns()->_decode($name, $value);
} }
@@ -917,30 +922,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
/**
* @param string $type
* @return array
*/
protected function getAnnotation(string $type = self::GET): array
{
return $this->_annotations[$type] ?? [];
}
/**
* @param $name
* @param string $type
* @return bool
*/
protected function hasAnnotation($name, string $type = self::GET): bool
{
if (!isset($this->_annotations[$type])) {
return FALSE;
}
return isset($this->_annotations[$type][$name]);
}
/** /**
* @param $item * @param $item
* @param $data * @param $data
@@ -960,22 +941,6 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
return isset($this->_attributes[$name]); return isset($this->_attributes[$name]);
} }
/**
* @param $call
* @return mixed
* @throws Exception
*/
private function resolveClass($call): mixed
{
if ($call instanceof HasOne) {
return $call->get();
} else if ($call instanceof HasMany) {
return $call->get();
} else {
return $call;
}
}
/** /**
* @param mixed $offset * @param mixed $offset
@@ -1025,19 +990,12 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
} }
/** /**
* @param string ...$params
* @return array * @return array
*/ */
public function unset(): array public function unset(string ...$params): array
{ {
$fields = func_get_args(); return array_diff_assoc($params, $this->_attributes);
$fields = array_shift($fields);
if (!is_array($fields)) {
$fields = explode(',', $fields);
}
$array = array_combine($fields, $fields);
return array_diff_assoc($array, $this->_attributes);
} }
-8
View File
@@ -1,8 +0,0 @@
<?php
namespace Database;
class CreateConnectionPool
{
}
+4 -4
View File
@@ -8,7 +8,6 @@ use Exception;
use Kiri; use Kiri;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Abstracts\Providers; use Kiri\Abstracts\Providers;
use Kiri\Application;
use Kiri\Pool\Connection as PoolConnection; use Kiri\Pool\Connection as PoolConnection;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Events\EventProvider; use Kiri\Events\EventProvider;
@@ -18,6 +17,7 @@ use Kiri\Server\Events\OnTaskerStart;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Kiri\Server\Events\OnWorkerExit; use Kiri\Server\Events\OnWorkerExit;
use Swoole\Timer; use Swoole\Timer;
use Kiri\Di\LocalService;
/** /**
* Class DatabasesProviders * Class DatabasesProviders
@@ -35,12 +35,12 @@ class DatabasesProviders extends Providers
/** /**
* @param Application $application * @param LocalService $application
* @return void * @return void
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public function onImport(Application $application): void public function onImport(LocalService $application): void
{ {
$databases = Config::get('databases.connections', []); $databases = Config::get('databases.connections', []);
if (empty($databases)) { if (empty($databases)) {
@@ -75,7 +75,7 @@ class DatabasesProviders extends Providers
*/ */
public function get($name): Connection public function get($name): Connection
{ {
return Kiri::app()->get($name); return Kiri::service()->get($name);
} }
+27 -57
View File
@@ -113,14 +113,18 @@ class Model extends Base\Model
if (empty($attributes)) { if (empty($attributes)) {
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
} }
/** @var static $select */ /** @var static $select */
$select = static::query()->where($condition)->first(); $select = static::query()->where($condition)->first();
if (empty($select)) { if (!empty($select)) {
$select = new static(); return $select;
$select->attributes = $attributes; }
if (!$select->save()) {
throw new Exception($select->getLastError()); $select = new static();
} $select->setAttributes($condition);
$select->setAttributes($attributes);
if (!$select->save()) {
throw new Exception($select->getLastError());
} }
return $select; return $select;
} }
@@ -142,8 +146,9 @@ class Model extends Base\Model
$select = static::query()->where($condition)->first(); $select = static::query()->where($condition)->first();
if (empty($select)) { if (empty($select)) {
$select = new static(); $select = new static();
$select->setAttributes($condition);
} }
$select->attributes = $attributes; $select->setAttributes($attributes);
if (!$select->save()) { if (!$select->save()) {
throw new Exception($select->getLastError()); throw new Exception($select->getLastError());
} }
@@ -152,17 +157,14 @@ class Model extends Base\Model
/** /**
* @param $action
* @param $columns * @param $columns
* @param null|array $condition * @param $action
* @return array|bool|int|string|null * @return array|bool|int|string|null
* @throws Exception * @throws Exception
*/ */
private function mathematics($columns, $action, ?array $condition = null): int|bool|array|string|null private function mathematics($columns, $action): int|bool|array|string|null
{ {
if (empty($condition)) { $condition = [$this->getPrimary() => $this->getPrimaryValue()];
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
}
$activeQuery = static::query()->where($condition); $activeQuery = static::query()->where($condition);
$create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action); $create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action);
@@ -191,13 +193,10 @@ class Model extends Base\Model
*/ */
public static function inserts(array $data): bool public static function inserts(array $data): bool
{ {
$result = false;
if (empty($data)) { if (empty($data)) {
error('Insert data empty.', 'mysql'); return error('Insert data empty.', 'mysql');
} else {
$result = static::query()->batchInsert($data);
} }
return $result; return static::query()->batchInsert($data);
} }
/** /**
@@ -210,14 +209,14 @@ class Model extends Base\Model
if (empty($primary) || !$this->hasPrimaryValue()) { if (empty($primary) || !$this->hasPrimaryValue()) {
return $this->logger->addError("Only primary key operations are supported.", 'mysql'); return $this->logger->addError("Only primary key operations are supported.", 'mysql');
} }
if ($this->beforeDelete()) { if (!$this->beforeDelete()) {
$result = static::deleteByCondition([$primary => $this->getPrimaryValue()]); return false;
Coroutine::create(function () use ($result) {
$this->afterDelete($result);
});
return $result;
} }
return false; $result = static::deleteByCondition([$primary => $this->getPrimaryValue()]);
$this->afterDelete($result);
return $result;
} }
@@ -226,8 +225,6 @@ class Model extends Base\Model
* @param array $attributes * @param array $attributes
* *
* @return bool * @return bool
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
public static function updateAll(mixed $condition, array $attributes = []): bool public static function updateAll(mixed $condition, array $attributes = []): bool
@@ -240,8 +237,6 @@ class Model extends Base\Model
/** /**
* @param $condition * @param $condition
* @return array|Collection * @return array|Collection
* @throws NotFindClassException
* @throws ReflectionException
*/ */
public static function get($condition): Collection|array public static function get($condition): Collection|array
{ {
@@ -265,30 +260,6 @@ class Model extends Base\Model
return $query->all(); return $query->all();
} }
/**
* @param $method
* @return mixed
* @throws Exception
*/
private function withRelation($method): mixed
{
$method = $this->getRelate($method);
if (empty($method)) {
return null;
}
$resolve = $this->{$method}();
if ($resolve instanceof HasBase) {
$resolve = $resolve->get();
}
if ($resolve instanceof ToArray) {
return $resolve->toArray();
} else if (is_object($resolve)) {
return get_object_vars($resolve);
} else {
return $resolve;
}
}
/** /**
* @return array * @return array
@@ -297,9 +268,8 @@ class Model extends Base\Model
public function toArray(): array public function toArray(): array
{ {
$data = $this->_attributes; $data = $this->_attributes;
$lists = di(Getter::class)->getGetter(static::class); foreach ($data as $key => $datum) {
foreach ($lists as $key => $item) { $data[$key] = $this->withPropertyOverride($key, $datum);
$data[$key] = $this->{$item}($data[$key] ?? null);
} }
return $this->withRelates($data); return $this->withRelates($data);
} }
@@ -315,7 +285,7 @@ class Model extends Base\Model
return $relates; return $relates;
} }
foreach ($with as $val) { foreach ($with as $val) {
$relates[$val] = $this->withRelation($val); $relates[$val] = $this->withRelate($val);
} }
return $relates; return $relates;
} }