Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: phpunit

on:
push:
branches:
- 'main'
pull_request:
# schedule:
# - cron: '0 0 * * *'

jobs:
phpunit:
runs-on: ubuntu-latest
strategy:
matrix:
php: [7.4, 8.0, 8.1, 8.2, 8.3, 8.4]
stability: [
#prefer-lowest, 问题较多
prefer-stable
]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }}

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug
#extensions: 'redis'
#ini-values: error_reporting=E_ALL php8.4 下 nullable is deprecated 暂时未完全解决,因此暂时不启用
tools: composer:v2

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: php${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
restore-keys: php${{ matrix.php }}-${{ matrix.stability }}-composer-

- name: Install dependencies
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress

- name: Run test
run: composer test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ vendor
.vscode
.phpunit*
composer.lock

# webman 1.6 以上版本 BASE_PATH 限定在根目录,为了 tests 排除
/config
/tests/helpers.php
27 changes: 21 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,30 @@
"src/helper.php"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"require": {
"php": ">=7.4",
"php": "^7.4||^8.0",
"ext-json": "*"
},
"require-dev": {
"workerman/webman-framework": "^1.3",
"illuminate/database": "^8.83",
"illuminate/pagination": "^8.83",
"illuminate/validation": "^8.83",
"webman-tech/polyfill": "^1.0"
"workerman/webman-framework": "^1.5|^2.0",
"illuminate/database": "^8.0|^9.0|^10.0|^11.0",
"illuminate/pagination": "^8.0|^9.0|^10.0|^11.0",
"illuminate/validation": "^8.0|^9.0|^10.0|^11.0",
"webman-tech/polyfill": "^1.0|^2.0",
"pestphp/pest": "^1.23|^2.0",
"symfony/var-dumper": "^5.4|^6.0|^7.0"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"scripts": {
"test": "pest"
}
}
40 changes: 37 additions & 3 deletions docs/common_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

<details>
<summary>如何修改 crud 的详情打开的 dialog 的 size</summary>

在对应的 controller 中添加以下配置
<pre>
/**
* @inheritdoc
*/
// 修改该参数,对于 新增、修改、明细 都使用 lg 的 dialog
protected ?array $defaultDialogConfig = [
'size' => 'lg',
];

// 单独控制
protected function gridActionsConfig(): array
{
return [
// 单独配置 detail
'schema_detail' => [
'dialog' => [
'size' => 'lg',
Expand All @@ -18,4 +23,33 @@ protected function gridActionsConfig(): array
];
}
</pre>
</details>

<details>
<summary>如何全局配置一个 amis 的组件</summary>

在 config 的 amis 中 components 中添加以下配置
<pre>
return [
// ... 其他配置
/*
* 用于全局替换组件的默认参数
* @see Component::$config
*/
'components' => [
// 例如: 将列表页的字段默认左显示
/*\WebmanTech\AmisAdmin\Amis\GridColumn::class => [
'schema' => [
'align' => 'left',
],
],*/
// typeXxx,xxx 未 amis 的组件 type,通过 schema 会全局注入到每个 type 组件
'typeImage' => [
'schema' => [
'enlargeAble' => true,
],
],
],
];
</pre>
</details>
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</phpunit>
10 changes: 10 additions & 0 deletions src/Amis.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ private function getAssets(): array
return $item;
}, $assets['js']);

$assets['lang'] = $assets['lang'] ?? 'zh';
if (is_callable($assets['lang'])) {
$assets['lang'] = call_user_func($assets['lang']);
}

$assets['locale'] = $assets['locale'] ?? 'zh-CN';
if (is_callable($assets['locale'])) {
$assets['locale'] = call_user_func($assets['locale']);
}

return $assets;
}
}
33 changes: 32 additions & 1 deletion src/Amis/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WebmanTech\AmisAdmin\Amis;

use Illuminate\Support\Str;
use WebmanTech\AmisAdmin\Helper\ArrayHelper;
use WebmanTech\AmisAdmin\Helper\ConfigHelper;
use support\Container;
Expand Down Expand Up @@ -36,7 +37,7 @@ public function __construct()
public static function make(array $schema = null)
{
/** @var static $component */
$component = Container::get(static::class);
$component = clone Container::get(static::class);
if ($schema) {
$component->schema($schema);
}
Expand Down Expand Up @@ -88,6 +89,9 @@ public function toArray(): array
protected function deepToArray(array $arr): array
{
$newArr = [];
if (isset($arr['type']) && $arr['type']) {
$arr = $this->mergeGlobalConfigWithType($arr['type'], $arr);
}
foreach ($arr as $key => $item) {
if (is_array($item)) {
$item = $this->deepToArray($item);
Expand Down Expand Up @@ -119,4 +123,31 @@ protected function merge(...$arrays): array
{
return ArrayHelper::merge(...$arrays);
}

/**
* 合并全局的 配置 参数
* @param string $type
* @param array $schema
* @return array
*/
protected function mergeGlobalConfigWithType(string $type, array $schema): array
{
$componentTypeName = 'type' . Str::studly($type);
$componentConfig = ConfigHelper::get('components.' . $componentTypeName, [], true);
if ($componentConfig === [] && Str::contains($type, 'static-')) {
// 支持兼容 typeStaticImage 到 typeImage 的配置
$componentTypeName = str_replace('typeStatic', 'type', $componentTypeName);
$componentConfig = ConfigHelper::get('components.' . $componentTypeName, [], true);
}
if ($globalSchema = $componentConfig['schema'] ?? []) {
if (isset($globalSchema['type'])) {
$schema['type'] = $globalSchema['type']; // 允许做全局的 type 修改,这样可以做自定义组件
}
$schema = $this->merge($globalSchema, $schema);
if ($schema['type'] !== $type) {
return $this->mergeGlobalConfigWithType($schema['type'], $schema);
}
}
return $schema;
}
}
2 changes: 1 addition & 1 deletion src/Amis/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function withColumns(array $columns)
*/
public function withCreate(string $api, array $form, string $can = '1==1')
{
$label = $this->config['schema_create']['label'] ?? '新增';
$label = $this->config['schema_create']['label'] ?? trans('新增', [], 'amis-admin');
return $this->withButtonDialog(static::INDEX_CREATE, $label, $form, $this->merge([
'api' => $api,
'level' => 'primary',
Expand Down
25 changes: 20 additions & 5 deletions src/Amis/DetailAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace WebmanTech\AmisAdmin\Amis;

use WebmanTech\AmisAdmin\Amis\Traits\ComponentCommonFn;

/**
* 详情的一个字段
* @link https://aisuda.bce.baidu.com/amis/zh-CN/components/form/static
Expand Down Expand Up @@ -36,6 +38,8 @@
*/
class DetailAttribute extends Component
{
use ComponentCommonFn;

protected array $schema = [
'type' => 'static',
'name' => '',
Expand Down Expand Up @@ -65,12 +69,23 @@ public function __call($name, $arguments)
$this->schema['type'] = 'static-' . lcfirst(substr($name, 4));
$this->schema($arguments[0] ?? []);
} else {
$value = $arguments[0] ?? null;
if ($value === null) {
$value = $this->defaultValue[$name] ?? null;
}
$this->schema[$name] = $value;
$this->callToSetSchema($name, $arguments);
}
return $this;
}

public function toArray(): array
{
$this->solveType();

return parent::toArray();
}

protected function solveType()
{
$type = $this->schema['type'];
if ($type === 'static-mapping') {
$this->solveMappingMap();
}
}
}
12 changes: 7 additions & 5 deletions src/Amis/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace WebmanTech\AmisAdmin\Amis;

use WebmanTech\AmisAdmin\Amis\Traits\ComponentCommonFn;

/**
* 表单的一个字段
*
Expand All @@ -21,6 +23,7 @@
* @method $this visibleOn(string $expression)
* @method $this required(bool $is = true)
* @method $this requiredOn(string $expression)
* @method $this static (bool $is = true)
* @method $this validations(array $rules)
* @method $this validationErrors(array $messages)
* @method $this validateOnChange(bool $is = true)
Expand Down Expand Up @@ -85,6 +88,8 @@
*/
class FormField extends Component
{
use ComponentCommonFn;

protected array $schema = [
'type' => 'input-text',
'name' => '',
Expand All @@ -96,6 +101,7 @@ class FormField extends Component
'hidden' => true,
'visible' => true,
'required' => true,
'static' => true,
'validateOnChange' => true,
];

Expand All @@ -105,11 +111,7 @@ public function __call($name, $arguments)
$this->schema['type'] = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '-$1', lcfirst(substr($name, 4))));
$this->schema($arguments[0] ?? []);
} else {
$value = $arguments[0] ?? null;
if ($value === null) {
$value = $this->defaultValue[$name] ?? null;
}
$this->schema[$name] = $value;
$this->callToSetSchema($name, $arguments);
}
return $this;
}
Expand Down
Loading