* zin: add context

This commit is contained in:
Hao Sun
2023-03-27 16:34:09 +08:00
parent f7f0e284ee
commit 72882bcf08
4 changed files with 76 additions and 17 deletions
+48
View File
@@ -0,0 +1,48 @@
<?php
/**
* The context class file of zin of ZenTaoPMS.
*
* @copyright Copyright 2023 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
* @author Hao Sun <sunhao@easycorp.ltd>
* @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]);
}
}
+4 -4
View File
@@ -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()
+5
View File
@@ -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);
}
}
+19 -13
View File
@@ -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;