jscontext.class.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * The js context class file of zin lib.
  4. *
  5. * @copyright Copyright 2024 青岛易软天创网络科技有限公司(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 . 'js.class.php';
  13. /**
  14. * Class for generating js code with context.
  15. */
  16. class jsContext extends js
  17. {
  18. /**
  19. * @var string|null
  20. */
  21. protected $contextName;
  22. /**
  23. * @param null|string|bool $name
  24. * @param string|mixed[]|\zin\js|null ...$codes
  25. * @param mixed $value
  26. */
  27. public function __construct($value, $name = null, ...$codes)
  28. {
  29. parent::__construct(...$codes);
  30. if($name === false)
  31. {
  32. $this->contextName = $value;
  33. }
  34. else
  35. {
  36. if(!is_string($name)) $name = '__C' . (static::$tempIndex++);
  37. $this->contextName = $name;
  38. $this->const($name, jsRaw($value));
  39. }
  40. }
  41. public function __call($name, $arguments)
  42. {
  43. if(str_ends_with($name, 'If'))
  44. {
  45. return parent::__call($name, $arguments);
  46. }
  47. return $this->call($name, ...$arguments);
  48. }
  49. /**
  50. * @param mixed ...$args
  51. * @return $this
  52. * @param string $func
  53. */
  54. public function call($func, ...$args)
  55. {
  56. return parent::call($this->contextName . '.' . $func, ...$args);
  57. }
  58. /**
  59. * @param mixed ...$args
  60. */
  61. public function callSelf(...$args)
  62. {
  63. return parent::call($this->contextName, ...$args);
  64. }
  65. /**
  66. * @param string|mixed[]|\zin\js|null ...$codes
  67. * @return $this
  68. */
  69. public function do(...$codes)
  70. {
  71. return $this->with($this->contextName, ...$codes);
  72. }
  73. /**
  74. * @var int
  75. */
  76. public static $tempIndex = 0;
  77. }
  78. /**
  79. * @param string|mixed[]|null $codes
  80. * @param mixed $value
  81. */
  82. function jsContext($value, $name = null, $codes = null)
  83. {
  84. return new jsContext($value, $name, $codes);
  85. }