1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php namespace Dever;
- use Dever;
- class Import
- {
- public $class;
- public $path;
- public function __construct($class, $app, $path)
- {
- $this->class = false;
- $this->path = $path;
- $project = Project::load($app);
- if (strpos($project['path'], 'http') === 0) {
- $this->class = $project;
- } else {
- $this->class = Library::load($class, $app, $path);
- }
- }
- public function __call($method, $param)
- {
- return $this->loadDevelop($method, $param);
- }
- public function loadDevelop($method, $param)
- {
- if (is_array($this->class)) {
- return $this->loadServer();
- }
- if ($this->path == 'api') {
- if (method_exists($this->class, $method . '_secure')) {
- $key = false;
- $token = $method . '_token';
- if (method_exists($this->class, $token)) {
- $key = $this->class->{$token}();
- }
- \Dever\Helper\Secure::check($param, 300, $key);
- }
- if ($param && is_array($param) && !isset($param[0])) {
- $reflectionMethod = new \ReflectionMethod($this->class, $method);
- $data = $reflectionMethod->getParameters();
- $result = array();
- foreach ($data as $k => $v) {
- $name = $v->name;
- if (isset($param[$name])) {
- $result[] = $param[$name];
- }
- }
- $param = $result;
- } else {
- if (!is_array($param)) {
- $param = array($param);
- }
- }
- }
- return $this->loadDevelopCommit($method, $param);
- }
- private function loadDevelopCommit($method, $param)
- {
- if (method_exists($this->class, $method . '_cmd') && DEVER_PROTO != 'cmd') {
- Dever::error('route error');
- }
- if (method_exists($this->class, $method . '_commit') && Dever::$commit) {
- $db = Dever::store();
- try {
- Dever::$commit = false;
- $db->begin();
- $data = $this->loadDevelopMethod($method, $param);
- $db->commit();
- return $data;
- } catch (\Exception $e) {
- $db->rollBack();
- $data = $e->getTrace();
- Debug::trace($data);
- throw new \Exception(json_encode($data));
- }
- } else {
- return $this->loadDevelopMethod($method, $param);
- }
- }
- private function loadServer()
- {
- return 'error';
- }
- private function loadDevelopMethod($method, $param)
- {
- $data = $this->class->$method(...$param);
- Debug::lib($this->class, $method);
- Dever::reset();
- return $data;
- }
- }
|