Skip to content

Commit 82787dc

Browse files
authored
新增获取应用请求地址方法 (#587)
* 新增获取应用请求地址方法 * 修复测试 * 修复测试 * 优化代码
1 parent 04d3676 commit 82787dc

File tree

29 files changed

+199
-6
lines changed

29 files changed

+199
-6
lines changed

dev/bootstrap.php

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Imi\Event\Event;
6+
use Imi\Util\Uri;
67
use Rector\Config\RectorConfig;
78
use Rector\Set\ValueObject\LevelSetList;
89
use SebastianBergmann\CodeCoverage\CodeCoverage;
@@ -12,6 +13,29 @@
1213

1314
ini_set('date.timezone', 'Asia/Shanghai');
1415

16+
const TEST_APP_URI_CONFIG = [
17+
'host' => 'imi-test',
18+
'port' => 1234,
19+
'scheme' => 'https',
20+
'user' => 'root',
21+
'pass' => '123',
22+
'path' => '/test',
23+
'query' => 'id=666',
24+
'fragment' => 'test',
25+
];
26+
const TEST_APP_URI = 'https://root:123@imi-test:1234/test?id=666#test';
27+
function testAppCallbackUriConfig(Uri $uri): Uri
28+
{
29+
return $uri->withHost('imi-test-callback')
30+
->withPort(6666)
31+
->withScheme('https')
32+
->withUserInfo('admin', '123456')
33+
->withPath('/testCallback')
34+
->withQuery('id=999')
35+
->withFragment('testCallback');
36+
}
37+
const TEST_APP_CALLBACK_URI = 'https://admin:123456@imi-test-callback:6666/testCallback?id=999#testCallback';
38+
1539
function getRectorConfigCallback(string $path): callable
1640
{
1741
// @phpstan-ignore-next-line

dev/generate-request-context-proxy.sh

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ __DIR__=$(cd `dirname $0`; pwd)
44

55
cd $__DIR__/../
66

7+
rm -f src/Server/Http/Message/Proxy/RequestProxy.php \
8+
&& rm -f src/Server/Http/Message/Proxy/RequestProxyObject.php \
9+
&& rm -f src/Server/Http/Message/Proxy/ResponseProxy.php \
10+
&& rm -f src/Server/Http/Message/Proxy/ResponseProxyObject.php \
11+
&& rm -f src/Server/TcpServer/Message/Proxy/ReceiveDataProxy.php \
12+
&& rm -f src/Server/UdpServer/Message/Proxy/PacketDataProxy.php \
13+
&& rm -f src/Server/WebSocket/Message/Proxy/FrameProxy.php
14+
715
src/Cli/bin/imi-cli --app-namespace "Imi" generate/requestContextProxy --target "Imi\Server\Http\Message\Proxy\RequestProxy" --class "Imi\Server\Http\Message\Contract\IHttpRequest" --name request && \
816

917
src/Cli/bin/imi-cli --app-namespace "Imi" generate/requestContextProxy --target "Imi\Server\Http\Message\Proxy\RequestProxyObject" --class "Imi\Server\Http\Message\Contract\IHttpRequest" --name request --bean HttpRequestProxy --interface "Imi\Server\Http\Message\Contract\IHttpRequest" --recursion=false && \

doc/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
## 多容器
4848

4949
* [PHP-FPM](container/php-fpm.md)
50+
* [服务器配置](container/php-fpm/serverConfig.md)
5051
* [Swoole](container/swoole.md)
5152
* [Swoole 环境安装教程](base/env.md)
5253
* [子服务器(单项目多端口多协议)](core/subServer.md)

doc/components/httpserver/request.md

+12
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ request 数据包含 get/post/cookie
133133
> 可在`nginx`配置`location`中添加 `proxy_set_header X-Forwarded-Proto $scheme;`
134134
> 通过获取请求头`$this->request->getHeaderLine('x-forwarded-proto');`来获取对应的`scheme`
135135
136+
### 获取应用请求地址
137+
138+
`public function getAppUri(?string $serverName = null)`
139+
140+
`getUri()` 不同的是,可以通过配置修改 `getUri()` 获取到的 Uri 里的 `host` 等参数。
141+
142+
适合用于替换生产环境中的 https、域名等参数。
143+
144+
需要在服务器配置中修改,详见对应容器服务器配置。
145+
146+
`$serverName` 默认不传则使用当前服务器。
147+
136148
### 获取 Swoole 服务器对象
137149

138150
`public function getServerInstance(): \Imi\Swoole\Server\Http\Server`

doc/container/php-fpm/serverConfig.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 服务器配置
2+
3+
[toc]
4+
5+
```php
6+
'fpmServer' => [
7+
// $request->getAppUri() 参数替换,每个参数都是可选项
8+
// 下面例子最终获取到的 Uri 为:https://root:123@imi-test:1234/test?id=666#test
9+
'appUri' => [
10+
'host' => 'imi-test', // 主机名
11+
'port' => 1234, // 端口
12+
'scheme' => 'https', // 协议
13+
'user' => 'root', // 用户名
14+
'pass' => '123', // 密码
15+
'path' => '/test', // 路径
16+
'query' => 'id=666', // 查询参数
17+
'fragment' => 'test', // 锚点
18+
],
19+
// 也支持回调
20+
'appUri' => function(\Imi\Util\Uri $uri) {
21+
return $uri->withHost('imi-test');
22+
},
23+
],
24+
```

doc/container/roadrunner/serverConfig.md

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@
1010
'roadRunnerServer' => [
1111
'main' => [
1212
'namespace' => 'ImiApp\ApiServer', // Http 服务的命名空间,为空则使用项目命名空间
13+
// $request->getAppUri() 参数替换,每个参数都是可选项
14+
// 下面例子最终获取到的 Uri 为:https://root:123@imi-test:1234/test?id=666#test
15+
'appUri' => [
16+
'host' => 'imi-test', // 主机名
17+
'port' => 1234, // 端口
18+
'scheme' => 'https', // 协议
19+
'user' => 'root', // 用户名
20+
'pass' => '123', // 密码
21+
'path' => '/test', // 路径
22+
'query' => 'id=666', // 查询参数
23+
'fragment' => 'test', // 锚点
24+
],
25+
// 也支持回调
26+
'appUri' => function(\Imi\Util\Uri $uri) {
27+
return $uri->withHost('imi-test');
28+
},
1329
],
1430
],
1531

doc/container/workerman/serverConfig.md

+16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ return [
2323
'beans' => [
2424
'aaa' => XXX::class,
2525
],
26+
// $request->getAppUri() 参数替换,每个参数都是可选项
27+
// 下面例子最终获取到的 Uri 为:https://root:123@imi-test:1234/test?id=666#test
28+
'appUri' => [
29+
'host' => 'imi-test', // 主机名
30+
'port' => 1234, // 端口
31+
'scheme' => 'https', // 协议
32+
'user' => 'root', // 用户名
33+
'pass' => '123', // 密码
34+
'path' => '/test', // 路径
35+
'query' => 'id=666', // 查询参数
36+
'fragment' => 'test', // 锚点
37+
],
38+
// 也支持回调
39+
'appUri' => function(\Imi\Util\Uri $uri) {
40+
return $uri->withHost('imi-test');
41+
},
2642
],
2743
// 下面可以继续加入其它协议其它端口的服务
2844
'websocket' => [

doc/core/subServer.md

+16
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ http 和 WebSocket 同时做在一个项目中,代码之间没有隔阂,可
4141
'beans' => [
4242
'aaa' => XXX::class,
4343
],
44+
// $request->getAppUri() 参数替换,每个参数都是可选项
45+
// 下面例子最终获取到的 Uri 为:https://root:123@imi-test:1234/test?id=666#test
46+
'appUri' => [
47+
'host' => 'imi-test', // 主机名
48+
'port' => 1234, // 端口
49+
'scheme' => 'https', // 协议
50+
'user' => 'root', // 用户名
51+
'pass' => '123', // 密码
52+
'path' => '/test', // 路径
53+
'query' => 'id=666', // 查询参数
54+
'fragment' => 'test', // 锚点
55+
],
56+
// 也支持回调
57+
'appUri' => function(\Imi\Util\Uri $uri) {
58+
return $uri->withHost('imi-test');
59+
},
4460
],
4561
],
4662
]

split-repository/composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"require": {
33
"knplabs/github-api": "^3.0",
44
"guzzlehttp/guzzle": "^7.0.1",
5-
"http-interop/http-factory-guzzle": "^1.0"
5+
"http-interop/http-factory-guzzle": "^1.0",
6+
"psr/http-message": "~1.0"
67
},
78
"autoload": {
89
"psr-4": {
@@ -14,4 +15,4 @@
1415
"php-http/discovery": false
1516
}
1617
}
17-
}
18+
}

src/Components/fpm/src/FpmApp.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function run(): void
9090
$server = ServerManager::getServer('main');
9191
if (null === $server)
9292
{
93-
$server = ServerManager::createServer('main', [
93+
$server = ServerManager::createServer('main', Config::get('@app.fpmServer') + [
9494
'type' => Type::HTTP,
9595
'namespace' => $this->namespace,
9696
]);

src/Components/fpm/tests/HttpServer/Tests/RequestTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Imi\Fpm\Test\Web\Tests;
66

77
use Imi\Util\Http\Consts\MediaType;
8+
use Imi\Util\Uri;
89
use Yurun\Util\HttpRequest;
910
use Yurun\Util\YurunHttp\Http\Psr7\UploadedFile;
1011

@@ -210,6 +211,7 @@ public function testUri(): void
210211
$response = $http->get($uri);
211212
$data = $response->json(true);
212213
$this->assertEquals($uri, $data['uri'] ?? null);
214+
$this->assertEquals(TEST_APP_CALLBACK_URI, $data['appUri'] ?? null);
213215
}
214216

215217
/**

src/Components/fpm/tests/Web/Controller/IndexController.php

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public function info()
123123
'server' => $request->getServerParams(),
124124
'request' => $request->request(),
125125
'uri' => (string) $request->getUri(),
126+
'appUri' => (string) $request->getAppUri(),
126127
];
127128
}
128129

src/Components/fpm/tests/Web/config/config.php

+4
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,8 @@
107107
],
108108
],
109109
],
110+
111+
'fpmServer' => [
112+
'appUri' => 'testAppCallbackUriConfig',
113+
],
110114
];

src/Components/roadrunner/src/RoadRunnerApp.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ public function loadRuntime(): int
8989
public function run(): void
9090
{
9191
$config = Config::get('@app.roadRunnerServer.main', []);
92-
$server = ServerManager::createServer('main', [
92+
$server = ServerManager::createServer('main', $config + [
9393
'type' => Type::HTTP,
94-
'namespace' => $config['namespace'] ?? $this->namespace,
94+
'namespace' => $this->namespace,
9595
]);
9696
Event::trigger('IMI.APP.INIT', [], $this);
9797
$server->start();

src/Components/roadrunner/tests/unit/HttpServer/Controller/IndexController.php

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public function info()
121121
'server' => $request->getServerParams(),
122122
'request' => $request->request(),
123123
'uri' => (string) $request->getUri(),
124+
'appUri' => (string) $request->getAppUri(),
124125
];
125126
}
126127

src/Components/roadrunner/tests/unit/HttpServer/Tests/RequestTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ public function testUri(): void
210210
$response = $http->get($uri);
211211
$data = $response->json(true);
212212
$this->assertEquals($uri, $data['uri'] ?? null);
213+
$this->assertEquals(TEST_APP_URI, $data['appUri'] ?? null);
213214
}
214215

215216
/**

src/Components/roadrunner/tests/unit/HttpServer/config/config.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
'RoadRunner' => 'Imi\RoadRunner',
1313
],
1414
'roadRunnerServer' => [
15-
'main' => [],
15+
'main' => [
16+
'appUri' => TEST_APP_URI_CONFIG,
17+
],
1618
],
1719
'ignorePaths' => [
1820
\dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'bin',

src/Components/swoole/tests/unit/HttpServer/ApiServer/Controller/IndexController.php

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public function info(): array
147147
'server' => $request->getServerParams(),
148148
'request' => $request->request(),
149149
'uri' => (string) $request->getUri(),
150+
'appUri' => (string) $request->getAppUri(),
150151
];
151152
}
152153

src/Components/swoole/tests/unit/HttpServer/HttpsTestServer/Controller/IndexController.php

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function info(): array
3131
'server' => $request->getServerParams(),
3232
'request' => $request->request(),
3333
'uri' => (string) $request->getUri(),
34+
'appUri' => (string) $request->getAppUri(),
3435
];
3536
}
3637
}

src/Components/swoole/tests/unit/HttpServer/Tests/HttpsTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function testUri(): void
3030
$response = $http->get($uri);
3131
$data = $response->json(true);
3232
$this->assertEquals($uri, $data['uri'] ?? null);
33+
$this->assertEquals($uri, $data['appUri'] ?? null);
3334
}
3435

3536
/**

src/Components/swoole/tests/unit/HttpServer/Tests/RequestTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ public function testUri(): void
245245
$response = $http->get($uri);
246246
$data = $response->json(true);
247247
$this->assertEquals($uri, $data['uri'] ?? null);
248+
$this->assertEquals(TEST_APP_URI, $data['appUri'] ?? null);
248249
}
249250

250251
/**

src/Components/swoole/tests/unit/HttpServer/config/config.php

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
'task_worker_num' => 1,
8484
'max_wait_time' => 30,
8585
],
86+
'appUri' => TEST_APP_URI_CONFIG,
8687
],
8788

8889
// 子服务器(端口监听)配置

src/Components/workerman/tests/unit/AppServer/ApiServer/Controller/IndexController.php

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public function info(): array
122122
'server' => $request->getServerParams(),
123123
'request' => $request->request(),
124124
'uri' => (string) $request->getUri(),
125+
'appUri' => (string) $request->getAppUri(),
125126
];
126127
}
127128

src/Components/workerman/tests/unit/AppServer/Tests/Http/RequestTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ public function testUri(): void
211211
$response = $http->get($uri);
212212
$data = $response->json(true);
213213
$this->assertEquals($uri, $data['uri'] ?? null);
214+
$this->assertEquals(TEST_APP_URI, $data['appUri'] ?? null);
214215
}
215216

216217
/**

src/Components/workerman/tests/unit/AppServer/config/config.php

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
'port' => 13000,
7979
'configs' => [
8080
],
81+
'appUri' => TEST_APP_URI_CONFIG,
8182
],
8283
'websocket' => [
8384
'namespace' => 'Imi\Workerman\Test\AppServer\WebSocketServer',

src/Server/Http/Message/Proxy/RequestProxy.php

+2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@
118118
* @method static \Imi\Util\Http\Contract\IRequest setMethod(string $method)
119119
* @method \Imi\Util\Http\Contract\IRequest setUri(\Psr\Http\Message\UriInterface $uri, bool $preserveHost = false)
120120
* @method static \Imi\Util\Http\Contract\IRequest setUri(\Psr\Http\Message\UriInterface $uri, bool $preserveHost = false)
121+
* @method \Psr\Http\Message\UriInterface getAppUri(?string $serverName = NULL)
122+
* @method static \Psr\Http\Message\UriInterface getAppUri(?string $serverName = NULL)
121123
*/
122124
class RequestProxy extends BaseRequestContextProxy
123125
{

src/Server/Http/Message/Proxy/RequestProxyObject.php

+10
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@
121121
* @method static \Imi\Util\Http\Contract\IRequest setMethod(string $method)
122122
* @method \Imi\Util\Http\Contract\IRequest setUri(\Psr\Http\Message\UriInterface $uri, bool $preserveHost = false)
123123
* @method static \Imi\Util\Http\Contract\IRequest setUri(\Psr\Http\Message\UriInterface $uri, bool $preserveHost = false)
124+
* @method \Psr\Http\Message\UriInterface getAppUri(?string $serverName = NULL)
125+
* @method static \Psr\Http\Message\UriInterface getAppUri(?string $serverName = NULL)
124126
*/
125127
class RequestProxyObject extends BaseRequestContextProxy implements \Imi\Server\Http\Message\Contract\IHttpRequest
126128
{
@@ -555,4 +557,12 @@ public function setUri(\Psr\Http\Message\UriInterface $uri, bool $preserveHost =
555557
{
556558
return self::__getProxyInstance()->setUri($uri, $preserveHost);
557559
}
560+
561+
/**
562+
* {@inheritDoc}
563+
*/
564+
public function getAppUri(?string $serverName = null): \Psr\Http\Message\UriInterface
565+
{
566+
return self::__getProxyInstance()->getAppUri($serverName);
567+
}
558568
}

src/Util/Http/Contract/IRequest.php

+2
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,6 @@ public function setMethod(string $method): self;
7070
* @return static
7171
*/
7272
public function setUri(UriInterface $uri, bool $preserveHost = false): self;
73+
74+
public function getAppUri(?string $serverName = null): UriInterface;
7375
}

0 commit comments

Comments
 (0)