jqh il y a 4 ans
Parent
commit
92a1471c7c

+ 3 - 18
src/Admin.php

@@ -6,12 +6,10 @@ use Closure;
 use Dcat\Admin\Contracts\Repository;
 use Dcat\Admin\Controllers\AuthController;
 use Dcat\Admin\Exception\Handler;
-use Dcat\Admin\Layout\Content;
 use Dcat\Admin\Layout\Menu;
 use Dcat\Admin\Layout\Navbar;
 use Dcat\Admin\Layout\SectionManager;
 use Dcat\Admin\Repositories\EloquentRepository;
-use Dcat\Admin\Repositories\Proxy;
 use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Traits\HasAssets;
 use Dcat\Admin\Traits\HasPermissions;
@@ -130,17 +128,6 @@ class Admin
         static::$favicon = $favicon;
     }
 
-    /**
-     * @param Closure $callable
-     *
-     * @return Content
-     * @deprecated
-     */
-    public static function content(Closure $callable = null)
-    {
-        return new Content($callable);
-    }
-
     /**
      * 获取登录用户模型.
      *
@@ -321,14 +308,12 @@ class Admin
             throw new \InvalidArgumentException("The class [{$class}] must be a type of [".Repository::class.'].');
         }
 
-        if ($repository instanceof Proxy) {
-            return $repository;
-        }
-
-        return new Proxy($repository);
+        return $repository;
     }
 
     /**
+     * 应用管理.
+     *
      * @return Application
      */
     public static function app()

+ 66 - 60
src/Form/Concerns/HasEvents.php

@@ -3,30 +3,19 @@
 namespace Dcat\Admin\Form\Concerns;
 
 use Closure;
+use Dcat\Admin\Form\Events;
 use Dcat\Admin\Contracts\UploadField as UploadFieldInterface;
+use Illuminate\Support\Facades\Event;
 use Symfony\Component\HttpFoundation\File\UploadedFile;
 use Symfony\Component\HttpFoundation\RedirectResponse;
 use Symfony\Component\HttpFoundation\Response;
 
 trait HasEvents
 {
-    /**
-     * @var array
-     */
-    protected $__hooks = [
-        'creating'  => [],
-        'editing'   => [],
-        'submitted' => [],
-        'saving'    => [],
-        'saved'     => [],
-        'deleting'  => [],
-        'deleted'   => [],
-        'uploading' => [],
-        'uploaded'  => [],
-    ];
+    public $eventResponse;
 
     /**
-     * Set after getting creating model callback.
+     * 监听创建页面访问事件.
      *
      * @param Closure $callback
      *
@@ -34,13 +23,13 @@ trait HasEvents
      */
     public function creating(Closure $callback)
     {
-        $this->__hooks['creating'][] = $callback;
+        Event::listen(Events\Creating::class, $this->makeListener($callback));
 
         return $this;
     }
 
     /**
-     * Set after getting editing model callback.
+     * 监听编辑页面访问时间.
      *
      * @param Closure $callback
      *
@@ -48,13 +37,13 @@ trait HasEvents
      */
     public function editing(Closure $callback)
     {
-        $this->__hooks['editing'][] = $callback;
+        Event::listen(Events\Editing::class, $this->makeListener($callback));
 
         return $this;
     }
 
     /**
-     * Set submitted callback.
+     * 监听提交事件
      *
      * @param Closure $callback
      *
@@ -62,7 +51,7 @@ trait HasEvents
      */
     public function submitted(Closure $callback)
     {
-        $this->__hooks['submitted'][] = $callback;
+        Event::listen(Events\Submitted::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -76,7 +65,7 @@ trait HasEvents
      */
     public function saving(Closure $callback)
     {
-        $this->__hooks['saving'][] = $callback;
+        Event::listen(Events\Saving::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -90,7 +79,7 @@ trait HasEvents
      */
     public function saved(Closure $callback)
     {
-        $this->__hooks['saved'][] = $callback;
+        Event::listen(Events\Saved::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -102,7 +91,7 @@ trait HasEvents
      */
     public function deleting(Closure $callback)
     {
-        $this->__hooks['deleting'][] = $callback;
+        Event::listen(Events\Deleting::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -114,7 +103,7 @@ trait HasEvents
      */
     public function deleted(Closure $callback)
     {
-        $this->__hooks['deleted'][] = $callback;
+        Event::listen(Events\Deleted::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -126,7 +115,7 @@ trait HasEvents
      */
     public function uploading(Closure $callback)
     {
-        $this->__hooks['uploading'][] = $callback;
+        Event::listen(Events\Uploading::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -138,53 +127,78 @@ trait HasEvents
      */
     public function uploaded(Closure $callback)
     {
-        $this->__hooks['uploaded'][] = $callback;
+        Event::listen(Events\Uploaded::class, $this->makeListener($callback));
 
         return $this;
     }
 
     /**
-     * Call creating callbacks.
+     * @param \Closure $callback
+     *
+     * @return \Closure
+     */
+    protected function makeListener(Closure $callback)
+    {
+        return function (Events\Event $event) use ($callback) {
+            if ($model = $event->form->model()) {
+                $callback = $callback->bindTo($model);
+            }
+
+            $ret = $callback($this, ...$event->payload);
+
+            if (
+                $ret instanceof Response
+                || ($ret instanceof RedirectResponse && ! $this->isAjaxRequest())
+            ) {
+                $event->form->eventResponse = $ret;
+
+                return false;
+            }
+        };
+    }
+
+    /**
+     * 触发创建页访问事件.
      *
      * @return mixed
      */
     protected function callCreating()
     {
-        return $this->callListeners('creating');
+        return $this->fire(Events\Creating::class);
     }
 
     /**
-     * Call editing callbacks.
+     * 触发编辑页访问事件.
      *
      * @return mixed
      */
     protected function callEditing()
     {
-        return $this->callListeners('editing');
+        return $this->fire(Events\Editing::class);
     }
 
     /**
-     * Call submitted callback.
+     * 触发表单提交事件.
      *
      * @return mixed
      */
     protected function callSubmitted()
     {
-        return $this->callListeners('submitted');
+        return $this->fire(Events\Submitted::class);
     }
 
     /**
-     * Call saving callback.
+     * 触发表单保存事件.
      *
      * @return mixed
      */
     protected function callSaving()
     {
-        return $this->callListeners('saving');
+        return $this->fire(Events\Saving::class);
     }
 
     /**
-     * Callback after saving a Model.
+     * 触发表单保存完成事件.
      *
      * @param mixed $result
      *
@@ -192,28 +206,34 @@ trait HasEvents
      */
     protected function callSaved($result)
     {
-        return $this->callListeners('saved', [$result]);
+        return $this->fire(Events\Saved::class, [$result]);
     }
 
     /**
+     * 触发数据删除事件.
+     *
      * @return mixed|null
      */
     protected function callDeleting()
     {
-        return $this->callListeners('deleting');
+        return $this->fire(Events\Deleting::class);
     }
 
     /**
+     * 触发数据删除完成事件.
+     *
      * @param mixed $result
      *
      * @return mixed|null
      */
     protected function callDeleted($result)
     {
-        return $this->callListeners('deleted', [$result]);
+        return $this->fire(Events\Deleted::class, [$result]);
     }
 
     /**
+     * 触发文件上传事件.
+     *
      * @param UploadFieldInterface|\Dcat\Admin\Form\Field $field
      * @param UploadedFile                                $file
      *
@@ -221,10 +241,12 @@ trait HasEvents
      */
     protected function callUploading($field, $file)
     {
-        return $this->callListeners('uploading', [$field, $file]);
+        return $this->fire(Events\Uploading::class, [$field, $file]);
     }
 
     /**
+     * 触发文件上传完成事件
+     *
      * @param UploadFieldInterface|\Dcat\Admin\Form\Field $field
      * @param UploadedFile                                $file
      * @param Response                                    $response
@@ -233,35 +255,19 @@ trait HasEvents
      */
     protected function callUploaded($field, $file, $response)
     {
-        return $this->callListeners('uploaded', [$field, $file, $response]);
+        return $this->fire(Events\Uploaded::class, [$field, $file, $response]);
     }
 
     /**
      * @param string $name
+     * @param array  $payload
      *
      * @return RedirectResponse|\Illuminate\Http\Response|void
      */
-    protected function callListeners($name, array $params = [])
+    protected function fire($name, array $payload = [])
     {
-        $response = null;
-
-        foreach ($this->__hooks[$name] as $func) {
-            $this->model && $func->bindTo($this->model);
-
-            $ret = $func($this, ...$params);
-
-            if (
-                $response
-                || ! $ret
-                || ! $ret instanceof Response
-                || ($ret instanceof RedirectResponse && $this->isAjaxRequest())
-            ) {
-                continue;
-            }
-
-            $response = $ret;
-        }
+        Event::dispatch(new $name($this, $payload));
 
-        return $response;
+        return $this->eventResponse;
     }
 }

+ 7 - 0
src/Form/Events/Creating.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Form\Events;
+
+class Creating extends Event
+{
+}

+ 7 - 0
src/Form/Events/Deleted.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Form\Events;
+
+class Deleted extends Event
+{
+}

+ 7 - 0
src/Form/Events/Deleting.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Form\Events;
+
+class Deleting extends Event
+{
+}

+ 7 - 0
src/Form/Events/Editing.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Form\Events;
+
+class Editing extends Event
+{
+}

+ 21 - 0
src/Form/Events/Event.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace Dcat\Admin\Form\Events;
+
+use Dcat\Admin\Form;
+
+abstract class Event
+{
+    /**
+     * @var Form
+     */
+    public $form;
+
+    public $payload = [];
+
+    public function __construct(Form $form, array $payload = [])
+    {
+        $this->form = $form;
+        $this->payload = $payload;
+    }
+}

+ 7 - 0
src/Form/Events/Saved.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Form\Events;
+
+class Saved extends Event
+{
+}

+ 7 - 0
src/Form/Events/Saving.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Form\Events;
+
+class Saving extends Event
+{
+}

+ 7 - 0
src/Form/Events/Submitted.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Form\Events;
+
+class Submitted extends Event
+{
+}

+ 7 - 0
src/Form/Events/Uploaded.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Form\Events;
+
+class Uploaded extends Event
+{
+}

+ 7 - 0
src/Form/Events/Uploading.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Form\Events;
+
+class Uploading extends Event
+{
+}

+ 0 - 1
src/Grid/Tools/QuickSearch.php

@@ -3,7 +3,6 @@
 namespace Dcat\Admin\Grid\Tools;
 
 use Dcat\Admin\Admin;
-use Dcat\Admin\Grid;
 use Dcat\Admin\Support\Helper;
 
 class QuickSearch extends AbstractTool

+ 19 - 19
src/Repositories/EloquentRepository.php

@@ -73,10 +73,10 @@ class EloquentRepository extends Repository implements TreeRepository
             $this->with($modelOrRelations);
         }
 
-        $this->setKeyName($this->eloquent()->getKeyName());
+        $this->setKeyName($this->model()->getKeyName());
 
         $this->setIsSoftDeletes(
-            in_array(SoftDeletes::class, class_uses($this->eloquent()))
+            in_array(SoftDeletes::class, class_uses($this->model()))
         );
     }
 
@@ -85,7 +85,7 @@ class EloquentRepository extends Repository implements TreeRepository
      */
     public function getCreatedAtColumn()
     {
-        return $this->eloquent()->getCreatedAtColumn();
+        return $this->model()->getCreatedAtColumn();
     }
 
     /**
@@ -93,7 +93,7 @@ class EloquentRepository extends Repository implements TreeRepository
      */
     public function getUpdatedAtColumn()
     {
-        return $this->eloquent()->getUpdatedAtColumn();
+        return $this->model()->getUpdatedAtColumn();
     }
 
     /**
@@ -324,7 +324,7 @@ class EloquentRepository extends Repository implements TreeRepository
         $result = null;
 
         DB::transaction(function () use ($form, &$result) {
-            $model = $this->eloquent();
+            $model = $this->model();
 
             $updates = $form->updates();
 
@@ -343,7 +343,7 @@ class EloquentRepository extends Repository implements TreeRepository
             $this->updateRelation($form, $model, $relations, $relationKeyMap);
         });
 
-        return $this->eloquent()->getKey();
+        return $this->model()->getKey();
     }
 
     /**
@@ -368,7 +368,7 @@ class EloquentRepository extends Repository implements TreeRepository
     public function update(Form $form)
     {
         /* @var EloquentModel $builder */
-        $model = $this->eloquent();
+        $model = $this->model();
 
         if (! $model->getKey()) {
             $model->exists = true;
@@ -407,7 +407,7 @@ class EloquentRepository extends Repository implements TreeRepository
      */
     public function moveOrderUp()
     {
-        $model = $this->eloquent();
+        $model = $this->model();
 
         if (! $model instanceof Sortable) {
             throw new \RuntimeException(
@@ -429,7 +429,7 @@ class EloquentRepository extends Repository implements TreeRepository
      */
     public function moveOrderDown()
     {
-        $model = $this->eloquent();
+        $model = $this->model();
 
         if (! $model instanceof Sortable) {
             throw new \RuntimeException(
@@ -514,7 +514,7 @@ class EloquentRepository extends Repository implements TreeRepository
      */
     public function getParentColumn()
     {
-        $model = $this->eloquent();
+        $model = $this->model();
 
         if (method_exists($model, 'getParentColumn')) {
             return $model->getParentColumn();
@@ -528,7 +528,7 @@ class EloquentRepository extends Repository implements TreeRepository
      */
     public function getTitleColumn()
     {
-        $model = $this->eloquent();
+        $model = $this->model();
 
         if (method_exists($model, 'getTitleColumn')) {
             return $model->getTitleColumn();
@@ -542,7 +542,7 @@ class EloquentRepository extends Repository implements TreeRepository
      */
     public function getOrderColumn()
     {
-        $model = $this->eloquent();
+        $model = $this->model();
 
         if (method_exists($model, 'getOrderColumn')) {
             return $model->getOrderColumn();
@@ -557,7 +557,7 @@ class EloquentRepository extends Repository implements TreeRepository
      */
     public function saveOrder($tree = [], $parentId = 0)
     {
-        $this->eloquent()->saveOrder($tree, $parentId);
+        $this->model()->saveOrder($tree, $parentId);
     }
 
     /**
@@ -569,7 +569,7 @@ class EloquentRepository extends Repository implements TreeRepository
      */
     public function withQuery($queryCallback)
     {
-        $this->eloquent()->withQuery($queryCallback);
+        $this->model()->withQuery($queryCallback);
 
         return $this;
     }
@@ -587,7 +587,7 @@ class EloquentRepository extends Repository implements TreeRepository
             });
         }
 
-        return $this->eloquent()->toTree();
+        return $this->model()->toTree();
     }
 
     /**
@@ -599,7 +599,7 @@ class EloquentRepository extends Repository implements TreeRepository
             return clone $this->queryBuilder;
         }
 
-        return $this->eloquent()->newQuery();
+        return $this->model()->newQuery();
     }
 
     /**
@@ -607,9 +607,9 @@ class EloquentRepository extends Repository implements TreeRepository
      *
      * @return EloquentModel
      */
-    public function eloquent()
+    public function model()
     {
-        return $this->model ?: ($this->model = $this->createEloquent());
+        return $this->model ?: ($this->model = $this->createModel());
     }
 
     /**
@@ -617,7 +617,7 @@ class EloquentRepository extends Repository implements TreeRepository
      *
      * @return EloquentModel
      */
-    public function createEloquent(array $data = [])
+    public function createModel(array $data = [])
     {
         $model = new $this->eloquentClass();
 

+ 0 - 150
src/Repositories/Proxy.php

@@ -1,150 +0,0 @@
-<?php
-
-namespace Dcat\Admin\Repositories;
-
-use Dcat\Admin\Form;
-use Dcat\Admin\Grid;
-use Dcat\Admin\Show;
-
-/**
- * @deprecated 即将在2.0中废弃
- */
-class Proxy implements \Dcat\Admin\Contracts\Repository
-{
-    protected $repository;
-
-    protected $__listeners = [];
-
-    protected $__caches = [
-        'edit'     => [],
-        'detail'   => [],
-        'updating' => [],
-    ];
-
-    public function __construct(Repository $repository)
-    {
-        $this->repository = $repository;
-
-        $this->__listeners = Repository::getListeners(get_class($repository));
-    }
-
-    public function getOriginalClassName()
-    {
-        return get_class($this->repository);
-    }
-
-    public function getKeyName()
-    {
-        return $this->repository->getKeyName();
-    }
-
-    public function isSoftDeletes()
-    {
-        return $this->repository->isSoftDeletes();
-    }
-
-    public function getCreatedAtColumn()
-    {
-        return $this->repository->getCreatedAtColumn();
-    }
-
-    public function getUpdatedAtColumn()
-    {
-        return $this->repository->getUpdatedAtColumn();
-    }
-
-    public function get(Grid\Model $model)
-    {
-        return $this->repository->get($model);
-    }
-
-    public function edit(Form $form): array
-    {
-        $id = $form->getKey();
-
-        if (array_key_exists($id, $this->__caches['edit'])) {
-            return $this->__caches['edit'][$id];
-        }
-
-        return $this->__caches['edit'][$id] = $this->repository->edit($form);
-    }
-
-    public function detail(Show $show): array
-    {
-        $id = $show->getKey();
-
-        if (array_key_exists($id, $this->__caches['detail'])) {
-            return $this->__caches['detail'][$id];
-        }
-
-        return $this->__caches['detail'][$id] = $this->repository->detail($show);
-    }
-
-    public function store(Form $form)
-    {
-        foreach ($this->__listeners as $listener) {
-            $listener->creating($form);
-        }
-
-        $newId = $this->repository->store($form);
-
-        foreach ($this->__listeners as $listener) {
-            $listener->created($form, $newId);
-        }
-
-        return $newId;
-    }
-
-    public function getDataWhenUpdating(Form $form): array
-    {
-        $id = $form->getKey();
-
-        if (array_key_exists($id, $this->__caches['updating'])) {
-            return $this->__caches['updating'][$id];
-        }
-
-        return $this->__caches['updating'][$id] = $this->repository->getDataWhenUpdating($form);
-    }
-
-    public function update(Form $form)
-    {
-        $editAttributes = $this->__caches['edit'] ?? [];
-
-        foreach ($this->__listeners as $listener) {
-            $listener->updating($form, $editAttributes);
-        }
-
-        $result = $this->repository->update($form);
-
-        foreach ($this->__listeners as $listener) {
-            $listener->updated($form, $editAttributes, $result);
-        }
-
-        return $result;
-    }
-
-    public function destroy(Form $form, array $deletingData)
-    {
-        foreach ($this->__listeners as $listener) {
-            $listener->deleting($form, $deletingData);
-        }
-
-        $result = $this->repository->destroy($form, $deletingData);
-
-        foreach ($this->__listeners as $listener) {
-            $listener->deleted($form, $deletingData, $result);
-        }
-
-        return $result;
-    }
-
-    public function getDataWhenDeleting(Form $form): array
-    {
-        return $this->repository->getDataWhenDeleting($form);
-    }
-
-    public function __call($method, $arguments)
-    {
-        return $this->repository->$method(...$arguments);
-    }
-}

+ 23 - 0
src/Support/Helper.php

@@ -2,6 +2,7 @@
 
 namespace Dcat\Admin\Support;
 
+use Dcat\Admin\Admin;
 use Dcat\Admin\Grid;
 use Dcat\Laravel\Database\WhereHasInServiceProvider;
 use Illuminate\Contracts\Support\Arrayable;
@@ -799,4 +800,26 @@ class Helper
             $relation->$query(...$params);
         });
     }
+
+    /**
+     * @param string $html
+     *
+     * @return string
+     */
+    public static function html($html)
+    {
+        if (! $html = static::render($html)) {
+            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);
+
+        dd($js, $css);
+    }
 }

+ 0 - 31
src/Traits/HasBuilderEvents.php

@@ -4,52 +4,26 @@ namespace Dcat\Admin\Traits;
 
 trait HasBuilderEvents
 {
-    /**
-     * Register a resolving event.
-     *
-     * @param callable $callback
-     * @param bool     $once
-     */
     public static function resolving(callable $callback, bool $once = false)
     {
         static::addBuilderListeners('builder.resolving', $callback, $once);
     }
 
-    /**
-     * Call the resolving callbacks.
-     *
-     * @param array ...$params
-     */
     protected function callResolving(...$params)
     {
         $this->fireBuilderEvent('builder.resolving', ...$params);
     }
 
-    /**
-     * Register a composing event.
-     *
-     * @param callable $callback
-     * @param bool     $once
-     */
     public static function composing(callable $callback, bool $once = false)
     {
         static::addBuilderListeners('builder.composing', $callback, $once);
     }
 
-    /**
-     * Call the composing callbacks.
-     *
-     * @param array ...$params
-     */
     protected function callComposing(...$params)
     {
         $this->fireBuilderEvent('builder.composing', ...$params);
     }
 
-    /**
-     * @param $listeners
-     * @param array ...$params
-     */
     protected function fireBuilderEvent($key, ...$params)
     {
         $storage = app('admin.context');
@@ -71,11 +45,6 @@ trait HasBuilderEvents
         $storage[$key] = $listeners;
     }
 
-    /**
-     * @param string   $key
-     * @param callable $callback
-     * @param bool     $once
-     */
     protected static function addBuilderListeners($key, $callback, $once)
     {
         $storage = app('admin.context');