discuz_container.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Discuz! Team
  4. * This is NOT a freeware, use is subject to license terms
  5. * https://license.discuz.vip
  6. */
  7. if(!defined('IN_DISCUZ')) {
  8. exit('Access Denied');
  9. }
  10. class discuz_container extends discuz_base {
  11. protected $_obj;
  12. protected $_objs = [];
  13. public function __construct($obj = null) {
  14. if(isset($obj)) {
  15. if(is_object($obj)) {
  16. $this->_obj = $obj;
  17. } else if(is_string($obj)) {
  18. try {
  19. if(func_num_args()) {
  20. $p = func_get_args();
  21. unset($p[0]);
  22. $ref = new ReflectionClass($obj);
  23. $this->_obj = $ref->newInstanceArgs($p);
  24. unset($ref);
  25. } else {
  26. $this->_obj = new $obj;
  27. }
  28. } catch (Exception $e) {
  29. throw new Exception('Class "'.$obj.'" does not exists.');
  30. }
  31. }
  32. }
  33. parent::__construct();
  34. }
  35. public function getobj() {
  36. return $this->_obj;
  37. }
  38. public function setobj($value) {
  39. $this->_obj = $value;
  40. }
  41. public function __call($name, $p) {
  42. if(method_exists($this->_obj, $name)) {
  43. if(isset($this->_obj->methods[$name][0])) {
  44. $this->_call($name, $p, 0);
  45. }
  46. $this->_obj->data = match (count($p)) {
  47. 0 => $this->_obj->{$name}(),
  48. 1 => $this->_obj->{$name}($p[0]),
  49. 2 => $this->_obj->{$name}($p[0], $p[1]),
  50. 3 => $this->_obj->{$name}($p[0], $p[1], $p[2]),
  51. 4 => $this->_obj->{$name}($p[0], $p[1], $p[2], $p[3]),
  52. 5 => $this->_obj->{$name}($p[0], $p[1], $p[2], $p[3], $p[4]),
  53. default => call_user_func_array([$this->_obj, $name], $p),
  54. };
  55. if(isset($this->_obj->methods[$name][1])) {
  56. $this->_call($name, $p, 1);
  57. }
  58. return $this->_obj->data;
  59. } else {
  60. throw new Exception('Class "'.get_class($this->_obj).'" does not have a method named "'.$name.'".');
  61. }
  62. }
  63. protected function _call($name, $p, $type) {
  64. $ret = null;
  65. if(isset($this->_obj->methods[$name][$type])) {
  66. foreach($this->_obj->methods[$name][$type] as $extend) {
  67. if(is_array($extend) && isset($extend['class'])) {
  68. $obj = $this->_getobj($extend['class'], $this->_obj);
  69. $ret = match (count($p)) {
  70. 0 => $obj->{$extend['method']}(),
  71. 1 => $obj->{$extend['method']}($p[0]),
  72. 2 => $obj->{$extend['method']}($p[0], $p[1]),
  73. 3 => $obj->{$extend['method']}($p[0], $p[1], $p[2]),
  74. 4 => $obj->{$extend['method']}($p[0], $p[1], $p[2], $p[3]),
  75. 5 => $obj->{$extend['method']}($p[0], $p[1], $p[2], $p[3], $p[4]),
  76. default => call_user_func_array([$obj, $extend['method']], $p),
  77. };
  78. } elseif(is_callable($extend, true)) {
  79. if(is_array($extend)) {
  80. list($obj, $method) = $extend;
  81. if(method_exists($obj, $method)) {
  82. if(is_object($obj)) {
  83. $obj->obj = $this->_obj;
  84. $ret = match (count($p)) {
  85. 0 => $obj->{$method}(),
  86. 1 => $obj->{$method}($p[0]),
  87. 2 => $obj->{$method}($p[0], $p[1]),
  88. 3 => $obj->{$method}($p[0], $p[1], $p[2]),
  89. 4 => $obj->{$method}($p[0], $p[1], $p[2], $p[3]),
  90. 5 => $obj->{$method}($p[0], $p[1], $p[2], $p[3], $p[4]),
  91. default => call_user_func_array([$obj, $method], $p),
  92. };
  93. } else {
  94. $p[] = $this;
  95. $ret = call_user_func_array($extend, $p);
  96. }
  97. }/* else {
  98. throw new Exception('Class "'.get_class($extend[0]).'" does not have a method named "'.$extend[1].'".');
  99. }*/
  100. } else {
  101. $p[] = $this->_obj;
  102. $ret = call_user_func_array($extend, $p);
  103. }
  104. }
  105. }
  106. }
  107. return $ret;
  108. }
  109. protected function _getobj($class, $obj) {
  110. if(!isset($this->_objs[$class])) {
  111. $this->_objs[$class] = new $class($obj);
  112. if(method_exists($this->_objs[$class], 'init_base_var')) {
  113. $this->_objs[$class]->init_base_var();
  114. }
  115. }
  116. return $this->_objs[$class];
  117. }
  118. public function __get($name) {
  119. if(isset($this->_obj) && property_exists($this->_obj, $name) === true) {
  120. return $this->_obj->$name;
  121. } else {
  122. return parent::__get($name);
  123. }
  124. }
  125. public function __set($name, $value) {
  126. if(isset($this->_obj) && property_exists($this->_obj, $name) === true) {
  127. return $this->_obj->$name = $value;
  128. } else {
  129. return parent::__set($name, $value);
  130. }
  131. }
  132. public function __isset($name) {
  133. return isset($this->_obj->$name);
  134. }
  135. }