Skip to content

Commit

Permalink
[3.0] 废弃注释注解,全面拥抱原生注解🚀 (#625)
Browse files Browse the repository at this point in the history
* 注释注解转原生注解

* 删除测试中动态注解属性(计划废弃)

* 重构代码,修复 amqp 兼容

* 修复测试

* 修复

* 修复

* 修复一些注解常量值

* rector

* 废弃注释注解

* 更新 PhpParser

* 修复测试

* 更新文档

* 文档中的注释注解转换为原生注解

* \t 转 4 空格
  • Loading branch information
Yurunsoft authored Nov 1, 2023
1 parent 86e3528 commit 36c5270
Show file tree
Hide file tree
Showing 699 changed files with 4,002 additions and 6,402 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"symfony/polyfill-php81": "^1.23",
"symfony/polyfill-php82": "^1.26",
"symfony/process": "^5.1|^6.0",
"vlucas/phpdotenv": "~5.5",
"yurunsoft/doctrine-annotations": "^1.73.0"
"vlucas/phpdotenv": "~5.5"
},
"require-dev": {
"composer/semver": "^3.3.2",
Expand Down
23 changes: 6 additions & 17 deletions doc/annotations/annotation.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ use Imi\Bean\Annotation\Parser;
* 示例注解
* @Annotation
* @Target("METHOD")
* @Parser("\Imi\Bean\Parser\NullParser")
*
* // 下面是IDE提示注释
* @property string $name 随便定义的属性
* @property int $age 随便定义的属性
*/
// 下面的是原生注解定义
#[\Attribute(\Attribute::TARGET_METHOD)]
#[Parser(className: \Imi\Bean\Parser\NullParser::class)]
class MyAnnotation extends Base
{
/**
Expand Down Expand Up @@ -84,11 +83,6 @@ use ImiApp\Annotation\MyAnnotation;
#[Bean(['Test'])]
class Test
{
/**
* @MyAnnotation("a")
* @MyAnnotation(name="b", age=11)
*/
// 下面是原生注解用法
#[MyAnnotation(name: 'a')]
#[MyAnnotation(name: 'b', age: 11)]
public function aaa()
Expand All @@ -113,22 +107,17 @@ use Imi\Aop\Annotation\PointCut;
use Imi\Aop\PointCutType;
use Imi\Aop\AroundJoinPoint;

/**
* @Aspect
*/
#[Aspect]
class TransactionAop
{
/**
* 自动事务支持
* @PointCut(
* type=PointCutType::ANNOTATION,
* allow={
* \ImiApp\Annotation\MyAnnotation::class
* }
* )
* @Around
* @return mixed
*/
#[
PointCut(type: PointCutType::ANNOTATION, allow: [\ImiApp\Annotation\MyAnnotation::class]),
Around
]
public function parseTransaction(AroundJoinPoint $joinPoint)
{
// 前置操作
Expand Down
17 changes: 5 additions & 12 deletions doc/annotations/filterArg.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ imi 框架的方法参数过滤器是一种方便开发者在框架层面对方
简单使用:

```php
/*
* @FilterArg(name="data", filter="json_decode")
*/
#[
FilterArg(name: 'data', filter: 'json_decode')
]
public function test($data)
{
var_dump($data); // 这是一个stdClass对象
Expand All @@ -34,9 +34,7 @@ $obj->test('{"id":1, "message": "imi nb!"}');
结合`@Callback``@Inject`注解使用,支持使用`bean`中的方法。

```php
/**
* @Bean("XXX")
*/
#[Bean(name: 'XXX')]
class TestXXX
{
public function decode($data)
Expand All @@ -45,12 +43,7 @@ class TestXXX
}
}

/*
* @FilterArg(name="data", filter=@Callback(
* class=@Inject("XXX"),
* method="decode"
* ))
*/
#[FilterArg(name: 'data', filter: new Callback(class: new Inject(name: 'XXX', method: 'decode')))]
public function test($data)
{
var_dump($data); // 这是一个数组
Expand Down
12 changes: 1 addition & 11 deletions doc/annotations/injectValue.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,7 @@
## 用法示例

```php
/*
* @Cacheable(
* key="index:{page}",
* ttl=10,
* lockable=@Lockable(
* id="index:{page}",
* waitTimeout=999999,
* ),
* preventBreakdown=true,
* )
*/
#[Cacheable(key: 'index:{page}', ttl: 10, lockable: new Lockable(id: 'index:{page}', waitTimeout: 999999), preventBreakdown: true)]
```

### @DbInject
Expand Down
28 changes: 14 additions & 14 deletions doc/base/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ return [
'别名1' => [],

// bean扫描目录,指定命名空间,建议省略
// 'beanScan' => [
// 'ImiDemo\WebSocketDemo\Listener',
// 'beanScan' => [
// 'ImiDemo\WebSocketDemo\Listener',
// ],

// 日志配置,详见日志文档
Expand Down Expand Up @@ -128,19 +128,19 @@ return [
'tool' => 'imi:{toolName}/{toolOperation}:{namespace}',
],
// 主服务器配置
'mainServer' => [
'mainServer' => [
// 指定服务器命名空间
'namespace' => 'ImiDemo\HttpDemo\MainServer',
'namespace' => 'ImiDemo\HttpDemo\MainServer',
// 服务器类型(http/WebSocket/TcpServer/UdpServer)
'type' => \Imi\Swoole\Server\Type::HTTP,
'type' => \Imi\Swoole\Server\Type::HTTP,
// 监听的IP地址,可选
'host' => '0.0.0.0',
'host' => '0.0.0.0',
// 监听的端口
'port' => 8080,
'port' => 8080,
// 参考 swoole mode,可选
'mode' => SWOOLE_BASE,
'mode' => SWOOLE_BASE,
// 参考 swoole sockType,可选
'sockType' => SWOOLE_SOCK_TCP,
'sockType' => SWOOLE_SOCK_TCP,
// 同步连接,当连接事件执行完后,才执行 receive 事件。仅 TCP、WebSocket 且 SWOOLE_BASE 模式有效
'syncConnect' => true,
// 服务器配置,参数用法同\Swoole\Server->set($configs)
Expand All @@ -149,10 +149,10 @@ return [
// 参考: http://wiki.swoole.com/#/http_server?id=%e9%85%8d%e7%bd%ae%e9%80%89%e9%a1%b9
'nonControlFrameType' => \Imi\Server\WebSocket\Enum\NonControlFrameType::TEXT, // 配置 WebSocket 纯文本通信协议
// 'nonControlFrameType' => \Imi\Server\WebSocket\Enum\NonControlFrameType::BINARY, // 配置 WebSocket 二进制通信协议
'configs' => [
'reactor_num' => 8,
'worker_num' => 8,
'task_worker_num' => 16,
'configs' => [
'reactor_num' => 8,
'worker_num' => 8,
'task_worker_num' => 16,
// Swoole 错误日志文件。如果不设置或为null则自动记录到 .runtime/swoole/swoole.log。如果设为 false 不记录 Swoole 错误日志。
// 'log_file' => '',
],
Expand All @@ -164,7 +164,7 @@ return [
// 子服务器(端口监听)配置
'subServers' => [
// 子服务器别名
'alias1' => [
'alias1' => [
// 这里同主服务器配置
]
],
Expand Down
61 changes: 61 additions & 0 deletions doc/base/version/2.1-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,65 @@

[toc]

## 迁移工具

迁移工具可以帮助你把旧项目中的注释注解、注解类,自动升级到 3.0 格式。

### 安装

```shell
composer require imiphp/annotation-migration
```

### 迁移注解语法

检查 src 目录下的 php 是否有传统注解并重写

```shell
./vendor/bin/imi-migration --dir="src"
```

### 迁移注解定义

检查 src 目录下的 php 是否有传统定义并转换[构造器属性提升](https://www.php.net/manual/zh/language.oop5.decon.php#language.oop5.decon.constructor.promotion)语法

```shell
./vendor/bin/imi-migration --dir="src" --annotation-rewrite
```

### 初始化配置文件(可选)

配置文件务必是输出在项目根目录,可对`imi`注解语法解析器进行配置,以解决注解读取的冲突问题。

```shell
./vendor/bin/imi-migration --init-config
```

**默认配置文件例子:**

```php
<?php
declare(strict_types=1);

return [
'globalIgnoredName' => [
// 'depends',
// 'type',
// 'testdox',
],
'globalIgnoredNamespace' => [],
'globalImports' => [
// 'oa', 'OpenApi\Annotations',
],
];
```

### 共用参数选项说明

- `--dry-run` 尝试运行,预览哪些文件会受到影响
- `--no-catch-continue` 遇到异常时中断转换过程
- `--no-error-continue` 检查到错误时中断转换过程

## 不兼容的更改

### 框架核心
Expand Down Expand Up @@ -43,3 +102,5 @@
* 废弃 `Model::updateBatch()``Model::deleteBatch()`

* 废弃模型查询时动态指定字段名的特殊处理

* 废弃注释注解,改为全部使用原生注解
Loading

0 comments on commit 36c5270

Please sign in to comment.