Skip to content

Commit 1429c95

Browse files
committed
第一次提交
0 parents  commit 1429c95

File tree

16 files changed

+405
-0
lines changed

16 files changed

+405
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/composer.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 imiphp
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# imi-rpc
2+
3+
[![Latest Version](https://img.shields.io/packagist/v/imiphp/imi-rpc.svg)](https://packagist.org/packages/imiphp/imi-rpc)
4+
[![Php Version](https://img.shields.io/badge/php-%3E=7.1-brightgreen.svg)](https://secure.php.net/)
5+
[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.1.0-brightgreen.svg)](https://github.com/swoole/swoole-src)
6+
[![IMI License](https://img.shields.io/github/license/imiphp/imi-rpc.svg)](https://github.com/imiphp/imi-rpc/blob/master/LICENSE)
7+
8+
## 介绍
9+
10+
在 imi 框架中开发 RPC 服务的基础组件。本组件不提供实际的 RPC 实现,仅提供开发 RPC 服务的一些插槽。
11+
12+
## Composer
13+
14+
本项目可以使用composer安装,遵循psr-4自动加载规则,在你的 `composer.json` 中加入下面的内容:
15+
16+
```json
17+
{
18+
"require": {
19+
"imiphp/imi-rpc": "~1.0"
20+
}
21+
}
22+
```
23+
24+
然后执行 `composer update` 安装。
25+
26+
## 使用
27+
28+
在项目 `config/config.php` 中配置:
29+
30+
```php
31+
[
32+
'components' => [
33+
// 引入本组件
34+
'rpc' => 'Imi\Rpc',
35+
],
36+
]
37+
```
38+
39+
## 免费技术支持
40+
41+
QQ群:17916227 [![点击加群](https://pub.idqqimg.com/wpa/images/group.png "点击加群")](https://jq.qq.com/?_wv=1027&k=5wXf4Zq),如有问题会有人解答和修复。
42+
43+
## 运行环境
44+
45+
- [PHP](https://php.net/) >= 7.1
46+
- [Composer](https://getcomposer.org/)
47+
- [Swoole](https://www.swoole.com/) >= 4.1.0
48+
49+
## 版权信息
50+
51+
`imi-rpc` 遵循 MIT 开源协议发布,并提供免费使用。
52+
53+
## 捐赠
54+
55+
<img src="https://raw.githubusercontent.com/imiphp/imi-rpc/dev/res/pay.png"/>
56+
57+
开源不求盈利,多少都是心意,生活不易,随缘随缘……

composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "imiphp/imi-rpc",
3+
"type": "library",
4+
"license": "MIT",
5+
"description": "在 imi 框架中开发 RPC 服务的基础组件",
6+
"require": {
7+
8+
},
9+
"require-dev": {
10+
"yurunsoft/imi": "dev-dev"
11+
},
12+
"autoload": {
13+
"psr-4" : {
14+
"Imi\\Rpc\\" : "src/"
15+
}
16+
}
17+
}

res/pay.png

140 KB
Loading

src/BaseRpcServer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace Imi\Rpc;
3+
4+
use Imi\Server\Base;
5+
6+
/**
7+
* RPC 服务器基类
8+
*/
9+
abstract class BaseRpcServer extends Base
10+
{
11+
12+
}

src/Controller/RpcController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Imi\Rpc\Controller;
3+
4+
class RpcController
5+
{
6+
7+
}

src/Listener/RouteInit.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
namespace Imi\Rpc\Listener;
3+
4+
use Imi\ServerManage;
5+
use Imi\RequestContext;
6+
use Imi\Event\EventParam;
7+
use Imi\Event\IEventListener;
8+
use Imi\Bean\Annotation\Listener;
9+
use Imi\Server\Route\RouteCallable;
10+
use Imi\Bean\Annotation\AnnotationManager;
11+
use Imi\Rpc\Route\Annotation\RpcRoute;
12+
use Imi\Rpc\Route\Annotation\RpcAction;
13+
use Imi\Rpc\Route\Annotation\Parser\RpcControllerParser;
14+
use Imi\Rpc\BaseRpcServer;
15+
16+
/**
17+
* RPC 服务器路由初始化
18+
* @Listener("IMI.MAIN_SERVER.WORKER.START")
19+
*/
20+
class RouteInit implements IEventListener
21+
{
22+
/**
23+
* 事件处理方法
24+
* @param EventParam $e
25+
* @return void
26+
*/
27+
public function handle(EventParam $e)
28+
{
29+
$this->parseAnnotations($e);
30+
}
31+
32+
/**
33+
* 处理注解路由
34+
* @return void
35+
*/
36+
private function parseAnnotations(EventParam $e)
37+
{
38+
$controllerParser = RpcControllerParser::getInstance();
39+
foreach(ServerManage::getServers() as $name => $server)
40+
{
41+
if(!$server instanceof BaseRpcServer)
42+
{
43+
continue;
44+
}
45+
RequestContext::create();
46+
RequestContext::set('server', $server);
47+
$route = $server->getBean('RpcRoute');
48+
foreach($controllerParser->getByServer($name) as $className => $classItem)
49+
{
50+
$classAnnotation = $classItem['annotation'];
51+
foreach(AnnotationManager::getMethodsAnnotations($className, RpcAction::class) as $methodName => $actionAnnotations)
52+
{
53+
$routes = AnnotationManager::getMethodAnnotations($className, $methodName, RpcRoute::class);
54+
if(!isset($routes[0]))
55+
{
56+
$routes = [
57+
new RpcRoute([
58+
'name' => $methodName,
59+
])
60+
];
61+
}
62+
63+
foreach($routes as $routeItem)
64+
{
65+
$route->addRuleAnnotation($routeItem, new RouteCallable($className, $methodName), [
66+
'serverName' => $name,
67+
'controller' => $classAnnotation,
68+
]);
69+
}
70+
}
71+
}
72+
RequestContext::destroy();
73+
}
74+
}
75+
76+
}

src/Main.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace Imi\Rpc;
3+
4+
use Imi\Main\BaseMain;
5+
6+
class Main extends BaseMain
7+
{
8+
public function __init()
9+
{
10+
11+
}
12+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
namespace Imi\Rpc\Route\Annotation\Parser;
3+
4+
use Imi\Bean\Annotation\Base;
5+
use Imi\Bean\Parser\BaseParser;
6+
use Imi\Util\Traits\TServerAnnotationParser;
7+
use Imi\Rpc\Route\Annotation\RpcController;
8+
use Imi\Event\Event;
9+
10+
/**
11+
* 控制器注解处理器
12+
*/
13+
class RpcControllerParser extends BaseParser
14+
{
15+
use TServerAnnotationParser;
16+
17+
protected $controllerAnnotationClass = RpcController::class;
18+
19+
/**
20+
* 处理方法
21+
* @param \Imi\Bean\Annotation\Base $annotation 注解类
22+
* @param string $className 类名
23+
* @param string $target 注解目标类型(类/属性/方法)
24+
* @param string $targetName 注解目标名称
25+
* @return void
26+
*/
27+
public function parse(\Imi\Bean\Annotation\Base $annotation, string $className, string $target, string $targetName)
28+
{
29+
$eventName = 'IMI.RPC.ANNOTATION.PARSER:' . get_class($annotation);
30+
Event::trigger($eventName, func_get_args(), $this);
31+
}
32+
33+
}

0 commit comments

Comments
 (0)