diff --git a/composer.json b/composer.json index 3c14dc7..3f422e7 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ "ext-openssl": "*", "ext-mbstring": "*", "psr/log": "1.*", + "vlucas/phpdotenv":"3.3", "guzzlehttp/guzzle":"7.*", "illuminate/contracts": "7.*", "illuminate/container": "7.*", @@ -36,12 +37,16 @@ "illuminate/http":"7.*", "illuminate/support": "7.*", "illuminate/pagination": "7.*", - "vlucas/phpdotenv":"3.3" + "windwalker/renderer":"3.*" }, "suggest": { - "hashids/hashids": "Required HashID In the Eloquent", - "nesbot/carbon": "Include Carbon DateTime Formating", - "twig/twig": "Required Twig 3.x Of View Module" + "hashids/hashids": "Suggest HashID In the Eloquent", + "nesbot/carbon": "Suggest Carbon DateTime Formating", + "twig/twig": "Suggest Twig 2.x Of View Engine", + "league/plates": "Suggest Plates 3.x Of View Engine", + "windwalker/edge": "Suggest Edge 3.x Of View Engine", + "illuminate/view": "Suggest Blade 7.* Of View Engine", + "mustache/mustache": "Suggest Mustcache 2.x Of View Engine" }, "autoload": { "psr-4": { diff --git a/src/Application.php b/src/Application.php index 57f5717..b35d0d7 100644 --- a/src/Application.php +++ b/src/Application.php @@ -3,7 +3,7 @@ namespace VM; /** - * Varimax The Full Stack PHP Frameworks. + * Varimax The Slim PHP Frameworks. * varimax * FILE: 2020 * USER: 陶之11. @@ -276,7 +276,7 @@ public function cli() } /** - * GET Varimax The Full Stack PHP Frameworks Version" + * GET Varimax The Slim PHP Frameworks Version" * @return string */ public function version() diff --git a/src/Bootstrap.php b/src/Bootstrap.php index 068eee7..3c26bfb 100644 --- a/src/Bootstrap.php +++ b/src/Bootstrap.php @@ -1,6 +1,6 @@ diff --git a/src/Cache/Cache.php b/src/Cache/Cache.php index 72d37e0..9f7e970 100755 --- a/src/Cache/Cache.php +++ b/src/Cache/Cache.php @@ -1,6 +1,6 @@ diff --git a/src/Cache/Driver/ApcDriver.php b/src/Cache/Driver/ApcDriver.php index debebd6..faa3068 100644 --- a/src/Cache/Driver/ApcDriver.php +++ b/src/Cache/Driver/ApcDriver.php @@ -1,6 +1,6 @@ diff --git a/src/Cache/Driver/Driver.php b/src/Cache/Driver/Driver.php index 8cb7fb8..7236ab1 100644 --- a/src/Cache/Driver/Driver.php +++ b/src/Cache/Driver/Driver.php @@ -1,6 +1,6 @@ diff --git a/src/Cache/Driver/DriverInterface.php b/src/Cache/Driver/DriverInterface.php index 5723fe0..a6ebfd5 100644 --- a/src/Cache/Driver/DriverInterface.php +++ b/src/Cache/Driver/DriverInterface.php @@ -1,6 +1,6 @@ diff --git a/src/Cache/Driver/FileSystemDriver.php b/src/Cache/Driver/FileSystemDriver.php index dd7f6f9..443336e 100755 --- a/src/Cache/Driver/FileSystemDriver.php +++ b/src/Cache/Driver/FileSystemDriver.php @@ -1,6 +1,6 @@ diff --git a/src/Cache/Driver/FilesDriver.php b/src/Cache/Driver/FilesDriver.php index 68545d6..5caa12a 100644 --- a/src/Cache/Driver/FilesDriver.php +++ b/src/Cache/Driver/FilesDriver.php @@ -1,7 +1,7 @@ diff --git a/src/Cache/Driver/NullDriver.php b/src/Cache/Driver/NullDriver.php index a289629..780c8a4 100644 --- a/src/Cache/Driver/NullDriver.php +++ b/src/Cache/Driver/NullDriver.php @@ -1,6 +1,6 @@ diff --git a/src/Cache/Driver/RedisDriver.php b/src/Cache/Driver/RedisDriver.php index 6e15cca..74ea5c1 100644 --- a/src/Cache/Driver/RedisDriver.php +++ b/src/Cache/Driver/RedisDriver.php @@ -1,6 +1,6 @@ diff --git a/src/Config/Config.php b/src/Config/Config.php index 708394c..93d6735 100755 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -1,7 +1,7 @@ diff --git a/src/Controller.php b/src/Controller.php index 7ef413a..2654861 100755 --- a/src/Controller.php +++ b/src/Controller.php @@ -1,7 +1,7 @@ @@ -89,10 +89,9 @@ protected function make($abstract, array $parameters = []) */ protected function view($template, $variable = []) { - return make('view')->show($template, $variable); + return make('view')->display($template, $variable); } - /** * [assign] * @@ -105,6 +104,7 @@ protected function assign($var, $val = null) make('view')->assign($var, $val); } + public function __destruct() { $this->off(); diff --git a/src/Crypt/Crypt.php b/src/Crypt/Crypt.php index 578c75a..0b3d59e 100644 --- a/src/Crypt/Crypt.php +++ b/src/Crypt/Crypt.php @@ -1,7 +1,7 @@ diff --git a/src/Exception/E.php b/src/Exception/E.php index 868450e..eb99196 100644 --- a/src/Exception/E.php +++ b/src/Exception/E.php @@ -1,7 +1,7 @@ diff --git a/src/Exception/FatalError.php b/src/Exception/FatalError.php index ac8d9bd..6a37f64 100644 --- a/src/Exception/FatalError.php +++ b/src/Exception/FatalError.php @@ -1,7 +1,7 @@ diff --git a/src/Exception/FileException.php b/src/Exception/FileException.php index 024ee3c..21863e3 100644 --- a/src/Exception/FileException.php +++ b/src/Exception/FileException.php @@ -1,7 +1,7 @@ diff --git a/src/Exception/HttpException.php b/src/Exception/HttpException.php index 1f707f4..b9ca481 100755 --- a/src/Exception/HttpException.php +++ b/src/Exception/HttpException.php @@ -1,7 +1,7 @@ diff --git a/src/Exception/LogicException.php b/src/Exception/LogicException.php index 12a6031..a3ae951 100755 --- a/src/Exception/LogicException.php +++ b/src/Exception/LogicException.php @@ -1,7 +1,7 @@ diff --git a/src/Exception/NotFoundException.php b/src/Exception/NotFoundException.php index 111b3d7..36b682f 100755 --- a/src/Exception/NotFoundException.php +++ b/src/Exception/NotFoundException.php @@ -1,7 +1,7 @@ diff --git a/src/Exception/ServiceException.php b/src/Exception/ServiceException.php index c58715a..81a94ea 100755 --- a/src/Exception/ServiceException.php +++ b/src/Exception/ServiceException.php @@ -1,7 +1,7 @@ diff --git a/src/Exception/SystemException.php b/src/Exception/SystemException.php index 16db762..3f6c461 100755 --- a/src/Exception/SystemException.php +++ b/src/Exception/SystemException.php @@ -1,7 +1,7 @@ diff --git a/src/FileSystem/FileSystem.php b/src/FileSystem/FileSystem.php index d0e06af..f29950b 100644 --- a/src/FileSystem/FileSystem.php +++ b/src/FileSystem/FileSystem.php @@ -1,7 +1,7 @@ diff --git a/src/Http/Cookie.php b/src/Http/Cookie.php index d85c537..e82a239 100755 --- a/src/Http/Cookie.php +++ b/src/Http/Cookie.php @@ -1,7 +1,7 @@ diff --git a/src/Http/Curl/Curl.php b/src/Http/Curl/Curl.php index 04668f0..5afe82d 100644 --- a/src/Http/Curl/Curl.php +++ b/src/Http/Curl/Curl.php @@ -1,6 +1,6 @@ diff --git a/src/Http/Curl/Response.php b/src/Http/Curl/Response.php index 979fa22..3a48360 100644 --- a/src/Http/Curl/Response.php +++ b/src/Http/Curl/Response.php @@ -1,6 +1,6 @@ diff --git a/src/Http/Redirect.php b/src/Http/Redirect.php index 1428699..c0bb2ce 100644 --- a/src/Http/Redirect.php +++ b/src/Http/Redirect.php @@ -1,7 +1,7 @@ diff --git a/src/Http/Request.php b/src/Http/Request.php index 80b0e82..136cdee 100755 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -1,7 +1,7 @@ diff --git a/src/Http/Request/Upload.php b/src/Http/Request/Upload.php index 5fd124c..f6cbd59 100644 --- a/src/Http/Request/Upload.php +++ b/src/Http/Request/Upload.php @@ -1,6 +1,6 @@ diff --git a/src/Http/Response/Base.php b/src/Http/Response/Base.php index 6311eaf..a6ab144 100644 --- a/src/Http/Response/Base.php +++ b/src/Http/Response/Base.php @@ -1,6 +1,6 @@ diff --git a/src/Http/Response/BinaryFile.php b/src/Http/Response/BinaryFile.php index 7398369..eb62245 100644 --- a/src/Http/Response/BinaryFile.php +++ b/src/Http/Response/BinaryFile.php @@ -1,6 +1,6 @@ diff --git a/src/Http/Response/Json.php b/src/Http/Response/Json.php index e803f57..6ae9b2e 100755 --- a/src/Http/Response/Json.php +++ b/src/Http/Response/Json.php @@ -1,6 +1,6 @@ diff --git a/src/Http/Response/Redirect.php b/src/Http/Response/Redirect.php index cb29cda..1b2f093 100755 --- a/src/Http/Response/Redirect.php +++ b/src/Http/Response/Redirect.php @@ -1,7 +1,7 @@ diff --git a/src/Http/Response/Response.php b/src/Http/Response/Response.php index bcc724f..6c33c08 100644 --- a/src/Http/Response/Response.php +++ b/src/Http/Response/Response.php @@ -1,7 +1,7 @@ diff --git a/src/Http/Response/ResponseTrait.php b/src/Http/Response/ResponseTrait.php index e808ca2..67824b6 100755 --- a/src/Http/Response/ResponseTrait.php +++ b/src/Http/Response/ResponseTrait.php @@ -1,7 +1,7 @@ diff --git a/src/Http/Response/Streamed.php b/src/Http/Response/Streamed.php index ffa7662..949396f 100644 --- a/src/Http/Response/Streamed.php +++ b/src/Http/Response/Streamed.php @@ -1,7 +1,7 @@ diff --git a/src/Http/Session/Handler/FilesSessionHandler.php b/src/Http/Session/Handler/FilesSessionHandler.php index ab07dd1..2ec8175 100644 --- a/src/Http/Session/Handler/FilesSessionHandler.php +++ b/src/Http/Session/Handler/FilesSessionHandler.php @@ -1,7 +1,7 @@ diff --git a/src/Http/Session/Handler/MemcacheSessionHandler.php b/src/Http/Session/Handler/MemcacheSessionHandler.php index 6c60709..af29fd6 100644 --- a/src/Http/Session/Handler/MemcacheSessionHandler.php +++ b/src/Http/Session/Handler/MemcacheSessionHandler.php @@ -1,7 +1,7 @@ diff --git a/src/Http/Session/Handler/MemcachedSessionHandler.php b/src/Http/Session/Handler/MemcachedSessionHandler.php index 9ebf9f8..8baf956 100644 --- a/src/Http/Session/Handler/MemcachedSessionHandler.php +++ b/src/Http/Session/Handler/MemcachedSessionHandler.php @@ -1,7 +1,7 @@ diff --git a/src/Http/Session/Handler/NullSessionHandler.php b/src/Http/Session/Handler/NullSessionHandler.php index a0d8820..7b6d80b 100644 --- a/src/Http/Session/Handler/NullSessionHandler.php +++ b/src/Http/Session/Handler/NullSessionHandler.php @@ -1,7 +1,7 @@ diff --git a/src/Http/Session/Handler/RedisSessionHandler.php b/src/Http/Session/Handler/RedisSessionHandler.php index 2844b2b..cadbdd0 100644 --- a/src/Http/Session/Handler/RedisSessionHandler.php +++ b/src/Http/Session/Handler/RedisSessionHandler.php @@ -1,7 +1,7 @@ diff --git a/src/Http/Session/Handler/SqliteSessionHandler.php b/src/Http/Session/Handler/SqliteSessionHandler.php index 2c02c13..16bab25 100644 --- a/src/Http/Session/Handler/SqliteSessionHandler.php +++ b/src/Http/Session/Handler/SqliteSessionHandler.php @@ -1,7 +1,7 @@ diff --git a/src/Http/Session/SessionInterface.php b/src/Http/Session/SessionInterface.php index 5f9b6c5..2d19be3 100644 --- a/src/Http/Session/SessionInterface.php +++ b/src/Http/Session/SessionInterface.php @@ -1,7 +1,7 @@ diff --git a/src/I18n/Lang.php b/src/I18n/Lang.php index e79dd44..924b5d7 100644 --- a/src/I18n/Lang.php +++ b/src/I18n/Lang.php @@ -1,7 +1,7 @@ diff --git a/src/Logger/Logger.php b/src/Logger/Logger.php index 906f8db..5603816 100755 --- a/src/Logger/Logger.php +++ b/src/Logger/Logger.php @@ -1,7 +1,7 @@ diff --git a/src/Model.php b/src/Model.php index 0136237..c58f403 100644 --- a/src/Model.php +++ b/src/Model.php @@ -1,7 +1,7 @@ diff --git a/src/Routing/Route.php b/src/Routing/Route.php index b741ab5..d89af8c 100644 --- a/src/Routing/Route.php +++ b/src/Routing/Route.php @@ -1,6 +1,6 @@ diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 572fb81..263e9e9 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -1,7 +1,7 @@ diff --git a/src/Services/DatabaseServiceProvider.php b/src/Services/DatabaseServiceProvider.php index 3791138..1025c01 100644 --- a/src/Services/DatabaseServiceProvider.php +++ b/src/Services/DatabaseServiceProvider.php @@ -3,7 +3,7 @@ namespace VM\Services; /** - * Varimax The Full Stack PHP Frameworks. + * Varimax The Slim PHP Frameworks. * varimax.cn * FILE: 2020 * USER: 陶之11. diff --git a/src/Services/HashIdsServiceProvider.php b/src/Services/HashIdsServiceProvider.php index 84b9896..b57deb6 100644 --- a/src/Services/HashIdsServiceProvider.php +++ b/src/Services/HashIdsServiceProvider.php @@ -3,7 +3,7 @@ namespace VM\Services; /** - * Varimax The Full Stack PHP Frameworks. + * Varimax The Slim PHP Frameworks. * varimax.cn * FILE: 2020 * USER: 陶之11. diff --git a/src/Services/PaginationServiceProvider.php b/src/Services/PaginationServiceProvider.php index 2d959a0..a637a17 100644 --- a/src/Services/PaginationServiceProvider.php +++ b/src/Services/PaginationServiceProvider.php @@ -3,7 +3,7 @@ namespace VM\Services; /** - * Varimax The Full Stack PHP Frameworks. + * Varimax The Slim PHP Frameworks. * varimax.cn * FILE: 2020 * USER: 陶之11. diff --git a/src/Support/Facades/Response.php b/src/Support/Facades/Response.php index d1f1119..a46f8f9 100755 --- a/src/Support/Facades/Response.php +++ b/src/Support/Facades/Response.php @@ -1,7 +1,7 @@ diff --git a/src/Support/ServiceProvider.php b/src/Support/ServiceProvider.php index fb6585d..b40ea39 100644 --- a/src/Support/ServiceProvider.php +++ b/src/Support/ServiceProvider.php @@ -3,7 +3,7 @@ namespace VM\Support; /** - * Varimax The Full Stack PHP Frameworks. + * Varimax The Slim PHP Frameworks. * varimax.cn * FILE: 2020 * USER: 陶之11. diff --git a/src/View.php b/src/View.php index 0e82c2a..d41732e 100755 --- a/src/View.php +++ b/src/View.php @@ -1,11 +1,11 @@ - * Time: 2020-08-11-21:21 + * Time: 2023-02-16 * SITE: https://www.varimax.cn/ */ @@ -18,56 +18,195 @@ */ class View { /*** - * twig template dir - * @var string + * template paths + * @var array */ - protected $dir = '.'; + protected $dirs = []; /** + * template cache * - * @var + * @var string */ - protected $twig; + protected $cache; /** - * twig template cache - * - * @var string + * template variables + * @var array */ - protected $cache; + protected $assign = []; /** - * twig file extension - * @var string + * template renderer + * @var \Windwalker\Renderer\AbstractEngineRenderer */ - protected $append; + protected $engine = null; /** - * 模板公共变量 - * @var array + * Init view engine + */ + public function __construct($engine = null) + { + $this->dirs = config('view.dir', _DIR_._DS_.'View'); + $this->cache = config('view.cache', runtime('view')); + $this->engine = $this->engine($engine ? $engine : config('view.engine', 'blade')); + is_dir($this->cache) || mkdir($this->cache, 0777, true); + } + + /** + * add path to dirs + * @return self */ - protected $variables = array(); + public function dir() + { + $this->dirs = array_merge($this->dirs, func_get_args()); + return $this; + } /** - * [init 初始化模板引擎] + * set view cache dir + * @param $dir + * @return $this + */ + public function cache($dir) + { + $this->cache = $dir; + return $this; + } + + /** + * [assign 模板变量赋值方法] * - * @return \Twig\Environment - * @version v3 + * @param $var [变量名] + * @param null $val [变量值] + */ + public function assign($var, $val = null) + { + if(is_array($var)){ + $this->assign = array_merge_recursive($this->assign, $var); + }else{ + $this->assign[$var] = $val; + } + } + + /** + * set view engine + * @param $engine string + * @return + */ + private function engine($engine = null) + { + if($engine){ + if(!in_array($engine, $engines = ['php', 'edge', 'twig', 'blade', 'plates', 'mustcache'])){ + throw new \ErrorException('Only support ['. join(',',$engines) .'] template engine'); + } + $this->$engine(); + } + return $this->engine; + } + + /** + * [render 模板渲染] * + * @param $template + * @param $variable + * @return string + */ + public function render($template, array $variables = []) + { + return $this->engine()->render($template, array_merge($this->assign, Controller::$assign, $variables)); + } + + /** + * flush cache + * @return mixed + */ + public function flush() + { + return make('file')->cleanDirectory($this->cache); + } + + /** + * [display 模版展示] + * + * @param $template + * @param $variable */ - public function __construct() + public function display($template, array $variables = []) { - $this->dir = config('view.dir', _DIR_._DS_.'View'); - $this->cache = config('view.cache'); - $this->append = ltrim(config('view.append', 'twig'),'.'); + return make('response')->make($this->render($template, $variables)); + } - $loader = new \Twig\Loader\FilesystemLoader($this->dir); + /** + * [Php 模板引擎] + * @return \Windwalker\Renderer\PhpRenderer + * @version 20230215 + */ + public function php() + { + $this->engine = new \Windwalker\Renderer\PhpRenderer($this->dirs); + return $this; + } + /** + * [Edge 模板引擎] + * @return \Windwalker\Renderer\EdgeRenderer + * @version 20230215 + */ + public function edge() + { + $this->engine = new \Windwalker\Renderer\EdgeRenderer($this->dirs, ['cache_path' => $this->cache]); + return $this; + } + + /** + * [Blade 模板引擎] + * @return \Windwalker\Renderer\BladeRenderer + * @version 20230215 + */ + public function blade() + { + $this->engine = new \Windwalker\Renderer\BladeRenderer($this->dirs, ['cache_path' => $this->cache]); + return $this; + } + + /** + * [plates 模板引擎] + * @return \Windwalker\Renderer\PlatesRenderer + * @version 20230215 + */ + public function plates() + { + $this->engine = new \Windwalker\Renderer\PlatesRenderer($this->dirs); + return $this; + } + + /** + * [Mustache 模板引擎] + * @return \Windwalker\Renderer\MustacheRenderer + * @version 20230215 + */ + public function mustache() + { + $this->engine = new \Windwalker\Renderer\MustacheRenderer($this->dirs,[ + 'cache' => $this->cache, + 'cache_file_mode' => 0666, + 'cache_lambda_templates' => true, + 'delimiters' => '<% %>', + ]); + return $this; + } + + /** + * [twig 模板引擎] + * @return \Windwalker\Renderer\TwigRenderer + * @version v2.* + */ + public function twig(){ /** - * $loader->addPath($templateDir3); - * $loader->prependPath($templateDir4); + * @var \Twig_Environment $this->engine */ - $this->twig = new \Twig\Environment($loader, array( + $this->engine = new \Windwalker\Renderer\TwigRenderer($this->dirs, array( //生成的模板会有一个__toString()方法,可以用来显示生成的Node(缺省为false) 'debug' => config('view.debug', false), @@ -97,41 +236,25 @@ public function __construct() 'optimizations' => -1, )); - /* - $lexer = new \Twig_Lexer($this->twig, array( - 'tag_comment' => array('{#', '#}'), - 'tag_block' => array('{%', '%}'), - 'tag_variable' => array('{^', '^}'), - 'interpolation' => array('#{', '}'), - )); - $this->twig->setLexer($lexer); - */ - - /** - * 注册扩展方法 - * @var \Twig\Environment - * - * $this->twig = new Twig_Environment($loader,array('debug'=>true)); - * $this->twig->addExtension(new Twig_Extension_Debug()); - */ /** * 注册全局变量 + * @var \Twig_Environment $this->engine */ - $this->twig->addGlobal('_VM_', _VM_); - $this->twig->addGlobal('_APP_',_APP_); - $this->twig->addGlobal('lang', app('lang')); - $this->twig->addGlobal('route', app('router')->route()); - $this->twig->addGlobal('router', app('router')); - $this->twig->addGlobal('request', app('request')); + $this->engine->getEngine()->addGlobal('_VM_', _VM_); + $this->engine->getEngine()->addGlobal('_APP_',_APP_); + $this->engine->getEngine()->addGlobal('lang', app('lang')); + $this->engine->getEngine()->addGlobal('route', app('router')->route()); + $this->engine->getEngine()->addGlobal('router', app('router')); + $this->engine->getEngine()->addGlobal('request', app('request')); //注册模板扩展 - //$this->twig->addExtension(new \nochso\HtmlCompressTwig\Extension()); + //$this->engine->getEngine()->addExtension(new \nochso\HtmlCompressTwig\Extension()); /** * 注册全局可用函数 * @example {{ function() }} */ - $this->twig->addFunction(new \Twig\TwigFunction('*', + $this->engine->getEngine()->addFunction(new \Twig\TwigFunction('*', function(...$args){ return call_user_func_array(array_shift($args), $args); }, @@ -146,7 +269,7 @@ function(...$args){ $dump = function($variable){ echo '
'.var_dump($variable).'
'; }; - $this->twig->addFunction(new \Twig\TwigFunction('dump', $dump, array('pre_escape' => 'html', 'is_safe' => array('html')))); + $this->engine->getEngine()->addFunction(new \Twig\TwigFunction('dump', $dump, array('pre_escape' => 'html', 'is_safe' => array('html')))); /** * [$debug 注册debug函数] @@ -156,126 +279,25 @@ function(...$args){ echo "
".print_r($variable)."
"; }; - $this->twig->addFunction(new \Twig\TwigFunction('debug', $debug, array('pre_escape' => 'html', 'is_safe' => array('html')))); + $this->engine->getEngine()->addFunction(new \Twig\TwigFunction('debug', $debug, array('pre_escape' => 'html', 'is_safe' => array('html')))); /** * 注册过滤器 */ - $this->twig->addFilter(new \Twig\TwigFilter('dump', $dump)); - $this->twig->addFilter(new \Twig\TwigFilter('debug', $debug)); + $this->engine->getEngine()->addFilter(new \Twig\TwigFilter('dump', $dump)); + $this->engine->getEngine()->addFilter(new \Twig\TwigFilter('debug', $debug)); /** * [$suffix 截取字符串] * @var [type] */ - $this->twig->addFilter(new \Twig\TwigFilter('len',function($string, $length, $suffix = false){ + $this->engine->getEngine()->addFilter(new \Twig\TwigFilter('len',function($string, $length, $suffix = false){ return $string = mb_strlen($string)>$length ? ($suffix ? mb_substr($string, 0, $length).$suffix : mb_substr($string, 0, $length)) : $string; })); - return $this->twig; - } - - /** - * paths for twig - * @return View - */ - public function paths() - { - return $this->path(func_get_args()); - } - - /** - * set path for twig - * @param $paths - * @param bool $keep keep old path - */ - public function path($paths, $keep = true) - { - if (!\is_array($paths)) { - $paths = [$paths]; - } - - $keep && $paths = array_merge($paths, $this->twig()->getLoader()->getPaths()) ; - - $this->twig()->getLoader()->setPaths($paths); - return $this; } - /** - * set view cache dir - * @param $dir - * @return $this - */ - public function cache($dir) - { - $this->twig()->setCache($dir); - - return $this; - } - - /** - * getTwig - * - * @return \Twig_Environment - */ - public function twig() - { - return $this->twig; - } - - /** - * [assign 模板变量赋值方法] - * - * @param $var [变量名] - * @param null $val [变量值] - */ - public function assign($var, $val = null) - { - if(is_array($var)) - { - foreach($var as $key => $v) - { - $this->variables[$key] = $v; - } - }else{ - $this->variables[$var] = $val; - } - } - - /** - * [render 模板渲染] - * - * @param $template - * @param $variable - * @return string - */ - public function render($template, $variables) - { - $template = sprintf($template .'%s'. $this->append, '.'); - $variables = array_merge($this->variables, (array) $variables, (array) Controller::$assign); - return $this->twig()->load($template)->render($variables); - } - - /** - * [show] - * - * @param $template - * @param $variable - */ - public function show($template, $variables) - { - return make('response')->make($this->render($template, $variables)); - } - - /** - * flush cache - * @return mixed - */ - public function flush() - { - return make('file')->cleanDirectory(config('view.cache')); - } }