Răsfoiți Sursa

feat: twig view service

M1Screw 1 an în urmă
părinte
comite
73ef4df9d1

+ 2 - 3
.gitignore

@@ -28,11 +28,10 @@ public/clients/*.*
 
 storage/framework/smarty/cache/*
 storage/framework/smarty/compile/*
-storage/framework/twig/*
-storage/*.*
+storage/framework/twig/cache/*
 !storage/framework/smarty/cache/.gitkeep
 !storage/framework/smarty/compile/.gitkeep
-!storage/framework/twig/.gitkeep
+!storage/framework/twig/cache/.gitkeep
 
 .user.ini
 public/.user.ini

+ 1 - 0
composer.json

@@ -40,6 +40,7 @@
         "stripe/stripe-php": "^12.8",
         "symfony/translation": "^6.3.7",
         "tronovav/geoip2-update": "^2.3.1",
+        "twig/twig": "^3.8",
         "vectorface/googleauthenticator": "^3.0",
         "voku/anti-xss": "^4.1.42"
     },

+ 155 - 83
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "25635044dfd9ba42712e0f640b98c453",
+    "content-hash": "b90c6c01b6b2836ce21293a49dfe7719",
     "packages": [
         {
             "name": "anankke/omnipay-alipay",
@@ -5223,6 +5223,88 @@
             ],
             "time": "2023-05-12T14:21:09+00:00"
         },
+        {
+            "name": "symfony/polyfill-ctype",
+            "version": "v1.28.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/symfony/polyfill-ctype.git",
+                "reference": "ea208ce43cbb04af6867b4fdddb1bdbf84cc28cb"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/ea208ce43cbb04af6867b4fdddb1bdbf84cc28cb",
+                "reference": "ea208ce43cbb04af6867b4fdddb1bdbf84cc28cb",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=7.1"
+            },
+            "provide": {
+                "ext-ctype": "*"
+            },
+            "suggest": {
+                "ext-ctype": "For best performance"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-main": "1.28-dev"
+                },
+                "thanks": {
+                    "name": "symfony/polyfill",
+                    "url": "https://github.com/symfony/polyfill"
+                }
+            },
+            "autoload": {
+                "files": [
+                    "bootstrap.php"
+                ],
+                "psr-4": {
+                    "Symfony\\Polyfill\\Ctype\\": ""
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Gert de Pagter",
+                    "email": "[email protected]"
+                },
+                {
+                    "name": "Symfony Community",
+                    "homepage": "https://symfony.com/contributors"
+                }
+            ],
+            "description": "Symfony polyfill for ctype functions",
+            "homepage": "https://symfony.com",
+            "keywords": [
+                "compatibility",
+                "ctype",
+                "polyfill",
+                "portable"
+            ],
+            "support": {
+                "source": "https://github.com/symfony/polyfill-ctype/tree/v1.28.0"
+            },
+            "funding": [
+                {
+                    "url": "https://symfony.com/sponsor",
+                    "type": "custom"
+                },
+                {
+                    "url": "https://github.com/fabpot",
+                    "type": "github"
+                },
+                {
+                    "url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
+                    "type": "tidelift"
+                }
+            ],
+            "time": "2023-01-26T09:26:14+00:00"
+        },
         {
             "name": "symfony/polyfill-iconv",
             "version": "v1.28.0",
@@ -6101,6 +6183,78 @@
             },
             "time": "2023-10-25T22:16:50+00:00"
         },
+        {
+            "name": "twig/twig",
+            "version": "v3.8.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/twigphp/Twig.git",
+                "reference": "9d15f0ac07f44dc4217883ec6ae02fd555c6f71d"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/twigphp/Twig/zipball/9d15f0ac07f44dc4217883ec6ae02fd555c6f71d",
+                "reference": "9d15f0ac07f44dc4217883ec6ae02fd555c6f71d",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=7.2.5",
+                "symfony/polyfill-ctype": "^1.8",
+                "symfony/polyfill-mbstring": "^1.3",
+                "symfony/polyfill-php80": "^1.22"
+            },
+            "require-dev": {
+                "psr/container": "^1.0|^2.0",
+                "symfony/phpunit-bridge": "^5.4.9|^6.3|^7.0"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Twig\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "BSD-3-Clause"
+            ],
+            "authors": [
+                {
+                    "name": "Fabien Potencier",
+                    "email": "[email protected]",
+                    "homepage": "http://fabien.potencier.org",
+                    "role": "Lead Developer"
+                },
+                {
+                    "name": "Twig Team",
+                    "role": "Contributors"
+                },
+                {
+                    "name": "Armin Ronacher",
+                    "email": "[email protected]",
+                    "role": "Project Founder"
+                }
+            ],
+            "description": "Twig, the flexible, fast, and secure template language for PHP",
+            "homepage": "https://twig.symfony.com",
+            "keywords": [
+                "templating"
+            ],
+            "support": {
+                "issues": "https://github.com/twigphp/Twig/issues",
+                "source": "https://github.com/twigphp/Twig/tree/v3.8.0"
+            },
+            "funding": [
+                {
+                    "url": "https://github.com/fabpot",
+                    "type": "github"
+                },
+                {
+                    "url": "https://tidelift.com/funding/github/packagist/twig/twig",
+                    "type": "tidelift"
+                }
+            ],
+            "time": "2023-11-21T18:54:41+00:00"
+        },
         {
             "name": "vectorface/googleauthenticator",
             "version": "v3.0",
@@ -9573,88 +9727,6 @@
             ],
             "time": "2023-09-26T12:56:25+00:00"
         },
-        {
-            "name": "symfony/polyfill-ctype",
-            "version": "v1.28.0",
-            "source": {
-                "type": "git",
-                "url": "https://github.com/symfony/polyfill-ctype.git",
-                "reference": "ea208ce43cbb04af6867b4fdddb1bdbf84cc28cb"
-            },
-            "dist": {
-                "type": "zip",
-                "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/ea208ce43cbb04af6867b4fdddb1bdbf84cc28cb",
-                "reference": "ea208ce43cbb04af6867b4fdddb1bdbf84cc28cb",
-                "shasum": ""
-            },
-            "require": {
-                "php": ">=7.1"
-            },
-            "provide": {
-                "ext-ctype": "*"
-            },
-            "suggest": {
-                "ext-ctype": "For best performance"
-            },
-            "type": "library",
-            "extra": {
-                "branch-alias": {
-                    "dev-main": "1.28-dev"
-                },
-                "thanks": {
-                    "name": "symfony/polyfill",
-                    "url": "https://github.com/symfony/polyfill"
-                }
-            },
-            "autoload": {
-                "files": [
-                    "bootstrap.php"
-                ],
-                "psr-4": {
-                    "Symfony\\Polyfill\\Ctype\\": ""
-                }
-            },
-            "notification-url": "https://packagist.org/downloads/",
-            "license": [
-                "MIT"
-            ],
-            "authors": [
-                {
-                    "name": "Gert de Pagter",
-                    "email": "[email protected]"
-                },
-                {
-                    "name": "Symfony Community",
-                    "homepage": "https://symfony.com/contributors"
-                }
-            ],
-            "description": "Symfony polyfill for ctype functions",
-            "homepage": "https://symfony.com",
-            "keywords": [
-                "compatibility",
-                "ctype",
-                "polyfill",
-                "portable"
-            ],
-            "support": {
-                "source": "https://github.com/symfony/polyfill-ctype/tree/v1.28.0"
-            },
-            "funding": [
-                {
-                    "url": "https://symfony.com/sponsor",
-                    "type": "custom"
-                },
-                {
-                    "url": "https://github.com/fabpot",
-                    "type": "github"
-                },
-                {
-                    "url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
-                    "type": "tidelift"
-                }
-            ],
-            "time": "2023-01-26T09:26:14+00:00"
-        },
         {
             "name": "symfony/polyfill-php81",
             "version": "v1.28.0",

+ 28 - 1
src/Controllers/BaseController.php

@@ -8,6 +8,7 @@ use App\Models\User;
 use App\Services\Auth;
 use App\Services\View;
 use Smarty;
+use Twig\Environment;
 
 abstract class BaseController
 {
@@ -16,6 +17,11 @@ abstract class BaseController
      */
     protected Smarty $view;
 
+    /**
+     * @var Environment
+     */
+    protected Environment $twig;
+
     /**
      * @var User
      */
@@ -26,7 +32,6 @@ abstract class BaseController
      */
     public function __construct()
     {
-        $this->view = View::getSmarty();
         $this->user = Auth::getUser();
     }
 
@@ -35,6 +40,8 @@ abstract class BaseController
      */
     public function view(): Smarty
     {
+        $this->view = View::getSmarty();
+
         if (View::$connection) {
             $this->view->assign(
                 'queryLog',
@@ -46,4 +53,24 @@ abstract class BaseController
 
         return $this->view;
     }
+
+    /**
+     * Get twig
+     */
+    public function twig(): Environment
+    {
+        $this->twig = View::getTwig();
+
+        if (View::$connection) {
+            $this->twig->addGlobal(
+                'queryLog',
+                View::$connection
+                    ->connection('default')
+                    ->getQueryLog()
+            );
+            $this->twig->addGlobal('optTime', (microtime(true) - View::$beginTime) * 1000);
+        }
+
+        return $this->twig;
+    }
 }

+ 29 - 8
src/Services/View.php

@@ -7,6 +7,8 @@ namespace App\Services;
 use App\Models\Config;
 use Illuminate\Database\DatabaseManager;
 use Smarty;
+use Twig\Environment;
+use Twig\Loader\FilesystemLoader;
 
 final class View
 {
@@ -16,16 +18,9 @@ final class View
     public static function getSmarty(): Smarty
     {
         $smarty = new Smarty(); //实例化smarty
-
         $user = Auth::getUser();
 
-        if ($user->isLogin) {
-            $theme = $user->theme;
-        } else {
-            $theme = $_ENV['theme'];
-        }
-
-        $smarty->settemplatedir(BASE_PATH . '/resources/views/' . $theme . '/'); //设置模板文件存放目录
+        $smarty->settemplatedir(BASE_PATH . '/resources/views/' . self::getTheme($user) . '/'); //设置模板文件存放目录
         $smarty->setcompiledir(BASE_PATH . '/storage/framework/smarty/compile/'); //设置生成文件存放目录
         $smarty->setcachedir(BASE_PATH . '/storage/framework/smarty/cache/'); //设置缓存文件存放目录
         // add config
@@ -36,6 +31,32 @@ final class View
         return $smarty;
     }
 
+    public static function getTwig(): Environment
+    {
+        $loader = new FilesystemLoader(BASE_PATH . '/resources/views/' . self::getTheme($user) . '/');
+
+        $twig = new Environment($loader, [
+            'cache' => BASE_PATH . '/storage/framework/twig/cache/',
+        ]);
+
+        $twig->addGlobal('config', self::getConfig());
+        $twig->addGlobal('public_setting', Config::getPublicConfig());
+        $twig->addGlobal('user', $user);
+
+        return $twig;
+    }
+
+    public static function getTheme($user): string
+    {
+        if ($user->isLogin) {
+            $theme = $user->theme;
+        } else {
+            $theme = $_ENV['theme'];
+        }
+
+        return $theme;
+    }
+
     public static function getConfig(): array
     {
         return [

+ 0 - 0
storage/framework/twig/.gitkeep → storage/framework/twig/cache/.gitkeep