diff --git a/src/Console/ActionCommand.php b/src/Console/ActionCommand.php index 9d41fcac8..5f0860057 100644 --- a/src/Console/ActionCommand.php +++ b/src/Console/ActionCommand.php @@ -131,10 +131,10 @@ protected function getDefaultNamespace($rootNamespace) $segments = explode('\\', config('admin.route.namespace')); array_pop($segments); - array_push($segments, 'Actions'); + $segments[] = 'Actions'; if (isset($this->namespaceMap[$this->choice])) { - array_push($segments, $this->namespaceMap[$this->choice]); + $segments[] = $this->namespaceMap[$this->choice]; } return implode('\\', $segments); diff --git a/src/Exception/Handler.php b/src/Exception/Handler.php index d7adf189a..d00c10fe2 100644 --- a/src/Exception/Handler.php +++ b/src/Exception/Handler.php @@ -75,10 +75,6 @@ public function report(\Throwable $e) */ protected function replaceBasePath(string $path) { - return str_replace( - str_replace('\\', '/', base_path().'/'), - '', - str_replace('\\', '/', $path) - ); + return str_replace(['\\', str_replace('\\', '/', base_path() . '/')], ['/', ''], $path); } } diff --git a/src/Form/Concerns/HasFields.php b/src/Form/Concerns/HasFields.php index 76da2ab77..6851993be 100644 --- a/src/Form/Concerns/HasFields.php +++ b/src/Form/Concerns/HasFields.php @@ -123,7 +123,8 @@ protected function mergedFields() foreach ($this->fields() as $field) { if ($field instanceof FieldsCollection) { /** @var Field $field */ - $fields = array_merge($fields, $field->mergedFields()); + // 使用array_push和展开运算符,避免重复的array_merge调用 + array_push($fields, ...$field->mergedFields()); } else { $fields[] = $field; } diff --git a/src/Form/Field/ArrayField.php b/src/Form/Field/ArrayField.php index df446c45f..08c047b51 100644 --- a/src/Form/Field/ArrayField.php +++ b/src/Form/Field/ArrayField.php @@ -31,7 +31,8 @@ protected function buildRelatedForms() foreach (Helper::array($this->value()) as $key => $data) { if (isset($data['pivot'])) { - $data = array_merge($data, $data['pivot']); + // 使用展开运算符替代 array_merge,性能更优 + $data = [...$data, ...$data['pivot']]; } $forms[$key] = $this->buildNestedForm($key)->fill($data); diff --git a/src/Form/Field/Autocomplete.php b/src/Form/Field/Autocomplete.php index 576f63dbd..cd553ea5f 100644 --- a/src/Form/Field/Autocomplete.php +++ b/src/Form/Field/Autocomplete.php @@ -133,12 +133,20 @@ public function render() protected function formatGroupOptions() { + $groupOptions = []; + foreach ($this->groups as $group) { if (! array_key_exists('options', $group) || ! array_key_exists('label', $group)) { continue; } - $this->options = array_merge($this->options, $this->formatOptions($group['options'], $group['label'])); + // 先收集所有格式化的选项,避免在循环中重复合并数组 + $groupOptions[] = $this->formatOptions($group['options'], $group['label']); + } + + // 使用展开运算符一次性合并所有数组,性能更优 + if ($groupOptions) { + $this->options = array_merge($this->options, ...$groupOptions); } $this->groups = []; diff --git a/src/Form/Field/Embeds.php b/src/Form/Field/Embeds.php index 82c120308..f948f9c31 100755 --- a/src/Form/Field/Embeds.php +++ b/src/Form/Field/Embeds.php @@ -125,15 +125,9 @@ public function getValidator(array $input) * 'extra.end_atend' => "$label[end_at]" * ] */ - $attributes = array_merge( - $attributes, - $this->formatValidationAttribute($input, $field->label(), $column) - ); - - $messages = array_merge( - $messages, - $this->formatValidationMessages($input, $field->getValidationMessages()) - ); + $attributes += $this->formatValidationAttribute($input, $field->label(), $column); + + $messages += $this->formatValidationMessages($input, $field->getValidationMessages()); } if (empty($rules)) { diff --git a/src/Form/Field/Fieldset.php b/src/Form/Field/Fieldset.php index 7a94fa8ab..320188381 100644 --- a/src/Form/Field/Fieldset.php +++ b/src/Form/Field/Fieldset.php @@ -10,7 +10,7 @@ class Fieldset public function __construct() { - $this->name = uniqid('fieldset-'); + $this->name = uniqid('fieldset-', true); } public function start($title) diff --git a/src/Form/Field/HasMany.php b/src/Form/Field/HasMany.php index d9be9ef8e..8dd126252 100755 --- a/src/Form/Field/HasMany.php +++ b/src/Form/Field/HasMany.php @@ -154,15 +154,9 @@ public function getValidator(array $input) $rules[$column] = $fieldRules; } - $attributes = array_merge( - $attributes, - $this->formatValidationAttribute($input, $field->label(), $column) - ); - - $messages = array_merge( - $messages, - $this->formatValidationMessages($input, $field->getValidationMessages()) - ); + $attributes += $this->formatValidationAttribute($input, $field->label(), $column); + + $messages += $this->formatValidationMessages($input, $field->getValidationMessages()); } Arr::forget($rules, NestedForm::REMOVE_FLAG_NAME); diff --git a/src/Form/Field/Text.php b/src/Form/Field/Text.php index 445ec0fe4..75033a63b 100755 --- a/src/Form/Field/Text.php +++ b/src/Form/Field/Text.php @@ -161,16 +161,18 @@ protected function formatOptions($options) $original = []; $toReplace = []; - foreach ($options as $key => &$value) { + foreach ($options as $key => $value) { if (is_array($value)) { $subArray = $this->formatOptions($value); - $value = $subArray['options']; - $original = array_merge($original, $subArray['original']); - $toReplace = array_merge($toReplace, $subArray['toReplace']); + $options[$key] = $subArray['options']; + + // 使用array_push和展开运算符,避免重复的array_merge + array_push($original, ...$subArray['original']); + array_push($toReplace, ...$subArray['toReplace']); } elseif (preg_match('/function.*?/', $value)) { $original[] = $value; - $value = "%{$key}%"; - $toReplace[] = "\"{$value}\""; + $options[$key] = "%{$key}%"; + $toReplace[] = "\"%{$key}%\""; } } diff --git a/src/Form/Field/UploadField.php b/src/Form/Field/UploadField.php index b6cfd36be..a1d0190cf 100755 --- a/src/Form/Field/UploadField.php +++ b/src/Form/Field/UploadField.php @@ -312,7 +312,7 @@ public function sequenceName() */ protected function generateUniqueName(UploadedFile $file) { - return md5(uniqid()).'.'.$file->getClientOriginalExtension(); + return md5(uniqid('', true)).'.'.$file->getClientOriginalExtension(); } /** diff --git a/src/Grid/Column/Filter/Between.php b/src/Grid/Column/Filter/Between.php index 010d396a2..c2df195bd 100644 --- a/src/Grid/Column/Filter/Between.php +++ b/src/Grid/Column/Filter/Between.php @@ -18,8 +18,8 @@ class Between extends Filter public function __construct() { $this->class = [ - 'start' => uniqid('column-filter-start-'), - 'end' => uniqid('column-filter-end-'), + 'start' => uniqid('column-filter-start-', true), + 'end' => uniqid('column-filter-end-', true), ]; } @@ -175,20 +175,20 @@ public function render()