App.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php namespace Dever;
  2. use Dever;
  3. class App
  4. {
  5. public $app = '';
  6. public $name = '';
  7. public $class = null;
  8. public function __initialize($class)
  9. {
  10. if ($class && empty($this->class)) {
  11. if (strpos($class, '/')) {
  12. $temp = explode('/', $class, 2);
  13. if (empty($temp[1])) {
  14. Dever::error($class . ' error');
  15. }
  16. [$this->app, $this->name] = $temp;
  17. $class = strtr($class, '/', '\\');
  18. } else {
  19. $temp = explode('\\', $class, 2);
  20. if (empty($temp[1])) {
  21. Dever::error($class . ' error');
  22. }
  23. [$this->app, $this->name] = $temp;
  24. $this->name = strtr($this->name, '\\', '/');
  25. }
  26. $project = Dever::project($this->app);
  27. if ($project) {
  28. if (strpos($project['path'], 'http') === 0) {
  29. $this->class = $project;
  30. } else {
  31. if (strpos($this->name, 'Manage') === 0) {
  32. $path = 'manage';
  33. Dever::load(\Manage\Lib\Auth::class);
  34. $this->name = str_replace('Manage/', '', $this->name);
  35. } else {
  36. $path = 'app';
  37. }
  38. require_once $project['path'] . $path . DIRECTORY_SEPARATOR . $this->name . '.php';
  39. $this->class = Dever::get($class);
  40. }
  41. }
  42. }
  43. return $this;
  44. }
  45. public function __call($method, $param)
  46. {
  47. return $this->loadDevelop($method, $param);
  48. }
  49. public function loadDevelop($method, $param)
  50. {
  51. if (is_array($this->class)) {
  52. return $this->loadServer();
  53. }
  54. if (!method_exists($this->class, $method)) {
  55. Dever::error($method . ' error');
  56. }
  57. if (strpos($this->name, 'Api') === 0) {
  58. if (method_exists($this->class, $method . '_secure')) {
  59. $key = false;
  60. $token = $method . '_token';
  61. if (method_exists($this->class, $token)) {
  62. $key = $this->class->{$token}();
  63. }
  64. \Dever\Helper\Secure::check($param, 300, $key);
  65. }
  66. if ($param && is_array($param) && !isset($param[0])) {
  67. $reflectionMethod = new \ReflectionMethod($this->class, $method);
  68. $data = $reflectionMethod->getParameters();
  69. $result = [];
  70. foreach ($data as $k => $v) {
  71. $name = $v->name;
  72. if (isset($param[$name])) {
  73. $result[] = $param[$name];
  74. }
  75. }
  76. $param = $result;
  77. } else {
  78. if (!is_array($param)) {
  79. $param = [$param];
  80. }
  81. }
  82. }
  83. return $this->loadDevelopCommit($method, $param);
  84. }
  85. private function loadDevelopCommit($method, $param)
  86. {
  87. if (method_exists($this->class, $method . '_cmd') && DEVER_PROTO != 'cmd') {
  88. Dever::error('route error');
  89. }
  90. if (method_exists($this->class, $method . '_commit') && Dever::getCommit()) {
  91. $db = end(\Dever\Store\Pdo::$instance);
  92. if (!$db) {
  93. $db = Dever::store();
  94. }
  95. try {
  96. Dever::setCommit();
  97. $db->begin();
  98. $data = $this->loadDevelopMethod($method, $param);
  99. $db->commit();
  100. return $data;
  101. } catch (\Exception $e) {
  102. $db->rollback();
  103. $data = $e->getTrace();
  104. Dever::get(Debug::class)->trace($data);
  105. throw new \Exception(json_encode($data));
  106. }
  107. } else {
  108. return $this->loadDevelopMethod($method, $param);
  109. }
  110. }
  111. private function loadServer()
  112. {
  113. return 'error';
  114. }
  115. private function loadDevelopMethod($method, $param)
  116. {
  117. $data = $this->class->$method(...$param);
  118. Dever::get(Debug::class)->lib($this->class, $method);
  119. return $data;
  120. }
  121. }