From 72882bcf0802f2d8d87d4a99ee48dcbdfc2ec587 Mon Sep 17 00:00:00 2001 From: Hao Sun Date: Mon, 27 Mar 2023 16:34:09 +0800 Subject: [PATCH] * zin: add context --- zin/core/context.class.php | 48 ++++++++++++++++++++++++++++++++++++++ zin/core/h.class.php | 8 +++---- zin/core/portal.class.php | 5 ++++ zin/core/wg.class.php | 32 ++++++++++++++----------- 4 files changed, 76 insertions(+), 17 deletions(-) create mode 100644 zin/core/context.class.php diff --git a/zin/core/context.class.php b/zin/core/context.class.php new file mode 100644 index 0000000000..f66b8fabbf --- /dev/null +++ b/zin/core/context.class.php @@ -0,0 +1,48 @@ + + * @package zin + * @version $Id + * @link https://www.zentao.net + */ + +namespace zin; + +class context +{ + public $root; + + public $portals; + + public function __construct($root) + { + $this->root = $root; + $this->portals = $root->getPortals(); + } + + public function isRoot($root) + { + if(is_string($root)) return $root === $this->root->gid; + return $root->gid === $this->root->gid; + } + + public static $map = array(); + + public static function create($wg) + { + $gid = $wg->gid; + if(isset(static::$map[$gid])) return static::$map[$gid]; + $context = new context($wg); + static::$map[$gid] = $context; + return $context; + } + + public static function destroy($gid) + { + if($gid instanceof wg) $gid = $gid->gid; + if(isset(static::$map[$gid])) unset(static::$map[$gid]); + } +} diff --git a/zin/core/h.class.php b/zin/core/h.class.php index c9a61be06d..7bc04726d9 100644 --- a/zin/core/h.class.php +++ b/zin/core/h.class.php @@ -43,12 +43,12 @@ class h extends wg * Render widget to html * @return string */ - public function render(&$portals = NULL) + public function render(&$context = NULL) { - if(!empty($portals)) + if($context instanceof context) { $this->matchedPortals = array(); - foreach($portals as $portal) + foreach($context->portals as $portal) { if($this->isMatch($portal->prop('target'))) $this->matchedPortals[] = $portal->children(); } @@ -57,7 +57,7 @@ class h extends wg { $this->matchedPortals = NULL; } - return parent::render($portals); + return parent::render($context); } public function isSelfClose() diff --git a/zin/core/portal.class.php b/zin/core/portal.class.php index e0ff12f256..0d306bcbe9 100644 --- a/zin/core/portal.class.php +++ b/zin/core/portal.class.php @@ -16,4 +16,9 @@ require_once 'wg.class.php'; class portal extends wg { static $defineProps = 'target:string'; + + public static function __callStatic($name, $args) + { + return new portal(set('target', $name), $args); + } } diff --git a/zin/core/wg.class.php b/zin/core/wg.class.php index 8defd3b779..4f79a6a6ab 100644 --- a/zin/core/wg.class.php +++ b/zin/core/wg.class.php @@ -11,11 +11,10 @@ namespace zin; -use stdClass; - require_once 'props.class.php'; require_once 'directive.func.php'; require_once 'zin.class.php'; +require_once 'context.class.php'; class wg { @@ -63,21 +62,28 @@ class wg * Render widget to html * @return string */ - public function render(&$portals = NULL) + public function render(&$context = NULL) { - $before = $this->buildBefore(); - $children = $this->build(); - $after = $this->buildAfter(); + if($context === NULL) $context = context::create($this); - return static::renderToHtml(array($before, $children, $after), $portals); + $before = $this->buildBefore($context); + $children = $this->build($context); + $after = $this->buildAfter($context); + + $html = static::renderToHtml(array($before, $children, $after), $context); + + context::destroy($this->gid); + + return $html; } public function display() { zin::disableGlobalRender(); - $portals = $this->getPortals(); - $html = $this->render($portals); + $context = context::create($this); + $html = $this->render($context); + echo $html; $this->displayed = true; @@ -538,7 +544,7 @@ class wg /** * @return string */ - public static function renderToHtml($children, &$portals = NULL) + public static function renderToHtml($children, &$context = NULL) { $html = array(); foreach($children as $child) @@ -547,11 +553,11 @@ class wg if(is_array($child)) { - $html[] = static::renderToHtml($child, $portals); + $html[] = static::renderToHtml($child, $context); } elseif($child instanceof wg) { - $html[] = $child->render($portals); + $html[] = $child->render($context); } elseif(is_string($child)) { @@ -559,7 +565,7 @@ class wg } elseif(is_object($child)) { - if(method_exists($child, 'render')) $html[] = $child->render($portals); + if(method_exists($child, 'render')) $html[] = $child->render($context); elseif(isHtml($child)) $html[] = $child->data; elseif(isText($child)) $html[] = htmlspecialchars($child->data); elseif(isset($child->html)) $html[] = $child->html;