Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] 3.0 from imiphp:3.0 #190

Open
wants to merge 3 commits into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .github/php.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ARG SWOOLE_VERSION
COPY script/ /tmp/script

RUN set -eux \
&& apt-get update && apt-get -y install procps libpq-dev unzip git libzip-dev libevent-dev libssl-dev libicu-dev libc-ares-dev libcurl4-openssl-dev unixodbc-dev libsqlite3-dev \
&& apt-get update && apt-get -y install procps libpq-dev unzip git libzip-dev libevent-dev libssl-dev libicu-dev libc-ares-dev libcurl4-openssl-dev unixodbc-dev libsqlite3-dev libbrotli-dev \
&& docker-php-ext-install -j$(nproc) bcmath mysqli pdo_mysql pdo_pgsql pcntl sockets intl zip \
&& (php --ri redis || (pecl install redis && docker-php-ext-enable redis)) \
&& pecl install inotify \
Expand All @@ -18,6 +18,13 @@ RUN set -eux \
&& pecl install apcu \
&& docker-php-ext-enable apcu \
&& curl -sfL https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer && chmod +x /usr/bin/composer \
&& curl -L -o swoole.tar.gz https://github.com/swoole/swoole-src/archive/${SWOOLE_VERSION}.tar.gz && mkdir -p swoole && tar -xzf swoole.tar.gz -C swoole --strip-components=1 && rm swoole.tar.gz && cd swoole && ((stat ./make.sh && ./make.sh) || (stat ./scripts/make.sh && ./scripts/make.sh)) && cd - && docker-php-ext-enable swoole \
&& curl -L -o swoole.tar.gz https://github.com/swoole/swoole-src/archive/${SWOOLE_VERSION}.tar.gz && mkdir -p swoole && tar -xzf swoole.tar.gz -C swoole --strip-components=1 && rm swoole.tar.gz && cd swoole && ((stat ./make.sh && ./make.sh) || (phpize && ./configure --enable-openssl \
--enable-sockets \
--enable-mysqlnd \
--enable-swoole-curl \
--enable-cares \
--enable-swoole-pgsql \
--with-swoole-odbc=unixODBC,/usr \
--enable-swoole-sqlite && make -j install)) && cd - && docker-php-ext-enable swoole \
&& bash /tmp/script/swoole_postgresql.sh ${POSTGRESQL_VERSION} \
&& echo "zend_extension=opcache.so" >> /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ jobs:
# MacOS Arm64 下需要下面的修复,否则无法编译成功
- name: Fix include
run: |
sudo ln -s $(brew --prefix pcre2)/include/pcre2.h /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/
sudo mkdir -p /usr/local/include
sudo ln -s $(brew --prefix pcre2)/include/pcre2.h /usr/local/include/
- name: Get Openssl Dir
id: opecssl-dir
run: echo "path=$(brew --prefix [email protected])" >> $GITHUB_OUTPUT
Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/daily-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ on:
push:
paths:
- ".github/workflows/daily-test.yml"
- ".github/docker-compose.yml"
- ".github/php.dockerfile"
- ".github/actions/ci-prepare"
pull_request:
paths:
- ".github/workflows/daily-test.yml"
- ".github/docker-compose.yml"
- ".github/php.dockerfile"
- ".github/actions/ci-prepare"

jobs:
daily-test-3_0:
Expand All @@ -33,8 +39,6 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: "3.0"
- name: Cache dependencies
uses: actions/cache@v4
with:
Expand Down
4 changes: 4 additions & 0 deletions doc/base/version/2.1-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ return [

* 事件名称规则统一改为全小写,为保持兼容请[参考文档](/v3.0/event/index.html#3.0%20兼容性)

### 数据库

* `Imi\Db\Db::getNewInstance()` 返回值改为 `Imi\ConnectionCenter\Contract\IConnection`

### 模型

* UUID 发号器的 `type` 类型改为枚举,大小写有所变化
Expand Down
30 changes: 30 additions & 0 deletions src/Components/connection-center/src/ConnectionCenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,23 @@ public function getConnectionManagers(): array
return $this->connectionManagers;
}

/**
* 获取连接.
*
* 需要调用 getInstance() 获取实际的对象进行操作.
*
* 需要自行管理连接生命周期,连接对象释放即归还连接池.
*/
public function getConnection(string $name): IConnection
{
return $this->getConnectionManager($name)->getConnection();
}

/**
* 获取连接.
*
* 自动管理生命周期,当前上下文结束时自动释放.
*/
public function getRequestContextConnection(string $name): IConnection
{
$requestContext = RequestContext::getContext();
Expand Down Expand Up @@ -119,4 +131,22 @@ public function getRequestContextConnection(string $name): IConnection

return $connection;
}

/**
* 创建新的连接.
*
* 返回的连接是实际的对象,不受连接管理器管理生命周期.
*/
public function createConnection(?string $name = null, bool $autoConnect = true): mixed
{
$driver = self::getConnectionManager($name)->getDriver();
$instance = $driver->createInstance();

if ($autoConnect)
{
return $driver->connect($instance);
}

return $instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @method static IConnectionManager[] getConnectionManagers()
* @method static \Imi\ConnectionCenter\Contract\IConnection getConnection(string $name)
* @method static \Imi\ConnectionCenter\Contract\IConnection getRequestContextConnection(string $name)
* @method static mixed createConnection(?string $name = NULL, bool $autoConnect = true)
*/
#[
\Imi\Facade\Annotation\Facade(class: \Imi\ConnectionCenter\AppConnectionCenter::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ public function testRequestContextDestroy(): void
}
}

public function testCreateConnection(): void
{
foreach (self::$names as $name)
{
$connection1 = self::$connectionCenter->createConnection($name);
$this->assertTrue($connection1->connected);

$connection2 = self::$connectionCenter->createConnection($name, false);
$this->assertFalse($connection2->connected);

$this->assertTrue(self::$connectionCenter->createConnection($name) !== self::$connectionCenter->createConnection($name));
}
}

/**
* @depends testNewInstance
*/
Expand Down
31 changes: 23 additions & 8 deletions src/Components/database/src/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Imi\Db;

use Imi\Config;
use Imi\ConnectionCenter\Contract\IConnection;
use Imi\ConnectionCenter\Facade\ConnectionCenter;
use Imi\Db\ConnectionCenter\DatabaseDriverConfig;
use Imi\Db\Interfaces\IDb;
Expand All @@ -19,28 +20,42 @@ class Db
use \Imi\Util\Traits\TStaticClass;

/**
* 获取新的数据库连接实例.
* 获取数据库连接实例,每个上下文中共用一个.
*
* @param string|null $poolName 连接池名称
* @param int $queryType 查询类型
*/
public static function getNewInstance(?string $poolName = null, int $queryType = QueryType::WRITE): IDb
public static function getInstance(?string $poolName = null, int $queryType = QueryType::WRITE): IDb
{
$driver = ConnectionCenter::getConnectionManager(self::parsePoolName($poolName, $queryType))->getDriver();
$instance = $driver->createInstance();
return ConnectionCenter::getRequestContextConnection(self::parsePoolName($poolName, $queryType))->getInstance();
}

return $driver->connect($instance);
/**
* 创建新的数据库连接实例.
*
* @param string|null $poolName 连接池名称
* @param int $queryType 查询类型
*/
public static function createInstance(?string $poolName = null, int $queryType = QueryType::WRITE): IDb
{
return ConnectionCenter::createConnection(self::parsePoolName($poolName, $queryType));
}

/**
* 获取数据库连接实例,每个RequestContext中共用一个.
* 获取新的数据库连接实例.
*
* 返回的是连接池连接对象,需调用 `getInstance()` 获取实际的对象进行操作.
*
* 连接对象释放即断开连接,操作期间需要保证连接对象不被释放.
*
* 如果不是特别必要,不推荐使用此方法!!!
*
* @param string|null $poolName 连接池名称
* @param int $queryType 查询类型
*/
public static function getInstance(?string $poolName = null, int $queryType = QueryType::WRITE): IDb
public static function getNewInstance(?string $poolName = null, int $queryType = QueryType::WRITE): IConnection
{
return ConnectionCenter::getRequestContextConnection(self::parsePoolName($poolName, $queryType))->getInstance();
return ConnectionCenter::getConnection(self::parsePoolName($poolName, $queryType));
}

/**
Expand Down
52 changes: 0 additions & 52 deletions src/Components/database/src/Listener/CheckPoolResource.php

This file was deleted.

6 changes: 6 additions & 0 deletions src/Components/model/src/Annotation/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ public function __construct(
/**
* save/update 模型时是否将当前时间写入该字段;支持 date/time/datetime/timestamp/year/int/bigint;当字段为 int 类型,写入秒级时间戳;当字段为 bigint 类型,写入毫秒级时间戳.
*
* @deprecated 3.1
*
* @var bool|int
*/
public $updateTime = false,
/**
* 列表分割字符串;如果字段类型为list,并且此字段不为null,读取时会处理为数组,写入时会处理为字符串.
*
* @deprecated 3.1
*/
public ?string $listSeparator = null,
/**
Expand All @@ -80,6 +84,8 @@ public function __construct(
/**
* save/create 模型时是否将当前时间写入该字段,save时表有自增ID主键才支持;支持 date/time/datetime/timestamp/year/int/bigint;当字段为 int 类型,写入秒级时间戳;当字段为 bigint 类型,写入毫秒级时间戳.
*
* @deprecated 3.1
*
* @var bool|int
*/
public $createTime = false
Expand Down
26 changes: 26 additions & 0 deletions src/Components/model/src/Annotation/CreateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Imi\Model\Annotation;

use Imi\Bean\Annotation\Base;

/**
* 创建时间.
*
* 有此注解就表示启用.
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class CreateTime extends Base
{
public function __construct(
/**
* 时间精度.
*
* 仅 bigint 有效,例:1000为毫秒
*/
public int $timeAccuracy = 1000
) {
}
}
22 changes: 22 additions & 0 deletions src/Components/model/src/Annotation/ListSeparator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Imi\Model\Annotation;

use Imi\Bean\Annotation\Base;

/**
* 字符串字段分割为列表.
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ListSeparator extends Base
{
public function __construct(
/**
* 用于分割的字符串.
*/
public string $separator = ','
) {
}
}
26 changes: 26 additions & 0 deletions src/Components/model/src/Annotation/UpdateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Imi\Model\Annotation;

use Imi\Bean\Annotation\Base;

/**
* 更新时间.
*
* 有此注解就表示启用.
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class UpdateTime extends Base
{
public function __construct(
/**
* 时间精度.
*
* 仅 bigint 有效,例:1000为毫秒
*/
public int $timeAccuracy = 1000
) {
}
}
7 changes: 5 additions & 2 deletions src/Components/model/src/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Imi\Event\IEvent;
use Imi\Event\TEvent;
use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\ExtractProperty;
use Imi\Model\Annotation\JsonNotNull;
use Imi\Model\Annotation\Relation\AutoSelect;
use Imi\Model\Event\ModelEvents;
use Imi\Model\Event\Param\InitEventParam;
Expand Down Expand Up @@ -302,7 +304,8 @@ public function offsetSet(mixed $key, mixed $value): void
if (\is_array($value) || \is_object($value))
{
// 提取字段中的属性到当前模型
$extractProperties = $meta->getExtractPropertys();
/** @var ExtractProperty[][] $extractProperties */
$extractProperties = $meta->getPropertyAnnotations()[ExtractProperty::class] ?? [];
if (
(($name = $key) && isset($extractProperties[$name]))
|| (($name = Text::toUnderScoreCase($key)) && isset($extractProperties[$name]))
Expand Down Expand Up @@ -387,7 +390,7 @@ public function toArray(): array
if (null === $value)
{
// JsonNotNull 注解支持
if (isset(($propertyJsonNotNullMap ??= ($meta ??= $this->__meta)->getPropertyJsonNotNullMap())[$name]))
if (isset(($meta ??= $this->__meta)->getPropertyAnnotations()[JsonNotNull::class][$name]))
{
continue;
}
Expand Down
Loading
Loading