123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php namespace Dever;
- use Dever;
- class App
- {
- public $app = '';
- public $name = '';
- public $class = null;
- public function __initialize($class)
- {
- if ($class && empty($this->class)) {
- if (strpos($class, '/')) {
- $temp = explode('/', $class, 2);
- if (empty($temp[1])) {
- Dever::error($class . ' error');
- }
- [$this->app, $this->name] = $temp;
- $class = strtr($class, '/', '\\');
- } else {
- $temp = explode('\\', $class, 2);
- if (empty($temp[1])) {
- Dever::error($class . ' error');
- }
- [$this->app, $this->name] = $temp;
- $this->name = strtr($this->name, '\\', '/');
- }
- $project = Dever::project($this->app);
- if ($project) {
- if (strpos($project['path'], 'http') === 0) {
- $this->class = $project;
- } else {
- if (strpos($this->name, 'Manage') === 0) {
- $path = 'manage';
- Dever::load(\Manage\Lib\Auth::class);
- $this->name = str_replace('Manage/', '', $this->name);
- } else {
- $path = 'app';
- }
- require_once $project['path'] . $path . DIRECTORY_SEPARATOR . $this->name . '.php';
-
- $this->class = Dever::get($class);
- }
- }
- }
- return $this;
- }
- public function __call($method, $param)
- {
- return $this->loadDevelop($method, $param);
- }
- public function loadDevelop($method, $param)
- {
- if (is_array($this->class)) {
- return $this->loadServer();
- }
- if (!method_exists($this->class, $method)) {
- Dever::error($method . ' error');
- }
- if (strpos($this->name, 'Api') === 0) {
- 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 = [];
- foreach ($data as $k => $v) {
- $name = $v->name;
- if (isset($param[$name])) {
- $result[] = $param[$name];
- }
- }
- $param = $result;
- } else {
- if (!is_array($param)) {
- $param = [$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::getCommit()) {
- $db = end(\Dever\Store\Pdo::$instance);
- if (!$db) {
- $db = Dever::store();
- }
- try {
- Dever::setCommit();
- $db->begin();
- $data = $this->loadDevelopMethod($method, $param);
- $db->commit();
- return $data;
- } catch (\Exception $e) {
- $db->rollback();
- $data = $e->getTrace();
- Dever::get(Debug::class)->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);
- Dever::get(Debug::class)->lib($this->class, $method);
- return $data;
- }
- }
|