22
33namespace WebmanTech \AmisAdmin \Amis ;
44
5+ use Illuminate \Support \Str ;
56use WebmanTech \AmisAdmin \Helper \ArrayHelper ;
67use WebmanTech \AmisAdmin \Helper \ConfigHelper ;
78use 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}
0 commit comments