Skip to content

Commit 908d7ec

Browse files
committed
fix: 修复 fail 函数错误码问题
0 parents  commit 908d7ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+5850
-0
lines changed

.editorconfig

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = false
10+
11+
[*.{vue,js,scss}]
12+
charset = utf-8
13+
indent_style = space
14+
indent_size = 2
15+
end_of_line = lf
16+
insert_final_newline = true
17+
trim_trailing_whitespace = true
18+
19+
[*.md]
20+
trim_trailing_whitespace = false

.gitattributes

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
* text=auto
2+
3+
/tests export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
.scrutinizer.yml export-ignore
7+
.travis.yml export-ignore
8+
phpunit.php export-ignore
9+
phpunit.xml.dist export-ignore
10+
phpunit.xml export-ignore
11+
.php_cs export-ignore

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.idea
2+
*.DS_Store
3+
/vendor
4+
/coverage
5+
sftp-config.json
6+
composer.lock
7+
.subsplit
8+
.php_cs.cache

.php_cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
$header = <<<EOF
3+
This file is part of the zhenmu/support.
4+
5+
(c) mouyong <[email protected]>
6+
7+
This source file is subject to the MIT license that is bundled.
8+
EOF;
9+
10+
return PhpCsFixer\Config::create()
11+
->setRiskyAllowed(true)
12+
->setRules(array(
13+
'@Symfony' => true,
14+
'header_comment' => array('header' => $header),
15+
'array_syntax' => array('syntax' => 'short'),
16+
'ordered_imports' => true,
17+
'no_useless_else' => true,
18+
'no_useless_return' => true,
19+
'php_unit_construct' => true,
20+
'php_unit_strict' => true,
21+
))
22+
->setFinder(
23+
PhpCsFixer\Finder::create()
24+
->exclude('vendor')
25+
->in(__DIR__)
26+
)
27+
;

LaravelReadme.md

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<h1 style="text-align: center;"> support </h1>
2+
3+
## 安装
4+
5+
```shell
6+
$ composer require zhenmu/support -vvv
7+
```
8+
9+
## 使用
10+
11+
1. 通过 `php artisan make:controller` 控制器生成后,继承同目录下的 `Controller` 基类.
12+
2. 编写接口时可通过 `$this->success($data = [], $err_code = 200, $messsage = 'success');` 返回正确数据给接口.
13+
3. 编写接口时可通过 `$this->fail($messsage = '', $err_code = 400);` 返回错误信息给接口.
14+
4.`app/Exceptions/Handler.php``register` 函数中, 注册 `ResponseTrait``renderableHandle`, 示例见下方错误处理.
15+
16+
17+
### 控制器
18+
19+
```php
20+
<?php
21+
22+
namespace App\Http\Controllers;
23+
24+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
25+
use Illuminate\Foundation\Bus\DispatchesJobs;
26+
use Illuminate\Foundation\Validation\ValidatesRequests;
27+
use Illuminate\Routing\Controller as BaseController;
28+
use ZhenMu\Support\Traits\ResponseTrait; // here
29+
30+
class Controller extends BaseController
31+
{
32+
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
33+
use ResponseTrait; // here
34+
}
35+
36+
```
37+
38+
39+
### 错误处理
40+
41+
```php
42+
<?php
43+
44+
namespace App\Exceptions;
45+
46+
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
47+
use Throwable;
48+
use ZhenMu\Support\Traits\ResponseTrait; // here
49+
50+
class Handler extends ExceptionHandler
51+
{
52+
use ResponseTrait; // here
53+
54+
/**
55+
* A list of exception types with their corresponding custom log levels.
56+
*
57+
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
58+
*/
59+
protected $levels = [
60+
//
61+
];
62+
63+
/**
64+
* A list of the exception types that are not reported.
65+
*
66+
* @var array<int, class-string<\Throwable>>
67+
*/
68+
protected $dontReport = [
69+
//
70+
];
71+
72+
/**
73+
* A list of the inputs that are never flashed for validation exceptions.
74+
*
75+
* @var array<int, string>
76+
*/
77+
protected $dontFlash = [
78+
'current_password',
79+
'password',
80+
'password_confirmation',
81+
];
82+
83+
/**
84+
* Register the exception handling callbacks for the application.
85+
*
86+
* @return void
87+
*/
88+
public function register()
89+
{
90+
$this->reportable(function (Throwable $e) {
91+
//
92+
});
93+
94+
$this->renderable($this->renderableHandle()); // here
95+
}
96+
}
97+
98+
```
99+
100+
## 控制器调用
101+
102+
```
103+
<?php
104+
105+
namespace App\Http\Controllers;
106+
107+
use Illuminate\Http\Request;
108+
109+
class DemoController extends Controller
110+
{
111+
public function index()
112+
{
113+
// validate data
114+
\validator()->validate(\request(), [
115+
'name' => 'required|string',
116+
'age' => 'nullable|integer',
117+
]);
118+
119+
// your business logic
120+
$error = false;
121+
if ($error) { // here business logic error.
122+
throw new \RuntimeException('error message');
123+
}
124+
125+
return $this->success([ // here response success
126+
'key1' => 'value1',
127+
'key2' => 'value2',
128+
]);
129+
}
130+
}
131+
132+
```

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<h1 style="text-align: center;"> support </h1>
2+
3+
## 简介
4+
5+
php 基础支持,详细内容见 `src/Utils``src/Traits` 目录。
6+
7+
项目自动拆分,如需跟踪源码更新情况,请前往:https://github.com/plugins-world/packages 查看 PhpSupport 目录
8+
9+
10+
## 安装
11+
12+
```shell
13+
$ composer require zhenmu/support -vvv
14+
```
15+
16+
17+
## 使用
18+
19+
- [laravel 使用示例](/LaravelReadme.md)
20+
- [webman 使用示例](/WebmanReadme.md)

WebmanReadme.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<h1 style="text-align: center;"> support </h1>
2+
3+
## 安装
4+
5+
```shell
6+
$ composer require zhenmu/support -vvv
7+
```
8+
9+
## 使用
10+
11+
1. 通过 `./webman make:controller` 控制器生成后,继承同目录下的 `WebmanBaseController` 基类。
12+
2. 编写接口时可通过 `$this->success($data = [], $err_code = 200, $messsage = 'success');` 返回正确数据给接口。
13+
3. 编写接口时可通过 `$this->fail($messsage = '', $err_code = 400);` 返回错误信息给接口。
14+
4.`support/exception/Handler.php``render` 函数中,调用 `WebmanResponseTrait``$this->renderableHandle($request, $exception);` 示例见下方错误处理。
15+
16+
17+
### 控制器
18+
19+
```php
20+
<?php
21+
22+
namespace support\exception;
23+
24+
use Webman\Http\Request;
25+
use Webman\Http\Response;
26+
use Throwable;
27+
use Webman\Exception\ExceptionHandler;
28+
use ZhenMu\Support\Traits\WebmanResponseTrait;
29+
30+
/**
31+
* Class Handler
32+
* @package support\exception
33+
*/
34+
class Handler extends ExceptionHandler
35+
{
36+
use WebmanResponseTrait; // 这里需要引入 WebmanResponseTrait
37+
38+
public $dontReport = [
39+
BusinessException::class,
40+
];
41+
42+
public function report(Throwable $exception)
43+
{
44+
parent::report($exception);
45+
}
46+
47+
public function render(Request $request, Throwable $exception): Response
48+
{
49+
return $this->renderableHandle($request, $exception); // 这里进行调用,做了一些错误捕捉
50+
}
51+
}
52+
```
53+
54+
## 控制器调用
55+
56+
```
57+
<?php
58+
59+
namespace app\controller;
60+
61+
class DemoController extends WebmanBaseController
62+
{
63+
public function index()
64+
{
65+
// validate data
66+
\validator()->validate(\request(), [
67+
'name' => 'required|string',
68+
'age' => 'nullable|integer',
69+
]);
70+
71+
// your logic
72+
$error = false;
73+
if ($error) {
74+
throw new \RuntimeException('error message');
75+
}
76+
77+
return $this->success([
78+
'key1' => 'value1',
79+
'key2' => 'value2',
80+
]);
81+
}
82+
}
83+
```

composer.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "zhenmu/support",
3+
"description": "Php 代码扩展包, 一些常用的 php 代码封装",
4+
"keywords": ["support", "mouyong", "zhenmu", "laravel", "webman", "aes", "php", "composer", "xml", "uuid", "url", "state", "file", "tool", "utils", "util"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "mouyong",
9+
"email": "[email protected]",
10+
"homepage": "https://github.com/mouyong",
11+
"role": "Creator & Developer"
12+
}
13+
],
14+
"support": {
15+
"issues": "https://github.com/plugins-world/php-support/issues",
16+
"source": "https://github.com/plugins-world/php-support",
17+
"homepage": "https://laravel-workerman.iwnweb.com/d/22-php-composer"
18+
},
19+
"require": {
20+
"ext-json": "*",
21+
"ext-openssl": "*",
22+
"ext-fileinfo": "*",
23+
"ext-simplexml": "*",
24+
"league/fractal": "^0.20.1",
25+
"ramsey/uuid": "*",
26+
"symfony/process": "*",
27+
"nelexa/zip": "^4.0"
28+
},
29+
"autoload": {
30+
"files": [
31+
"src/helpers.php"
32+
],
33+
"psr-4": {
34+
"ZhenMu\\Support\\": "src"
35+
}
36+
}
37+
}

examples/example_rsa_chunk.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
// 定义 RSA 加密的密钥长度
4+
define("KEY_SIZE", 2048);
5+
6+
// 生成 RSA 加密的公钥和私钥
7+
$res = openssl_pkey_new(array(
8+
"private_key_bits" => KEY_SIZE,
9+
"private_key_type" => OPENSSL_KEYTYPE_RSA,
10+
));
11+
12+
// 将私钥转换为字符串
13+
openssl_pkey_export($res, $privateKey);
14+
15+
// 从私钥中得到公钥
16+
$publicKey = openssl_pkey_get_details($res)["key"];
17+
18+
// 需要加密的数据
19+
$plaintext = "Hello, World!";
20+
21+
// RSA 加密,将需要加密的数据按照最大加密长度分块
22+
$chunkSize = KEY_SIZE / 8 - 11;
23+
$output = "";
24+
while ($plaintext) {
25+
$chunk = substr($plaintext, 0, $chunkSize);
26+
$plaintext = substr($plaintext, $chunkSize);
27+
openssl_public_encrypt($chunk, $encrypted, $publicKey);
28+
$output .= $encrypted;
29+
}
30+
31+
// 对 RSA 加密后的数据进行解密
32+
$plaintext = "";
33+
while ($output) {
34+
$chunk = substr($output, 0, KEY_SIZE / 8);
35+
$output = substr($output, KEY_SIZE / 8);
36+
openssl_private_decrypt($chunk, $decrypted, $privateKey);
37+
$plaintext .= $decrypted;
38+
}
39+
40+
// 输出解密后的数据
41+
echo $plaintext;
42+
43+
?>

0 commit comments

Comments
 (0)