Skip to content

Commit 893a90b

Browse files
authored
Merge pull request #10 from webman-tech/dev
Dev PresetHelper
2 parents 046a10b + 439ca57 commit 893a90b

37 files changed

Lines changed: 1751 additions & 55 deletions

.github/workflows/tests.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: phpunit
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
pull_request:
8+
# schedule:
9+
# - cron: '0 0 * * *'
10+
11+
jobs:
12+
phpunit:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
php: [7.4, 8.0, 8.1, 8.2, 8.3, 8.4]
17+
stability: [
18+
#prefer-lowest, 问题较多
19+
prefer-stable
20+
]
21+
22+
name: PHP ${{ matrix.php }} - ${{ matrix.stability }}
23+
24+
steps:
25+
- name: Checkout
26+
uses: actions/checkout@v3
27+
28+
- name: Setup PHP
29+
uses: shivammathur/setup-php@v2
30+
with:
31+
php-version: ${{ matrix.php }}
32+
coverage: xdebug
33+
#extensions: 'redis'
34+
#ini-values: error_reporting=E_ALL php8.4 下 nullable is deprecated 暂时未完全解决,因此暂时不启用
35+
tools: composer:v2
36+
37+
- name: Get composer cache directory
38+
id: composer-cache
39+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
40+
41+
- name: Cache dependencies
42+
uses: actions/cache@v3
43+
with:
44+
path: ${{ steps.composer-cache.outputs.dir }}
45+
key: php${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
46+
restore-keys: php${{ matrix.php }}-${{ matrix.stability }}-composer-
47+
48+
- name: Install dependencies
49+
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress
50+
51+
- name: Run test
52+
run: composer test

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ vendor
44
.vscode
55
.phpunit*
66
composer.lock
7+
8+
# webman 1.6 以上版本 BASE_PATH 限定在根目录,为了 tests 排除
9+
/config
10+
/tests/helpers.php

composer.json

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,30 @@
1111
"src/helper.php"
1212
]
1313
},
14+
"autoload-dev": {
15+
"psr-4": {
16+
"Tests\\": "tests/"
17+
}
18+
},
1419
"require": {
15-
"php": ">=7.4",
20+
"php": "^7.4||^8.0",
1621
"ext-json": "*"
1722
},
1823
"require-dev": {
19-
"workerman/webman-framework": "^1.3",
20-
"illuminate/database": "^8.83",
21-
"illuminate/pagination": "^8.83",
22-
"illuminate/validation": "^8.83",
23-
"webman-tech/polyfill": "^1.0"
24+
"workerman/webman-framework": "^1.5|^2.0",
25+
"illuminate/database": "^8.0|^9.0|^10.0|^11.0",
26+
"illuminate/pagination": "^8.0|^9.0|^10.0|^11.0",
27+
"illuminate/validation": "^8.0|^9.0|^10.0|^11.0",
28+
"webman-tech/polyfill": "^1.0|^2.0",
29+
"pestphp/pest": "^1.23|^2.0",
30+
"symfony/var-dumper": "^5.4|^6.0|^7.0"
31+
},
32+
"config": {
33+
"allow-plugins": {
34+
"pestphp/pest-plugin": true
35+
}
36+
},
37+
"scripts": {
38+
"test": "pest"
2439
}
2540
}

docs/common_usage.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
<details>
44
<summary>如何修改 crud 的详情打开的 dialog 的 size</summary>
5+
56
在对应的 controller 中添加以下配置
67
<pre>
7-
/**
8-
* @inheritdoc
9-
*/
8+
// 修改该参数,对于 新增、修改、明细 都使用 lg 的 dialog
9+
protected ?array $defaultDialogConfig = [
10+
'size' => 'lg',
11+
];
12+
13+
// 单独控制
1014
protected function gridActionsConfig(): array
1115
{
1216
return [
17+
// 单独配置 detail
1318
'schema_detail' => [
1419
'dialog' => [
1520
'size' => 'lg',
@@ -18,4 +23,33 @@ protected function gridActionsConfig(): array
1823
];
1924
}
2025
</pre>
26+
</details>
27+
28+
<details>
29+
<summary>如何全局配置一个 amis 的组件</summary>
30+
31+
在 config 的 amis 中 components 中添加以下配置
32+
<pre>
33+
return [
34+
// ... 其他配置
35+
/*
36+
* 用于全局替换组件的默认参数
37+
* @see Component::$config
38+
*/
39+
'components' => [
40+
// 例如: 将列表页的字段默认左显示
41+
/*\WebmanTech\AmisAdmin\Amis\GridColumn::class => [
42+
'schema' => [
43+
'align' => 'left',
44+
],
45+
],*/
46+
// typeXxx,xxx 未 amis 的组件 type,通过 schema 会全局注入到每个 type 组件
47+
'typeImage' => [
48+
'schema' => [
49+
'enlargeAble' => true,
50+
],
51+
],
52+
],
53+
];
54+
</pre>
2155
</details>

phpunit.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="tests/bootstrap.php"
5+
colors="true"
6+
>
7+
<testsuites>
8+
<testsuite name="Test Suite">
9+
<directory suffix="Test.php">./tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
<coverage processUncoveredFiles="true">
13+
<include>
14+
<directory suffix=".php">./app</directory>
15+
<directory suffix=".php">./src</directory>
16+
</include>
17+
</coverage>
18+
</phpunit>

src/Amis.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ private function getAssets(): array
131131
return $item;
132132
}, $assets['js']);
133133

134+
$assets['lang'] = $assets['lang'] ?? 'zh';
135+
if (is_callable($assets['lang'])) {
136+
$assets['lang'] = call_user_func($assets['lang']);
137+
}
138+
139+
$assets['locale'] = $assets['locale'] ?? 'zh-CN';
140+
if (is_callable($assets['locale'])) {
141+
$assets['locale'] = call_user_func($assets['locale']);
142+
}
143+
134144
return $assets;
135145
}
136146
}

src/Amis/Component.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace WebmanTech\AmisAdmin\Amis;
44

5+
use Illuminate\Support\Str;
56
use WebmanTech\AmisAdmin\Helper\ArrayHelper;
67
use WebmanTech\AmisAdmin\Helper\ConfigHelper;
78
use support\Container;
@@ -36,7 +37,7 @@ public function __construct()
3637
public static function make(array $schema = null)
3738
{
3839
/** @var static $component */
39-
$component = Container::get(static::class);
40+
$component = clone Container::get(static::class);
4041
if ($schema) {
4142
$component->schema($schema);
4243
}
@@ -88,6 +89,9 @@ public function toArray(): array
8889
protected function deepToArray(array $arr): array
8990
{
9091
$newArr = [];
92+
if (isset($arr['type']) && $arr['type']) {
93+
$arr = $this->mergeGlobalConfigWithType($arr['type'], $arr);
94+
}
9195
foreach ($arr as $key => $item) {
9296
if (is_array($item)) {
9397
$item = $this->deepToArray($item);
@@ -119,4 +123,31 @@ protected function merge(...$arrays): array
119123
{
120124
return ArrayHelper::merge(...$arrays);
121125
}
126+
127+
/**
128+
* 合并全局的 配置 参数
129+
* @param string $type
130+
* @param array $schema
131+
* @return array
132+
*/
133+
protected function mergeGlobalConfigWithType(string $type, array $schema): array
134+
{
135+
$componentTypeName = 'type' . Str::studly($type);
136+
$componentConfig = ConfigHelper::get('components.' . $componentTypeName, [], true);
137+
if ($componentConfig === [] && Str::contains($type, 'static-')) {
138+
// 支持兼容 typeStaticImage 到 typeImage 的配置
139+
$componentTypeName = str_replace('typeStatic', 'type', $componentTypeName);
140+
$componentConfig = ConfigHelper::get('components.' . $componentTypeName, [], true);
141+
}
142+
if ($globalSchema = $componentConfig['schema'] ?? []) {
143+
if (isset($globalSchema['type'])) {
144+
$schema['type'] = $globalSchema['type']; // 允许做全局的 type 修改,这样可以做自定义组件
145+
}
146+
$schema = $this->merge($globalSchema, $schema);
147+
if ($schema['type'] !== $type) {
148+
return $this->mergeGlobalConfigWithType($schema['type'], $schema);
149+
}
150+
}
151+
return $schema;
152+
}
122153
}

src/Amis/Crud.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function withColumns(array $columns)
105105
*/
106106
public function withCreate(string $api, array $form, string $can = '1==1')
107107
{
108-
$label = $this->config['schema_create']['label'] ?? '新增';
108+
$label = $this->config['schema_create']['label'] ?? trans('新增', [], 'amis-admin');
109109
return $this->withButtonDialog(static::INDEX_CREATE, $label, $form, $this->merge([
110110
'api' => $api,
111111
'level' => 'primary',

src/Amis/DetailAttribute.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace WebmanTech\AmisAdmin\Amis;
44

5+
use WebmanTech\AmisAdmin\Amis\Traits\ComponentCommonFn;
6+
57
/**
68
* 详情的一个字段
79
* @link https://aisuda.bce.baidu.com/amis/zh-CN/components/form/static
@@ -36,6 +38,8 @@
3638
*/
3739
class DetailAttribute extends Component
3840
{
41+
use ComponentCommonFn;
42+
3943
protected array $schema = [
4044
'type' => 'static',
4145
'name' => '',
@@ -65,12 +69,23 @@ public function __call($name, $arguments)
6569
$this->schema['type'] = 'static-' . lcfirst(substr($name, 4));
6670
$this->schema($arguments[0] ?? []);
6771
} else {
68-
$value = $arguments[0] ?? null;
69-
if ($value === null) {
70-
$value = $this->defaultValue[$name] ?? null;
71-
}
72-
$this->schema[$name] = $value;
72+
$this->callToSetSchema($name, $arguments);
7373
}
7474
return $this;
7575
}
76+
77+
public function toArray(): array
78+
{
79+
$this->solveType();
80+
81+
return parent::toArray();
82+
}
83+
84+
protected function solveType()
85+
{
86+
$type = $this->schema['type'];
87+
if ($type === 'static-mapping') {
88+
$this->solveMappingMap();
89+
}
90+
}
7691
}

src/Amis/FormField.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace WebmanTech\AmisAdmin\Amis;
44

5+
use WebmanTech\AmisAdmin\Amis\Traits\ComponentCommonFn;
6+
57
/**
68
* 表单的一个字段
79
*
@@ -21,6 +23,7 @@
2123
* @method $this visibleOn(string $expression)
2224
* @method $this required(bool $is = true)
2325
* @method $this requiredOn(string $expression)
26+
* @method $this static (bool $is = true)
2427
* @method $this validations(array $rules)
2528
* @method $this validationErrors(array $messages)
2629
* @method $this validateOnChange(bool $is = true)
@@ -85,6 +88,8 @@
8588
*/
8689
class FormField extends Component
8790
{
91+
use ComponentCommonFn;
92+
8893
protected array $schema = [
8994
'type' => 'input-text',
9095
'name' => '',
@@ -96,6 +101,7 @@ class FormField extends Component
96101
'hidden' => true,
97102
'visible' => true,
98103
'required' => true,
104+
'static' => true,
99105
'validateOnChange' => true,
100106
];
101107

@@ -105,11 +111,7 @@ public function __call($name, $arguments)
105111
$this->schema['type'] = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '-$1', lcfirst(substr($name, 4))));
106112
$this->schema($arguments[0] ?? []);
107113
} else {
108-
$value = $arguments[0] ?? null;
109-
if ($value === null) {
110-
$value = $this->defaultValue[$name] ?? null;
111-
}
112-
$this->schema[$name] = $value;
114+
$this->callToSetSchema($name, $arguments);
113115
}
114116
return $this;
115117
}

0 commit comments

Comments
 (0)