rabin преди 1 година
родител
ревизия
f7f5633a6a
променени са 7 файла, в които са добавени 73 реда и са изтрити 67 реда
  1. 5 3
      boot.php
  2. 1 1
      src/Dever/Helper/Curl.php
  3. 40 57
      src/Dever/Import.php
  4. 8 5
      src/Dever/Model.php
  5. 1 1
      src/Dever/Session.php
  6. 1 0
      src/Dever/Store/Influxdb.php
  7. 17 0
      src/Dever/Store/Pdo.php

+ 5 - 3
boot.php

@@ -5,6 +5,7 @@ class Dever
 {
     public static $data = array();
     public static $app = array();
+    public static $commit = true;
     public static function run()
     {
         spl_autoload_register(array('Dever', 'autoload'));
@@ -25,8 +26,9 @@ class Dever
             }
         }
         self::$app[] = DEVER_APP_NAME;
-        if ($route['l']) {
-            $result = Dever\Output::success(self::load($route['l'], DEVER_APP_NAME, 'api'));
+        if ($route['l'] && strpos($route['l'], '.')) {
+            list($class, $method) = explode('.', $route['l']);
+            $result = Dever\Output::success(self::load($class, DEVER_APP_NAME, 'api')->loadDevelop($method, Dever\Route::input(), true));
         } else {
             $result = Dever\Output::success('ok');
         }
@@ -69,7 +71,7 @@ class Dever
         if (empty(self::$data[$index])) {
             self::$data[$index] = new Dever\Import($class, $app, $path);
         }
-        return self::$data[$index]->loadDevelop();
+        return self::$data[$index];
     }
     public static function db($table, $app = '', $store = 'default', $partition = false, $path = 'table')
     {

+ 1 - 1
src/Dever/Helper/Curl.php

@@ -5,7 +5,7 @@ class Curl
     private $handle;
     private $url;
     private $get_info = false;
-    private $log = true;
+    private $log = false;
     private $result_header = false;
     private $param = array();
     private $header = array();

+ 40 - 57
src/Dever/Import.php

@@ -2,18 +2,12 @@
 use Dever;
 class Import
 {
-    protected $param;
     protected $class;
-    protected $method;
     protected $path;
     public function __construct($class, $app, $path)
     {
         $this->class = false;
-        $this->method = '';
         $this->path = $path;
-        if (strpos($class, '.')) {
-            list($class, $this->method) = explode('.', $class);
-        }
         $project = Project::load($app);
         if (strpos($project['path'], 'http') === 0) {
             $this->class = $project;
@@ -23,81 +17,70 @@ class Import
     }
     public function __call($method, $param)
     {
-        $this->method = $method;
-        $this->param = $param;
-        return $this->loadDevelop();
+        return $this->loadDevelop($method, $param);
     }
-    public function loadDevelop()
+    public function loadDevelop($method, $param)
     {
-        if ($this->method) {
-            if (is_array($this->class)) {
-                return $this->loadDevelopServer();
+        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 ($this->path == 'api') {
-                if (!$this->param) $this->param = Route::input();
-                if (method_exists($this->class, $this->method . '_secure')) {
-                    $key = false;
-                    $token = $this->method . '_token';
-                    if (method_exists($this->class, $token)) {
-                        $key = $this->class->{$token}();
+            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];
                     }
-                    \Dever\Helper\Secure::check($this->param, 300, $key);
+                }
+                $param = $result;
+            } else {
+                if (!is_array($param)) {
+                    $param = array($param);
                 }
             }
-            return $this->loadDevelopCommit();
         }
-        return $this;
+        return $this->loadDevelopCommit($method, $param);
     }
-    private function loadDevelopServer()
+    private function loadDevelopCommit($method, $param)
     {
-        return 'error';
-    }
-    private function loadDevelopCommit()
-    {
-        if (method_exists($this->class, $this->method . '_commit')) {
+        if (method_exists($this->class, $method . '_commit') && Dever::$commit) {
             $db = Dever::store();
             try {
+                Dever::$commit = false;
                 $db->begin();
-                $data = $this->loadDevelopCall();
+                $data = $this->loadDevelopMethod($method, $param);
                 $db->commit();
                 return $data;
             } catch (\Exception $e) {
                 $db->rollBack();
                 $data = $e->getTrace();
                 Debug::trace($data);
-                throw new Exceptions(json_encode($data));
+                throw new \Exception(json_encode($data));
             }
         } else {
-            return $this->loadDevelopCall();
+            return $this->loadDevelopMethod($method, $param);
         }
     }
-    private function loadDevelopCall()
+    private function loadServer()
     {
-        $param = $this->loadDevelopParam();
-        $data = $this->class->{$this->method}(...$param);
-        Debug::lib($this->class, $this->method);
-        Dever::reset();
-        $this->method = '';
-        return $data;
+        return 'error';
     }
-    private function loadDevelopParam()
+    private function loadDevelopMethod($method, $param)
     {
-        if ($this->param && is_array($this->param) && !isset($this->param[0])) {
-            $reflectionMethod = new \ReflectionMethod($this->class, $this->method);
-            $param = $reflectionMethod->getParameters();
-            $result = array();
-            foreach ($param as $k => $v) {
-                $name = $v->name;
-                if (isset($this->param[$name])) {
-                    $result[] = $this->param[$name];
-                }
-            }
-            return $result;
-        } else {
-            if (!is_array($this->param)) {
-                $this->param = array($this->param);
-            }
-            return $this->param;
-        }
+        $data = $this->class->$method(...$param);
+        Debug::lib($this->class, $method);
+        Dever::reset();
+        return $data;
     }
 }

+ 8 - 5
src/Dever/Model.php

@@ -40,24 +40,27 @@ class Model
     }
     public function partition($partition)
     {
+        $setting = Dever::config('setting')['database']['partition'];
         if (is_array($partition)) {
             $this->partition = $partition;
+        } elseif (is_string($partition)) {
+            $e = '$v=' . $partition . ';';
+            eval($e);
+            $this->partition = $v;
         } else {
-            $this->partition = Dever::config('setting')['database']['partition'] ?? false;
+            $this->partition = $setting;
         }
         if ($this->partition) {
             foreach ($this->partition as $k => &$v) {
                 $t = Dever::session($k);
                 if ($t) {
                     $v = $t;
-                } elseif ($k == 'database' || $k == 'table') {
+                } elseif (($k == 'database' || $k == 'table') && strstr($v, '(')) {
                     $e = '$v=' . $v . ';';
                     eval($e);
                 }
             }
-            if (empty($this->partition['create'])) {
-                $this->partition['create'] = true;
-            }
+            $this->partition['create'] = $setting['create'];
         }
     }
     private function lang()

+ 1 - 1
src/Dever/Session.php

@@ -32,7 +32,7 @@ class Session
             self::$start = true;
         }
     }
-    public static function oper($key, $value = false, $timeout = 3600, $type = 'session', $encode = true)
+    public static function oper($key, $value = false, $timeout = 86400, $type = 'session', $encode = true)
     {
         if (empty(self::$save[$key])) {
             self::$save[$key] = new self(DEVER_APP_NAME, $type);

+ 1 - 0
src/Dever/Store/Influxdb.php

@@ -162,4 +162,5 @@ class Influxdb extends Base
     public function begin(){}
     public function commit(){}
     public function rollback(){}
+    public function transaction(){}
 }

+ 17 - 0
src/Dever/Store/Pdo.php

@@ -154,4 +154,21 @@ class Pdo extends Base
     {
         $this->update->rollback();
     }
+    public function transaction($class, $param, $msg)
+    {
+        if (\Dever::$commit) {
+            try {
+                \Dever::$commit = false;
+                $this->begin();
+                $result = call_user_func_array($class, $param);
+                $this->commit();
+                return $result;
+            } catch (\Exception $e) {
+                $this->rollBack();
+                Output::error($msg);
+            }
+        } else {
+            return call_user_func_array($class, $param);
+        }
+    }
 }