jqh 5 anni fa
parent
commit
87c0e62720
5 ha cambiato i file con 208 aggiunte e 35 eliminazioni
  1. 2 0
      src/Admin.php
  2. 1 9
      src/Layout/Asset.php
  3. 30 2
      src/Support/helpers.php
  4. 0 24
      src/Traits/HasAssets.php
  5. 175 0
      src/Traits/Renderable.php

+ 2 - 0
src/Admin.php

@@ -13,6 +13,7 @@ use Dcat\Admin\Repositories\EloquentRepository;
 use Dcat\Admin\Support\Composer;
 use Dcat\Admin\Traits\HasAssets;
 use Dcat\Admin\Traits\HasPermissions;
+use Dcat\Admin\Traits\Renderable;
 use Illuminate\Auth\GuardHelpers;
 use Illuminate\Contracts\Auth\Authenticatable;
 use Illuminate\Database\Eloquent\Builder;
@@ -28,6 +29,7 @@ use Illuminate\Support\Str;
 class Admin
 {
     use HasAssets;
+    use Renderable;
 
     /**
      * 版本号.

+ 1 - 9
src/Layout/Asset.php

@@ -253,14 +253,6 @@ class Asset
      */
     protected $isPjax = false;
 
-    /**
-     * @var array
-     */
-    protected $themeCssMap = [
-        'dark'      => 'dark-layout',
-        'semi-dark' => 'semi-dark-layout',
-    ];
-
     /**
      * Assets constructor.
      */
@@ -338,7 +330,7 @@ class Asset
      *
      * @param string $alias
      */
-    public function collect(string $alias)
+    public function collect(?string $alias)
     {
         if (mb_strpos($alias, '@') !== 0) {
             $alias = '@'.$alias;

+ 30 - 2
src/Support/helpers.php

@@ -371,7 +371,6 @@ if (! function_exists('admin_info')) {
 }
 
 if (! function_exists('admin_asset')) {
-
     /**
      * @param $path
      *
@@ -383,6 +382,19 @@ if (! function_exists('admin_asset')) {
     }
 }
 
+if (! function_exists('admin_require_assets')) {
+
+    /**
+     * @param $alias
+     *
+     * @return void
+     */
+    function admin_require_assets(?string $alias)
+    {
+        Admin::asset()->collect($alias);
+    }
+}
+
 if (! function_exists('admin_api_route')) {
 
     /**
@@ -410,4 +422,20 @@ if (! function_exists('admin_extension_path')) {
 
         return $path ? $dir.'/'.$path : $dir;
     }
-}
+}
+
+if (! function_exists('admin_color')) {
+    /**
+     * @param string|null $color
+     *
+     * @return string|\Dcat\Admin\Color
+     */
+    function admin_color(?string $color = null)
+    {
+        if ($color === null) {
+            return Admin::color();
+        }
+
+        return Admin::color()->get($color);
+    }
+}

+ 0 - 24
src/Traits/HasAssets.php

@@ -6,30 +6,6 @@ use Dcat\Admin\Support\Helper;
 
 trait HasAssets
 {
-    /**
-     * @var array
-     */
-    protected static $html = [];
-
-    /**
-     * @param string $html
-     *
-     * @return null|string
-     */
-    public static function html($html = '')
-    {
-        if (! empty($html)) {
-            static::$html = array_merge(
-                static::$html,
-                array_map([Helper::class, 'render'], (array) $html)
-            );
-
-            return;
-        }
-
-        return implode('', array_unique(static::$html));
-    }
-
     /**
      * @return \Dcat\Admin\Layout\Asset
      */

+ 175 - 0
src/Traits/Renderable.php

@@ -0,0 +1,175 @@
+<?php
+
+namespace Dcat\Admin\Traits;
+
+use Dcat\Admin\Support\Helper;
+use DOMElement;
+use DOMDocument;
+
+trait Renderable
+{
+    private static $shouldResolveTags = ['style', 'script', 'template'];
+
+    /**
+     * @var array
+     */
+    public static $html = [];
+
+    /**
+     * @param string $html
+     *
+     * @return null|string
+     */
+    public static function html($html = '')
+    {
+        $html = static::context()->html;
+
+        if (! empty($html)) {
+
+            static::$html = array_merge(
+                static::$html,
+                array_map([Helper::class, 'render'], (array) $html)
+            );
+
+            return;
+        }
+
+        return implode('', array_unique(static::$html));
+    }
+
+    /**
+     * @param string $view
+     * @param array  $data
+     *
+     * @return string
+     *
+     * @throws \Throwable
+     */
+    public static function view(string $view, array $data = [])
+    {
+        return static::render(view($view, $data));
+    }
+
+    /**
+     * @param string|\Illuminate\Contracts\Support\Renderable $view
+     * @param array                                           $data
+     *
+     * @throws \Throwable
+     *
+     * @return string
+     */
+    public static function render($value): string
+    {
+        $dom = static::getDOMDocument(Helper::render($value));
+
+        $head = $dom->getElementsByTagName('head')->item(0) ?: null;
+        $body = $dom->getElementsByTagName('body')->item(0) ?: null;
+
+        return static::resolveElement($head).static::resolveElement($body);
+    }
+
+    /**
+     * @param string $html
+     *
+     * @throws \Throwable
+     *
+     * @return DOMDocument
+     */
+    protected static function getDOMDocument(string $html)
+    {
+        $dom = new DOMDocument();
+
+        libxml_use_internal_errors(true);
+
+        $dom->loadHTML('<?xml encoding="utf-8" ?>'.$html);
+
+        libxml_use_internal_errors(false);
+
+        return $dom;
+    }
+
+    /**
+     * @param DOMElement $element
+     *
+     * @return void
+     */
+    protected static function resolve(DOMElement $element)
+    {
+        $method = 'resolve'.ucfirst($element->tagName);
+
+        return static::{$method}($element);
+    }
+
+    /**
+     * @param DOMElement $element
+     *
+     * @return void
+     */
+    protected static function resolveScript(DOMElement $element)
+    {
+        if ($element->hasAttribute('src')) {
+            static::js($element->getAttribute('src'));
+
+            return;
+        }
+
+        if (! empty($script = trim($element->nodeValue))) {
+            if ($require = $element->getAttribute('require')) {
+                static::asset()->collect($require);
+            }
+
+            static::script('(function () {'.$script.'})()');
+        }
+    }
+
+    /**
+     * @param DOMElement $element
+     *
+     * @return void
+     */
+    protected static function resolveStyle(DOMElement $element)
+    {
+        if (! empty(trim($element->nodeValue))) {
+            static::style($element->nodeValue);
+        }
+    }
+
+    /**
+     * @param DOMElement $element
+     *
+     * @return void
+     */
+    protected static function resolveTemplate(DOMElement $element)
+    {
+        $html = '';
+        foreach ($element->childNodes as $childNode) {
+            $html .= $element->ownerDocument->saveHTML($childNode);
+        }
+
+        $html && static::html($html);
+    }
+
+    protected static function resolveElement(?DOMElement $element)
+    {
+        $html = '';
+
+        if (! $element) {
+            return $html;
+        }
+
+        foreach ($element->childNodes as $child) {
+            if (
+                $element instanceof DOMElement
+                && in_array($element->tagName, static::$shouldResolveTags, true)
+            ) {
+                static::resolve($child);
+
+                continue;
+            }
+
+            $html .= trim($element->ownerDocument->saveHTML($child));
+
+            return $html;
+        }
+    }
+}