Setting.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. *
  31. * @return \Dcat\Admin\Http\JsonResponse
  32. */
  33. public function handle(array $input)
  34. {
  35. $this->extension()->config($this->formatInput($input));
  36. return $this->response()->success(trans('admin.save_succeeded'))->refresh();
  37. }
  38. /**
  39. * 格式化配置信息.
  40. *
  41. * @param array $input
  42. *
  43. * @return array
  44. */
  45. protected function formatInput(array $input)
  46. {
  47. return $input;
  48. }
  49. /**
  50. * 表单字段定义.
  51. *
  52. * @return void
  53. */
  54. abstract public function form();
  55. /**
  56. * 弹窗标题.
  57. *
  58. * @return string
  59. */
  60. public function title()
  61. {
  62. }
  63. /**
  64. * 翻译.
  65. *
  66. * @param string $key
  67. * @param array $replace
  68. * @param null $locale
  69. *
  70. * @return array|string|null
  71. */
  72. protected function trans($key, $replace = [], $locale = null)
  73. {
  74. return $this->extension()->trans($key, $replace, $locale);
  75. }
  76. /**
  77. * 填充表单数据.
  78. *
  79. * @return array
  80. */
  81. public function default()
  82. {
  83. return $this->extension()->config() ?: [];
  84. }
  85. /**
  86. * @return ServiceProvider
  87. */
  88. public function extension()
  89. {
  90. if (! empty($this->payload['_extension_'])) {
  91. return Admin::extension()->get($this->payload['_extension_']);
  92. }
  93. return $this->extension;
  94. }
  95. }