directive.class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * The directive class file of zin lib.
  4. *
  5. * @copyright Copyright 2023 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
  6. * @author Hao Sun <sunhao@easycorp.ltd>
  7. * @package zin
  8. * @version $Id
  9. * @link https://www.zentao.net
  10. */
  11. namespace zin;
  12. require_once __DIR__ . DS . 'zin.class.php';
  13. require_once __DIR__ . DS . 'context.func.php';
  14. use zin\node;
  15. interface iDirective
  16. {
  17. /**
  18. * @param \zin\node $node
  19. * @param string $blockName
  20. */
  21. public function apply($node, $blockName);
  22. }
  23. class directive implements iDirective
  24. {
  25. /**
  26. * @var string
  27. */
  28. public $type;
  29. /**
  30. * @var mixed
  31. */
  32. public $data;
  33. /**
  34. * @var mixed[]|null
  35. */
  36. public $options;
  37. /**
  38. * @var mixed
  39. */
  40. public $parent = null;
  41. /**
  42. * @var bool
  43. */
  44. public $notRenderInGlobal;
  45. /**
  46. * Construct a directive object
  47. * @param string $type
  48. * @param mixed $data
  49. * @param array $options
  50. * @access public
  51. */
  52. public function __construct($type, $data = null, $options = null)
  53. {
  54. $this->type = $type;
  55. $this->data = $data;
  56. $this->options = $options;
  57. if(!$options || !isset($options['notRenderInGlobal']) || !$options['notRenderInGlobal'])
  58. {
  59. renderInGlobal($this);
  60. }
  61. }
  62. public function __debugInfo()
  63. {
  64. return array(
  65. 'type' => $this->type,
  66. 'data' => $this->data,
  67. 'options' => $this->options
  68. );
  69. }
  70. /**
  71. * @param \zin\node $node
  72. * @param string $blockName
  73. */
  74. public function apply($node, $blockName)
  75. {
  76. $this->parent = $node;
  77. $data = $this->data;
  78. $type = $this->type;
  79. if($type === 'prop')
  80. {
  81. $node->setProp($data);
  82. return;
  83. }
  84. if($type === 'class' || $type === 'style')
  85. {
  86. $node->setProp($type, $data);
  87. return;
  88. }
  89. if($type === 'cssVar')
  90. {
  91. $node->setProp('--', $data);
  92. return;
  93. }
  94. if($type === 'html')
  95. {
  96. $html = new stdClass();
  97. $html->html = implode("\n", $data);
  98. $node->addToBlock($blockName, $html);
  99. return;
  100. }
  101. if($type === 'text')
  102. {
  103. $node->addToBlock($blockName, $data);
  104. return;
  105. }
  106. if($type === 'block')
  107. {
  108. foreach($data as $blockName => $blockChildren)
  109. {
  110. $node->addToBlock($blockName, $blockChildren);
  111. }
  112. }
  113. }
  114. /**
  115. * @param mixed $item
  116. */
  117. public static function is($item)
  118. {
  119. return ($item instanceof directive) || $item instanceof iDirective || (is_object($item) && method_exists($item, 'apply'));
  120. }
  121. }
  122. function directive($type, $data, $options = null)
  123. {
  124. return new directive($type, $data, $options);
  125. }
  126. /**
  127. * @param mixed $item
  128. */
  129. function isDirective($item, $type = null)
  130. {
  131. return directive::is($item);
  132. }