jqh преди 4 години
родител
ревизия
4805816138

+ 9 - 9
src/Contracts/Repository.php

@@ -59,18 +59,18 @@ interface Repository
      *
      * @param Form $form
      *
-     * @return array
+     * @return array|\Illuminate\Contracts\Support\Arrayable
      */
-    public function edit(Form $form): array;
+    public function edit(Form $form);
 
     /**
      * 获取详情页面数据.
      *
      * @param Show $show
      *
-     * @return array
+     * @return array|\Illuminate\Contracts\Support\Arrayable
      */
-    public function detail(Show $show): array;
+    public function detail(Show $show);
 
     /**
      * 新增记录.
@@ -86,9 +86,9 @@ interface Repository
      *
      * @param Form $form
      *
-     * @return array
+     * @return array|\Illuminate\Contracts\Support\Arrayable
      */
-    public function getDataWhenUpdating(Form $form): array;
+    public function updating(Form $form);
 
     /**
      * 更新数据.
@@ -107,14 +107,14 @@ interface Repository
      *
      * @return mixed
      */
-    public function destroy(Form $form, array $deletingData);
+    public function delete(Form $form, array $deletingData);
 
     /**
      * 查询删除前的行数据.
      *
      * @param Form $form
      *
-     * @return array
+     * @return array|\Illuminate\Contracts\Support\Arrayable
      */
-    public function getDataWhenDeleting(Form $form): array;
+    public function deleting(Form $form);
 }

+ 2 - 2
src/Exception/Handler.php

@@ -41,7 +41,7 @@ class Handler
 
     public function report(\Throwable $e)
     {
-        $this->getLogger()->error($this->convertExceptionToString($e));
+        $this->logger()->error($this->convertExceptionToString($e));
     }
 
     protected function convertExceptionToString(\Throwable $e)
@@ -65,7 +65,7 @@ class Handler
         );
     }
 
-    protected function getLogger()
+    protected function logger()
     {
         return logger();
     }

+ 70 - 49
src/Form.php

@@ -394,16 +394,20 @@ class Form implements Renderable
     }
 
     /**
-     * @param Fluent $model
+     * @param Fluent|array|\Illuminate\Database\Eloquent\Model $model
      *
-     * @return Fluent|void
+     * @return Fluent|\Illuminate\Database\Eloquent\Model|void
      */
-    public function model(Fluent $model = null)
+    public function model($model = null)
     {
         if ($model === null) {
             return $this->model;
         }
 
+        if (is_array($model)) {
+            $model = new Fluent($model);
+        }
+
         $this->model = $model;
     }
 
@@ -487,7 +491,7 @@ class Form implements Renderable
         $this->builder->mode(Builder::MODE_EDIT);
         $this->builder->setResourceId($id);
 
-        $this->model(new Fluent($this->repository->edit($this)));
+        $this->model($this->repository->edit($this));
 
         return $this;
     }
@@ -526,7 +530,7 @@ class Form implements Renderable
             $this->builder->setResourceId($id);
             $this->builder->mode(Builder::MODE_DELETE);
 
-            $data = $this->repository->getDataWhenDeleting($this);
+            $data = $this->repository->deleting($this);
 
             $this->model(new Fluent($data));
 
@@ -538,7 +542,7 @@ class Form implements Renderable
                 return $response;
             }
 
-            $result = $this->repository->destroy($this, $data);
+            $result = $this->repository->delete($this, $data);
 
             if (($response = $this->callDeleted($result)) instanceof Response) {
                 return $response;
@@ -574,38 +578,48 @@ class Form implements Renderable
      */
     public function store(?array $data = null, $redirectTo = null)
     {
-        if ($data) {
-            $this->request->replace($data);
-        }
+        try {
+            if ($data) {
+                $this->request->replace($data);
+            }
 
-        $data = $data ?: $this->request->all();
+            $data = $data ?: $this->request->all();
 
-        if ($response = $this->beforeStore($data)) {
-            return $response;
-        }
+            if ($response = $this->beforeStore($data)) {
+                return $response;
+            }
 
-        $this->updates = $this->prepareInsert($this->updates);
+            $this->updates = $this->prepareInsert($this->updates);
 
-        $id = $this->repository->store($this);
+            $id = $this->repository->store($this);
 
-        $this->builder->setResourceId($id);
+            $this->builder->setResourceId($id);
 
-        if (($response = $this->callSaved($id))) {
-            return $response;
-        }
+            if (($response = $this->callSaved($id))) {
+                return $response;
+            }
 
-        if ($response = $this->responseMultipleStepsDonePage()) {
-            return $response;
-        }
+            if ($response = $this->responseMultipleStepsDonePage()) {
+                return $response;
+            }
 
-        if (! $id) {
-            return $this->error(trans('admin.save_failed'));
-        }
+            if (! $id) {
+                return $this->error(trans('admin.save_failed'));
+            }
+
+            return $this->redirect(
+                $this->redirectUrl($id, $redirectTo),
+                trans('admin.save_succeeded')
+            );
+        } catch (\Throwable $e) {
+            $response = Admin::makeExceptionHandler()->handle($e);
 
-        return $this->redirect(
-            $this->redirectUrl($id, $redirectTo),
-            trans('admin.save_succeeded')
-        );
+            if ($response instanceof Response) {
+                return $response;
+            }
+
+            return $this->error($e->getMessage() ?: trans('admin.save_failed'));
+        }
     }
 
     /**
@@ -745,32 +759,39 @@ class Form implements Renderable
         ?array $data = null,
         $redirectTo = null
     ) {
-        if ($data) {
-            $this->request->replace($data);
-        }
+        try {
+            if ($data) {
+                $this->request->replace($data);
+            }
 
-        $data = $data ?: $this->request->all();
+            $data = $data ?: $this->request->all();
 
-        if ($response = $this->beforeUpdate($id, $data)) {
-            return $response;
-        }
+            if ($response = $this->beforeUpdate($id, $data)) {
+                return $response;
+            }
 
-        $this->updates = $this->prepareUpdate($this->updates);
+            $this->updates = $this->prepareUpdate($this->updates);
 
-        $updated = $this->repository->update($this);
+            $updated = $this->repository->update($this);
 
-        if (($response = $this->callSaved($updated))) {
-            return $response;
-        }
+            if (($response = $this->callSaved($updated))) {
+                return $response;
+            }
 
-        if (! $updated) {
-            return $this->error(trans('admin.update_succeeded'));
-        }
+            if (! $updated) {
+                return $this->error(trans('admin.update_succeeded'));
+            }
+
+            return $this->redirect($this->redirectUrl($id, $redirectTo), trans('admin.update_succeeded'));
+        } catch (\Throwable $e) {
+            $response = Admin::makeExceptionHandler()->handle($e);
+
+            if ($response instanceof Response) {
+                return $response;
+            }
 
-        return $this->redirect(
-            $this->redirectUrl($id, $redirectTo),
-            trans('admin.update_succeeded')
-        );
+            return $this->error($e->getMessage() ?: trans('admin.save_failed'));
+        }
     }
 
     /**
@@ -787,7 +808,7 @@ class Form implements Renderable
 
         $this->inputs = $data;
 
-        $this->model(new Fluent($this->repository->getDataWhenUpdating($this)));
+        $this->model($this->repository->updating($this));
 
         $this->build();
 

+ 2 - 9
src/Layout/Content.php

@@ -4,6 +4,7 @@ namespace Dcat\Admin\Layout;
 
 use Closure;
 use Dcat\Admin\Admin;
+use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Traits\HasBuilderEvents;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Str;
@@ -151,14 +152,6 @@ class Content implements Renderable
         return $this;
     }
 
-    /**
-     * @deprecated
-     */
-    public function perfectScrollbar()
-    {
-        return $this;
-    }
-
     /**
      * @param array $breadcrumb
      *
@@ -274,7 +267,7 @@ class Content implements Renderable
             $html .= $row->render();
         }
 
-        return $html;
+        return Helper::html($html);
     }
 
     /**

+ 3 - 3
src/Models/Repositories/Extension.php

@@ -81,7 +81,7 @@ class Extension extends Repository
      *
      * @return array
      */
-    public function getDataWhenUpdating(Form $form): array
+    public function updating(Form $form): array
     {
         return [];
     }
@@ -91,7 +91,7 @@ class Extension extends Repository
         return [];
     }
 
-    public function destroy(Form $form, array $deletingData)
+    public function delete(Form $form, array $deletingData)
     {
     }
 
@@ -99,7 +99,7 @@ class Extension extends Repository
     {
     }
 
-    public function getDataWhenDeleting(Form $form): array
+    public function deleting(Form $form): array
     {
         return [];
     }

+ 23 - 13
src/Repositories/EloquentRepository.php

@@ -70,7 +70,7 @@ class EloquentRepository extends Repository implements TreeRepository
             $this->eloquentClass = get_class($this->model);
             $this->queryBuilder = $modelOrRelations;
         } else {
-            $this->with($modelOrRelations);
+            $this->setRelations($modelOrRelations);
         }
 
         $this->setKeyName($this->model()->getKeyName());
@@ -133,7 +133,7 @@ class EloquentRepository extends Repository implements TreeRepository
      *
      * @return $this
      */
-    public function with($relations)
+    public function setRelations($relations)
     {
         $this->relations = (array) $relations;
 
@@ -273,9 +273,9 @@ class EloquentRepository extends Repository implements TreeRepository
      *
      * @param Form $form
      *
-     * @return array
+     * @return array|\Illuminate\Contracts\Support\Arrayable
      */
-    public function edit(Form $form): array
+    public function edit(Form $form)
     {
         $query = $this->newQuery();
 
@@ -287,7 +287,7 @@ class EloquentRepository extends Repository implements TreeRepository
             ->with($this->getRelations())
             ->findOrFail($form->getKey(), $this->getFormColumns());
 
-        return $this->model->toArray();
+        return $this->model;
     }
 
     /**
@@ -295,9 +295,9 @@ class EloquentRepository extends Repository implements TreeRepository
      *
      * @param Show $show
      *
-     * @return array
+     * @return array|\Illuminate\Contracts\Support\Arrayable
      */
-    public function detail(Show $show): array
+    public function detail(Show $show)
     {
         $query = $this->newQuery();
 
@@ -309,7 +309,7 @@ class EloquentRepository extends Repository implements TreeRepository
             ->with($this->getRelations())
             ->findOrFail($show->getKey(), $this->getDetailColumns());
 
-        return $this->model->toArray();
+        return $this->model;
     }
 
     /**
@@ -351,9 +351,9 @@ class EloquentRepository extends Repository implements TreeRepository
      *
      * @param Form $form
      *
-     * @return array
+     * @return array|\Illuminate\Contracts\Support\Arrayable
      */
-    public function getDataWhenUpdating(Form $form): array
+    public function updating(Form $form)
     {
         return $this->edit($form);
     }
@@ -452,7 +452,7 @@ class EloquentRepository extends Repository implements TreeRepository
      *
      * @return bool
      */
-    public function destroy(Form $form, array $originalData)
+    public function delete(Form $form, array $originalData)
     {
         $models = $this->collection->keyBy($this->getKeyName());
 
@@ -487,7 +487,7 @@ class EloquentRepository extends Repository implements TreeRepository
      *
      * @return array
      */
-    public function getDataWhenDeleting(Form $form): array
+    public function deleting(Form $form)
     {
         $query = $this->newQuery();
 
@@ -628,12 +628,22 @@ class EloquentRepository extends Repository implements TreeRepository
         return $model;
     }
 
+    /**
+     * @param array $relations
+     *
+     * @return $this
+     */
+    public static function with($relations = [])
+    {
+        return (new static())->setRelations($relations);
+    }
+
     /**
      * 获取模型的所有关联关系.
      *
      * @return array
      */
-    protected function getRelations()
+    public function getRelations()
     {
         return $this->relations;
     }

+ 3 - 3
src/Repositories/QueryBuilderRepository.php

@@ -300,7 +300,7 @@ class QueryBuilderRepository extends Repository implements TreeRepository
      *
      * @return array
      */
-    public function getDataWhenUpdating(Form $form): array
+    public function updating(Form $form): array
     {
         return $this->edit($form);
     }
@@ -353,7 +353,7 @@ class QueryBuilderRepository extends Repository implements TreeRepository
      *
      * @return bool
      */
-    public function destroy(Form $form, array $deletingData)
+    public function delete(Form $form, array $deletingData)
     {
         $id = $form->getKey();
 
@@ -384,7 +384,7 @@ class QueryBuilderRepository extends Repository implements TreeRepository
      *
      * @return array
      */
-    public function getDataWhenDeleting(Form $form): array
+    public function deleting(Form $form): array
     {
         $query = $this->newQuery();
 

+ 9 - 80
src/Repositories/Repository.php

@@ -100,9 +100,9 @@ abstract class Repository implements RepositoryInterface, TreeRepository
      *
      * @param Form $form
      *
-     * @return array
+     * @return array|\Illuminate\Contracts\Support\Arrayable
      */
-    public function edit(Form $form): array
+    public function edit(Form $form)
     {
         throw new RuntimeException('This repository does not support "edit" method.');
     }
@@ -112,9 +112,9 @@ abstract class Repository implements RepositoryInterface, TreeRepository
      *
      * @param Show $show
      *
-     * @return array
+     * @return array|\Illuminate\Contracts\Support\Arrayable
      */
-    public function detail(Show $show): array
+    public function detail(Show $show)
     {
         throw new RuntimeException('This repository does not support "detail" method.');
     }
@@ -138,9 +138,9 @@ abstract class Repository implements RepositoryInterface, TreeRepository
      *
      * @return array
      */
-    public function getDataWhenUpdating(Form $form): array
+    public function updating(Form $form)
     {
-        throw new RuntimeException('This repository does not support "getDataWhenUpdating" method.');
+        throw new RuntimeException('This repository does not support "updating" method.');
     }
 
     /**
@@ -163,7 +163,7 @@ abstract class Repository implements RepositoryInterface, TreeRepository
      *
      * @return mixed
      */
-    public function destroy(Form $form, array $deletingData)
+    public function delete(Form $form, array $deletingData)
     {
         throw new RuntimeException('This repository does not support "destroy" method.');
     }
@@ -175,9 +175,9 @@ abstract class Repository implements RepositoryInterface, TreeRepository
      *
      * @return array
      */
-    public function getDataWhenDeleting(Form $form): array
+    public function deleting(Form $form)
     {
-        throw new RuntimeException('This repository does not support "getDataWhenDeleting" method.');
+        throw new RuntimeException('This repository does not support "deleting" method.');
     }
 
     /**
@@ -262,75 +262,4 @@ abstract class Repository implements RepositoryInterface, TreeRepository
     {
         return new static(...$params);
     }
-
-    /**
-     * @param array|string $repositories
-     * @param array|string $listeners
-     *
-     * @deprecated 即将在2.0中废弃
-     */
-    public static function listen($repositories, $listeners)
-    {
-        $storage = app('admin.context');
-
-        $array = $storage->get('repository.listeners') ?: [];
-
-        foreach ((array) $repositories as $v) {
-            if (! isset($array[$v])) {
-                $array[$v] = [];
-            }
-
-            $array[$v] = array_merge($array[$v], (array) $listeners);
-        }
-
-        $storage['repository.listeners'] = $array;
-    }
-
-    /**
-     * @param null|string $repository
-     *
-     * @return RepositoryListener[]
-     *
-     * @deprecated 即将在2.0中废弃
-     */
-    public static function getListeners(?string $repository)
-    {
-        if (! $repository) {
-            return null;
-        }
-
-        $any = $repository !== '*' ? static::getListeners('*') : [];
-
-        $storage = app('admin.context');
-
-        $listeners = $storage->get('repository.listeners') ?: [];
-        $resolves = $storage->get('repository.listeners.resolves') ?: [];
-
-        if (isset($resolves[$repository])) {
-            return array_merge($resolves[$repository], $any);
-        }
-
-        $resolves[$repository] = [];
-
-        if (! isset($listeners[$repository])) {
-            return $any;
-        }
-
-        foreach ($listeners[$repository] as $class) {
-            if (! class_exists($class)) {
-                continue;
-            }
-            $listener = new $class();
-
-            if (! $listener instanceof RepositoryListener) {
-                continue;
-            }
-
-            $resolves[$repository][] = $listener;
-        }
-
-        $storage['repository.listeners.resolves'] = $resolves;
-
-        return array_merge($resolves[$repository], $any);
-    }
 }

+ 0 - 72
src/Repositories/RepositoryListener.php

@@ -1,72 +0,0 @@
-<?php
-
-namespace Dcat\Admin\Repositories;
-
-use Dcat\Admin\Form;
-
-/**
- * @deprecated 即将在2.0中废弃
- */
-abstract class RepositoryListener
-{
-    /**
-     * Creating event.
-     *
-     * @param Form $form
-     */
-    public function creating(Form $form)
-    {
-    }
-
-    /**
-     * Created event.
-     *
-     * @param Form  $form
-     * @param mixed $newId
-     */
-    public function created(Form $form, $newId)
-    {
-    }
-
-    /**
-     * Updating event.
-     *
-     * @param Form  $form
-     * @param array $originalAttributes
-     */
-    public function updating(Form $form, array $originalAttributes)
-    {
-    }
-
-    /**
-     * Updated event.
-     *
-     * @param Form  $form
-     * @param array $originalAttributes
-     * @param bool  $result
-     */
-    public function updated(Form $form, array $originalAttributes, $result)
-    {
-    }
-
-    /**
-     * Deleting event.
-     *
-     * @param Form  $form
-     * @param array $originalAttributes
-     */
-    public function deleting(Form $form, array $originalAttributes)
-    {
-    }
-
-    /**
-     * Deleted event.
-     *
-     * @param Form  $form
-     * @param array $originalAttributes
-     * @param $result
-     */
-    public function deleted(Form $form, array $originalAttributes, $result)
-    {
-    }
-}

+ 20 - 4
src/Support/Helper.php

@@ -812,15 +812,31 @@ class Helper
             return $html;
         }
 
-        $space = '';
         $url = '[\s]*[\"\']([\s]*[\w-_\?\=\&\.\/]+[^\"\']*)[\"\']';
         $rel = '(?:rel[\s]*=[\s]*[\"\'][\s]*stylesheet[\s]*[\"\'])*[\s]*';
         $type = '(?:type[\s]*=[\s]*[\"\'][\s]*text\/javascript[\s]*[\"\'])*[\s]*';
 
-        preg_match_all("/<link[\s]+{$rel}href[\s]*={$url}/u", $html, $css);
-        preg_match_all("/<script[\s]+{$type}src[\s]*={$url}>/u", $html, $js);
+        $link = "/<link[\s]+{$rel}href[\s]*={$url}[\s]*[\/]*>/u";
+        $script = "/<script[\s]+{$type}src[\s]*={$url}>[\s]*<\/[\s]*script>/u";
 
-        dd($js, $css);
+        $js = $css = [];
+
+        $html = preg_replace_callback($link, function (&$match) use (&$css) {
+            if (! empty($match[1])) {
+                $css[] = $match[1];
+            }
+        }, $html);
+
+        $html = preg_replace_callback($script, function (&$match) use (&$js) {
+            if (! empty($match[1])) {
+                $js[] = $match[1];
+            }
+        }, $html);
+
+        $js && Admin::js($js);
+        $css && Admin::css($css);
+
+        return $html;
     }
 
     /**