Import.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php namespace Dever;
  2. use Dever;
  3. class Import
  4. {
  5. public $class;
  6. public $path;
  7. public function __construct($class, $app, $path)
  8. {
  9. $this->class = false;
  10. $this->path = $path;
  11. $project = Project::load($app);
  12. if (strpos($project['path'], 'http') === 0) {
  13. $this->class = $project;
  14. } else {
  15. $this->class = Library::load($class, $app, $path);
  16. }
  17. }
  18. public function __call($method, $param)
  19. {
  20. return $this->loadDevelop($method, $param);
  21. }
  22. public function loadDevelop($method, $param)
  23. {
  24. if (is_array($this->class)) {
  25. return $this->loadServer();
  26. }
  27. if ($this->path == 'api') {
  28. if (method_exists($this->class, $method . '_secure')) {
  29. $key = false;
  30. $token = $method . '_token';
  31. if (method_exists($this->class, $token)) {
  32. $key = $this->class->{$token}();
  33. }
  34. \Dever\Helper\Secure::check($param, 300, $key);
  35. }
  36. if ($param && is_array($param) && !isset($param[0])) {
  37. $reflectionMethod = new \ReflectionMethod($this->class, $method);
  38. $data = $reflectionMethod->getParameters();
  39. $result = array();
  40. foreach ($data as $k => $v) {
  41. $name = $v->name;
  42. if (isset($param[$name])) {
  43. $result[] = $param[$name];
  44. }
  45. }
  46. $param = $result;
  47. } else {
  48. if (!is_array($param)) {
  49. $param = array($param);
  50. }
  51. }
  52. }
  53. return $this->loadDevelopCommit($method, $param);
  54. }
  55. private function loadDevelopCommit($method, $param)
  56. {
  57. if (method_exists($this->class, $method . '_cmd') && DEVER_PROTO != 'cmd') {
  58. Dever::error('route error');
  59. }
  60. if (method_exists($this->class, $method . '_commit') && Dever::$commit) {
  61. $db = Dever::store();
  62. try {
  63. Dever::$commit = false;
  64. $db->begin();
  65. $data = $this->loadDevelopMethod($method, $param);
  66. $db->commit();
  67. return $data;
  68. } catch (\Exception $e) {
  69. $db->rollBack();
  70. $data = $e->getTrace();
  71. Debug::trace($data);
  72. throw new \Exception(json_encode($data));
  73. }
  74. } else {
  75. return $this->loadDevelopMethod($method, $param);
  76. }
  77. }
  78. private function loadServer()
  79. {
  80. return 'error';
  81. }
  82. private function loadDevelopMethod($method, $param)
  83. {
  84. $data = $this->class->$method(...$param);
  85. Debug::lib($this->class, $method);
  86. Dever::reset();
  87. return $data;
  88. }
  89. }