File tree Expand file tree Collapse file tree 5 files changed +151
-0
lines changed
Expand file tree Collapse file tree 5 files changed +151
-0
lines changed Original file line number Diff line number Diff line change @@ -138,3 +138,59 @@ class IndexController extends Controller
138138}
139139
140140```
141+
142+ ### 使用 Cloudflare
143+
144+ 安装组件
145+
146+ ``` shell
147+ composer require cloudflare/sdk
148+ ```
149+
150+ 设置环境变量
151+
152+ ``` dotenv
153+ DNS_CLOUD_CLOUDFLARE_EMAIL="xxx"
154+ DNS_CLOUD_CLOUDFLARE_API_KEY="xxx"
155+ ```
156+
157+ 编写测试代码,我们直接把代码全部写到控制器里,开发者请按照实际情况酌情处理
158+
159+ ``` php
160+ <?php
161+
162+ declare(strict_types=1);
163+ /**
164+ * This file is part of Hyperf.
165+ *
166+ * @link https://www.hyperf.io
167+ * @document https://hyperf.wiki
168+ * @contact group@hyperf.io
169+ * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
170+ */
171+
172+ namespace App\Controller;
173+
174+ use Cloudflare\API\Adapter\Guzzle;
175+ use Cloudflare\API\Endpoints\User;
176+ use Gemini\DnsCloud\Factory;
177+ use Hyperf\Di\Annotation\Inject;
178+
179+ class IndexController extends Controller
180+ {
181+ #[Inject]
182+ protected Factory $factory;
183+
184+ public function index()
185+ {
186+ /** @var Guzzle $client */
187+ $client = $this->factory->get('cloudflare')->client();
188+
189+ $user = new User($client);
190+
191+ return $this->response->success($user->getUserID());
192+ }
193+ }
194+
195+ ```
196+
Original file line number Diff line number Diff line change 2727 "require-dev" : {
2828 "alibabacloud/alidns-20150109" : " ^3.0" ,
2929 "alibabacloud/darabonba-openapi" : " ^0.2.9" ,
30+ "cloudflare/sdk" : " ^1.3" ,
3031 "friendsofphp/php-cs-fixer" : " ^3.0" ,
3132 "hyperf/config" : " ^3.0" ,
3233 "hyperf/support" : " ^3.0" ,
Original file line number Diff line number Diff line change 1010 * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
1111 */
1212use Gemini \DnsCloud \Client \Aliyun \AliyunClient ;
13+ use Gemini \DnsCloud \Client \Cloudflare \CloudflareClient ;
1314use Gemini \DnsCloud \Client \Tencent \TencentClient ;
1415
1516use function Hyperf \Support \env ;
2526 'secret_key ' => env ('DNS_CLOUD_TENCENT_SECRET_KEY ' ),
2627 'client ' => TencentClient::class,
2728 ],
29+ 'cloudflare ' => [
30+ 'email ' => env ('DNS_CLOUD_CLOUDFLARE_EMAIL ' ),
31+ 'api_key ' => env ('DNS_CLOUD_CLOUDFLARE_API_KEY ' ),
32+ 'client ' => CloudflareClient::class,
33+ ],
2834];
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+ /**
5+ * This file is part of Hyperf.
6+ *
7+ * @link https://www.hyperf.io
8+ * @document https://hyperf.wiki
9+ * @contact group@hyperf.io
10+ * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+ */
12+
13+ namespace Gemini \DnsCloud \Client \Cloudflare ;
14+
15+ use Cloudflare \API \Adapter \Guzzle ;
16+ use Cloudflare \API \Auth \APIKey ;
17+ use Gemini \DnsCloud \Client \ClientInterface ;
18+
19+ class CloudflareClient implements ClientInterface
20+ {
21+ protected APIKey $ key ;
22+
23+ protected string $ email ;
24+
25+ protected string $ apiKey ;
26+
27+ public function __construct (array $ config = [])
28+ {
29+ $ this ->email = $ config ['email ' ];
30+ $ this ->apiKey = $ config ['api_key ' ];
31+
32+ $ this ->key = new APIKey ($ this ->email , $ this ->apiKey );
33+ }
34+
35+ public function client (): mixed
36+ {
37+ return new Guzzle ($ this ->key );
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+ /**
5+ * This file is part of Hyperf.
6+ *
7+ * @link https://www.hyperf.io
8+ * @document https://hyperf.wiki
9+ * @contact group@hyperf.io
10+ * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+ */
12+
13+ namespace HyperfTest \Cases ;
14+
15+ use Cloudflare \API \Adapter \Guzzle ;
16+ use Cloudflare \API \Endpoints \User ;
17+ use Gemini \DnsCloud \Client \Cloudflare \CloudflareClient ;
18+ use Gemini \DnsCloud \Factory ;
19+ use Hyperf \Config \Config ;
20+
21+ /**
22+ * @internal
23+ * @coversNothing
24+ */
25+ class CloudflareTest extends AbstractTestCase
26+ {
27+ public function testGetUserID ()
28+ {
29+ $ this ->markTestSkipped ('暂时跳过 ' );
30+
31+ $ config = new Config ([
32+ 'dns_cloud ' => [
33+ 'cloudflare ' => [
34+ 'email ' => 'xxx ' ,
35+ 'api_key ' => 'xxx ' ,
36+ 'client ' => CloudflareClient::class,
37+ ],
38+ ],
39+ ]);
40+
41+ $ factory = new Factory ($ config );
42+
43+ /** @var Guzzle $client */
44+ $ client = $ factory ->get ('cloudflare ' )->client ();
45+ $ user = new User ($ client );
46+
47+ $ this ->assertIsString ($ user ->getUserID ());
48+ }
49+ }
You can’t perform that action at this time.
0 commit comments