Browse Source

优化结构

jqh 5 years ago
parent
commit
c2bcbedfb1

+ 0 - 1
resources/lang/en/admin.php

@@ -137,7 +137,6 @@ return [
         'path'   => 'HTTP path',
     ],
     'all_methods_if_empty'  => 'All methods if empty',
-    'any_character_is'      => 'Using "*" to match any character',
     'all'                   => 'All',
     'current_page'          => 'Current page',
     'selected_rows'         => 'Selected rows',

+ 0 - 1
resources/lang/zh-CN/admin.php

@@ -137,7 +137,6 @@ return [
         'path'   => 'HTTP路径',
     ],
     'all_methods_if_empty'  => '为空默认为所有方法',
-    'any_character_is'      => '使用占位符“*”可匹配任意字符,如:“/auth/*”、“/auth/roles/*/edit”',
     'all'                   => '全部',
     'current_page'          => '当前页',
     'selected_rows'         => '选择的行',

+ 4 - 4
src/Admin.php

@@ -189,7 +189,7 @@ class Admin
                     $show = new Show($id);
                 } else {
                     $show = new Show();
-                    $show->setId($id);
+                    $show->setKey($id);
                 }
 
                 break;
@@ -199,17 +199,17 @@ class Admin
                 } elseif ($repository instanceof \Closure) {
                     $show = new Show(null, $repository);
 
-                    $show->setId($id);
+                    $show->setKey($id);
                 } elseif ($repository instanceof Repository) {
                     $show = new Show($repository);
 
-                    $show->setId($id);
+                    $show->setKey($id);
                 }
                 break;
             case 3:
                 $show = new Show($repository, $callable);
 
-                $show->setId($id);
+                $show->setKey($id);
         }
 
         return $show;

+ 0 - 117
src/Console/stubs/controller.stub

@@ -1,117 +0,0 @@
-<?php
-
-namespace DummyNamespace;
-
-use DummyModelNamespace;
-use App\Http\Controllers\Controller;
-use Dcat\Admin\Controllers\HasResourceActions;
-use Dcat\Admin\Form;
-use Dcat\Admin\Grid;
-use Dcat\Admin\Layout\Content;
-use Dcat\Admin\Show;
-
-class DummyClass extends Controller
-{
-    use HasResourceActions;
-
-    /**
-     * Index interface.
-     *
-     * @param Content $content
-     * @return Content
-     */
-    public function index(Content $content)
-    {
-        return $content
-            ->header('Index')
-            ->description('description')
-            ->body($this->grid());
-    }
-
-    /**
-     * Show interface.
-     *
-     * @param mixed $id
-     * @param Content $content
-     * @return Content
-     */
-    public function show($id, Content $content)
-    {
-        return $content
-            ->header('Detail')
-            ->description('description')
-            ->body($this->detail($id));
-    }
-
-    /**
-     * Edit interface.
-     *
-     * @param mixed $id
-     * @param Content $content
-     * @return Content
-     */
-    public function edit($id, Content $content)
-    {
-        return $content
-            ->header('Edit')
-            ->description('description')
-            ->body($this->form()->edit($id));
-    }
-
-    /**
-     * Create interface.
-     *
-     * @param Content $content
-     * @return Content
-     */
-    public function create(Content $content)
-    {
-        return $content
-            ->header('Create')
-            ->description('description')
-            ->body($this->form());
-    }
-
-    /**
-     * Make a grid builder.
-     *
-     * @return Grid
-     */
-    protected function grid()
-    {
-        $grid = new Grid(new DummyModel);
-
-DummyGrid
-
-        return $grid;
-    }
-
-    /**
-     * Make a show builder.
-     *
-     * @param mixed $id
-     * @return Show
-     */
-    protected function detail($id)
-    {
-        $show = new Show(DummyModel::findOrFail($id));
-
-DummyShow
-
-        return $show;
-    }
-
-    /**
-     * Make a form builder.
-     *
-     * @return Form
-     */
-    protected function form()
-    {
-        $form = new Form(new DummyModel);
-
-DummyForm
-
-        return $form;
-    }
-}

+ 1 - 1
src/Console/stubs/extension/controller.stub

@@ -10,7 +10,7 @@ class :class_nameController extends Controller
     public function index(Content $content)
     {
         return $content
-            ->header('Title')
+            ->title('Title')
             ->description('Description')
             ->body(view(':base_package::index'));
     }

+ 102 - 0
src/Controllers/AdminController.php

@@ -0,0 +1,102 @@
+<?php
+
+namespace Dcat\Admin\Controllers;
+
+use Dcat\Admin\Layout\Content;
+use Illuminate\Routing\Controller;
+
+class AdminController extends Controller
+{
+    use HasResourceActions;
+
+    /**
+     * Title for current resource.
+     *
+     * @var string
+     */
+    protected $title = 'Title';
+
+    /**
+     * Set description for following 4 action pages.
+     *
+     * @var array
+     */
+    protected $description = [
+//        'index'  => 'Index',
+//        'show'   => 'Show',
+//        'edit'   => 'Edit',
+//        'create' => 'Create',
+    ];
+
+    /**
+     * Get content title.
+     *
+     * @return string
+     */
+    protected function title()
+    {
+        return $this->title;
+    }
+
+    /**
+     * Index interface.
+     *
+     * @param Content $content
+     *
+     * @return Content
+     */
+    public function index(Content $content)
+    {
+        return $content
+            ->title($this->title())
+            ->description($this->description['index'] ?? trans('admin.list'))
+            ->body($this->grid());
+    }
+
+    /**
+     * Show interface.
+     *
+     * @param mixed   $id
+     * @param Content $content
+     *
+     * @return Content
+     */
+    public function show($id, Content $content)
+    {
+        return $content
+            ->title($this->title())
+            ->description($this->description['show'] ?? trans('admin.show'))
+            ->body($this->detail($id));
+    }
+
+    /**
+     * Edit interface.
+     *
+     * @param mixed   $id
+     * @param Content $content
+     *
+     * @return Content
+     */
+    public function edit($id, Content $content)
+    {
+        return $content
+            ->title($this->title())
+            ->description($this->description['edit'] ?? trans('admin.edit'))
+            ->body($this->form()->edit($id));
+    }
+
+    /**
+     * Create interface.
+     *
+     * @param Content $content
+     *
+     * @return Content
+     */
+    public function create(Content $content)
+    {
+        return $content
+            ->title($this->title())
+            ->description($this->description['create'] ?? trans('admin.create'))
+            ->body($this->form());
+    }
+}

+ 4 - 2
src/Controllers/PermissionController.php

@@ -304,11 +304,13 @@ class PermissionController extends Controller
                 ->help(trans('admin.all_methods_if_empty'));
 
             $form->tags('http_path', trans('admin.http.path'))
-                ->options($this->getRoutes())
-                ->help(trans('admin.any_character_is'));
+                ->options($this->getRoutes());
 
             $form->display('created_at', trans('admin.created_at'));
             $form->display('updated_at', trans('admin.updated_at'));
+
+            $form->disableViewButton();
+            $form->disableViewCheck();
         });
     }
 

+ 1 - 1
src/Controllers/RoleController.php

@@ -145,7 +145,7 @@ class RoleController extends Controller
             $show->created_at;
             $show->updated_at;
 
-            if ($show->getId() == RoleModel::ADMINISTRATOR_ID) {
+            if ($show->getKey() == RoleModel::ADMINISTRATOR_ID) {
                 $show->disableDeleteButton();
             }
 

+ 1 - 1
src/Controllers/UserController.php

@@ -211,7 +211,7 @@ class UserController extends Controller
                 return $tree->render();
             });
 
-            if ($show->getId() == AdministratorModel::DEFAULT_ID) {
+            if ($show->getKey() == AdministratorModel::DEFAULT_ID) {
                 $show->disableDeleteButton();
             }
 

+ 16 - 6
src/Layout/Content.php

@@ -18,11 +18,11 @@ class Content implements Renderable
     protected $view = 'admin::content';
 
     /**
-     * Content header.
+     * Content title.
      *
      * @var string
      */
-    protected $header = '';
+    protected $title = '';
 
     /**
      * Content description.
@@ -69,15 +69,25 @@ class Content implements Renderable
     }
 
     /**
-     * Set header of content.
-     *
      * @param string $header
      *
      * @return $this
      */
     public function header($header = '')
     {
-        $this->header = $header;
+        return $this->title($header);
+    }
+
+    /**
+     * Set title of content.
+     *
+     * @param string $title
+     *
+     * @return $this
+     */
+    public function title($title)
+    {
+        $this->title = $title;
 
         return $this;
     }
@@ -352,7 +362,7 @@ CSS
         $this->setupStyles();
 
         $items = [
-            'header'      => $this->header,
+            'header'      => $this->title,
             'description' => $this->description,
             'breadcrumb'  => $this->breadcrumb,
             'content'     => $this->build(),

+ 1 - 1
src/Repositories/EloquentRepository.php

@@ -163,7 +163,7 @@ abstract class EloquentRepository extends Repository
 
         $this->model = $eloquent
             ->with($this->getRelations($show))
-            ->findOrFail($show->getId(), $this->getDetailColumns());
+            ->findOrFail($show->getKey(), $this->getDetailColumns());
 
         return $this->model->toArray();
     }

+ 1 - 1
src/Repositories/Proxy.php

@@ -68,7 +68,7 @@ class Proxy implements \Dcat\Admin\Contracts\Repository
 
     public function detail(Show $show): array
     {
-        $id = $show->getId();
+        $id = $show->getKey();
 
         if (array_key_exists($id, $this->__caches['detail'])) {
             return $this->__caches['detail'][$id];

+ 2 - 0
src/Scaffold/ControllerCreator.php

@@ -81,6 +81,7 @@ class ControllerCreator
             [
                 'DummyModelNamespace',
                 'DummyModel',
+                'DummyTitle',
                 '{controller}',
                 '{grid}',
                 '{form}',
@@ -89,6 +90,7 @@ class ControllerCreator
             [
                 $model,
                 class_basename($model),
+                class_basename($model),
                 $slug,
                 $this->generateGrid(),
                 $this->generateForm(),

+ 8 - 63
src/Scaffold/stubs/controller.stub

@@ -3,75 +3,20 @@
 namespace DummyNamespace;
 
 use DummyModelNamespace;
-use App\Http\Controllers\Controller;
-use Dcat\Admin\Controllers\HasResourceActions;
+use Dcat\Admin\Controllers\AdminController;
 use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
-use Dcat\Admin\Layout\Content;
 use Dcat\Admin\Show;
 use Dcat\Admin\Admin;
 
-class DummyClass extends Controller
+class DummyClass extends AdminController
 {
-    use HasResourceActions;
-
-    /**
-     * Index interface.
-     *
-     * @param Content $content
-     * @return Content
-     */
-    public function index(Content $content)
-    {
-        return $content
-            ->header(admin_trans_label())
-            ->description(admin_trans_label('list'))
-            ->body($this->grid());
-    }
-
     /**
-     * Show interface.
+     * Title for current resource.
      *
-     * @param mixed $id
-     * @param Content $content
-     * @return Content
+     * @var string
      */
-    public function show($id, Content $content)
-    {
-        return $content
-            ->header(admin_trans_label())
-            ->description(admin_trans_label('detail'))
-            ->body($this->detail($id));
-    }
-
-    /**
-     * Edit interface.
-     *
-     * @param mixed $id
-     * @param Content $content
-     * @return Content
-     */
-    public function edit($id, Content $content)
-    {
-        return $content
-            ->header(admin_trans_label())
-            ->description(admin_trans_label('edit'))
-            ->body($this->form()->edit($id));
-    }
-
-    /**
-     * Create interface.
-     *
-     * @param Content $content
-     * @return Content
-     */
-    public function create(Content $content)
-    {
-        return $content
-            ->header(admin_trans_label())
-            ->description(admin_trans_label('create'))
-            ->body($this->form());
-    }
+    protected $title = 'DummyTitle';
 
     /**
      * Make a grid builder.
@@ -80,7 +25,7 @@ class DummyClass extends Controller
      */
     protected function grid()
     {
-        return Admin::grid(new DummyModel, function (Grid $grid) {
+        return Admin::grid(new DummyModel(), function (Grid $grid) {
             {grid}
         });
     }
@@ -93,7 +38,7 @@ class DummyClass extends Controller
      */
     protected function detail($id)
     {
-        return Admin::show($id, new DummyModel, function (Show $show) {
+        return Admin::show($id, new DummyModel(), function (Show $show) {
             {show}
         });
     }
@@ -105,7 +50,7 @@ class DummyClass extends Controller
      */
     protected function form()
     {
-        return Admin::form(new DummyModel, function (Form $form) {
+        return Admin::form(new DummyModel(), function (Form $form) {
             {form}
         });
     }

+ 54 - 13
src/Show.php

@@ -9,11 +9,11 @@ use Dcat\Admin\Show\Newline;
 use Dcat\Admin\Show\Panel;
 use Dcat\Admin\Show\Relation;
 use Dcat\Admin\Traits\HasBuilderEvents;
+use Illuminate\Contracts\Support\Arrayable;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Collection;
-use Illuminate\Support\Facades\URL;
 use Illuminate\Support\Fluent;
 use Illuminate\Support\Traits\Macroable;
 
@@ -39,6 +39,11 @@ class Show implements Renderable
      */
     protected $__id;
 
+    /**
+     * @var string
+     */
+    protected $keyName = 'id';
+
     /**
      * @var Fluent
      */
@@ -80,14 +85,13 @@ class Show implements Renderable
     /**
      * Show constructor.
      *
-     * @param Model $model
+     * @param Model|Repository|array|Arrayable $model
      * @param \Closure $builder
      */
-    public function __construct(?Repository $repository = null, ?\Closure $builder = null)
+    public function __construct($model = null, ?\Closure $builder = null)
     {
-        if ($repository) {
-            $this->repository = Admin::repository($repository);
-        }
+        $this->initModel($model);
+
         $this->builder = $builder;
 
         $this->initPanel();
@@ -96,6 +100,19 @@ class Show implements Renderable
         $this->callResolving();
     }
 
+    protected function initModel($model)
+    {
+        if ($model instanceof Repository) {
+            $this->repository = Admin::repository($model);
+        } elseif ($model instanceof Arrayable) {
+            $this->setModel(new Fluent($model->toArray()));
+        } elseif (is_array($model)) {
+            $this->setModel(new Fluent($model));
+        } else {
+            $this->setModel(new Fluent());
+        }
+    }
+
     /**
      * Create a show instance.
      *
@@ -107,6 +124,17 @@ class Show implements Renderable
         return new static(...$params);
     }
 
+    /**
+     * @param string $value
+     * @return $this
+     */
+    public function setKeyName(string $value)
+    {
+        $this->keyName = $value;
+
+        return $this;
+    }
+
     /**
      * Get primary key name of model.
      *
@@ -114,13 +142,17 @@ class Show implements Renderable
      */
     public function getKeyName()
     {
-        return $this->repository->getKeyName();
+        if (! $this->repository) {
+            return $this->keyName;
+        }
+
+        return $this->keyName ?: $this->repository->getKeyName();
     }
 
     /**
      * @param mixed $id
      */
-    public function setId($id)
+    public function setKey($id)
     {
         $this->__id = $id;
     }
@@ -128,7 +160,7 @@ class Show implements Renderable
     /**
      * @return mixed
      */
-    public function getId()
+    public function getKey()
     {
         return $this->__id;
     }
@@ -146,10 +178,6 @@ class Show implements Renderable
      */
     public function model()
     {
-        if (!$this->model) {
-            $this->setModel(new Fluent($this->repository->detail($this)));
-        }
-
         return $this->model;
     }
 
@@ -553,6 +581,17 @@ class Show implements Renderable
         return false;
     }
 
+    protected function setupModel()
+    {
+        if ($this->repository && ! $this->model) {
+            $this->setModel(new Fluent($this->repository->detail($this)));
+        }
+
+        if (! $this->model) {
+            $this->setModel(new Fluent());
+        }
+    }
+
     /**
      * Render the show panels.
      *
@@ -561,6 +600,8 @@ class Show implements Renderable
     public function render()
     {
         try {
+            $this->setupModel();
+
             $model = $this->model();
 
             if (is_callable($this->builder)) {

+ 2 - 2
src/Show/Tools.php

@@ -206,7 +206,7 @@ class Tools implements Renderable
      */
     protected function getEditPath()
     {
-        $key = $this->panel->getParent()->getId();
+        $key = $this->panel->getParent()->getKey();
 
         return $this->getListPath().'/'.$key.'/edit';
     }
@@ -218,7 +218,7 @@ class Tools implements Renderable
      */
     protected function getDeletePath()
     {
-        $key = $this->panel->getParent()->getId();
+        $key = $this->panel->getParent()->getKey();
 
         return $this->getListPath().'/'.$key;
     }