Setting.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Dcat\Admin\Extend;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Contracts\LazyRenderable;
  5. use Dcat\Admin\Traits\LazyWidget;
  6. use Dcat\Admin\Widgets\Form;
  7. abstract class Setting extends Form implements LazyRenderable
  8. {
  9. use LazyWidget;
  10. /**
  11. * @var ServiceProvider
  12. */
  13. protected $extension;
  14. public function __construct(ServiceProvider $extension = null)
  15. {
  16. parent::__construct();
  17. $this->initExtension($extension);
  18. }
  19. protected function initExtension(?ServiceProvider $extension)
  20. {
  21. if ($extension) {
  22. $this->extension = $extension;
  23. $this->payload(['_extension_' => $extension->getName()]);
  24. }
  25. }
  26. /**
  27. * 处理请求.
  28. *
  29. * @param array $input
  30. * @return \Dcat\Admin\Http\JsonResponse
  31. */
  32. public function handle(array $input)
  33. {
  34. $this->extension()->config($this->formatInput($input));
  35. return $this->response()->success(trans('admin.save_succeeded'))->refresh();
  36. }
  37. /**
  38. * 格式化配置信息.
  39. *
  40. * @param array $input
  41. * @return array
  42. */
  43. protected function formatInput(array $input)
  44. {
  45. return $input;
  46. }
  47. /**
  48. * 表单字段定义.
  49. *
  50. * @return void
  51. */
  52. abstract public function form();
  53. /**
  54. * 弹窗标题.
  55. *
  56. * @return string
  57. */
  58. public function title()
  59. {
  60. }
  61. /**
  62. * 翻译.
  63. *
  64. * @param string $key
  65. * @param array $replace
  66. * @param null $locale
  67. * @return array|string|null
  68. */
  69. protected function trans($key, $replace = [], $locale = null)
  70. {
  71. return $this->extension()->trans($key, $replace, $locale);
  72. }
  73. /**
  74. * 填充表单数据.
  75. *
  76. * @return array
  77. */
  78. public function default()
  79. {
  80. return $this->extension()->config() ?: [];
  81. }
  82. /**
  83. * @return ServiceProvider
  84. */
  85. public function extension()
  86. {
  87. if (! empty($this->payload['_extension_'])) {
  88. return Admin::extension()->get($this->payload['_extension_']);
  89. }
  90. return $this->extension;
  91. }
  92. }