PageRenderTime 82ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/source/class/discuz/discuz_container.php

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