rabin 2 dni temu
rodzic
commit
412764fa7b
1 zmienionych plików z 168 dodań i 84 usunięć
  1. 168 84
      boot.php

+ 168 - 84
boot.php

@@ -1,4 +1,5 @@
 <?php
+ini_set('display_errors', true);
 header('Content-Type: text/html; charset=utf-8');date_default_timezone_set("PRC");define('DEVER_TIME', $_SERVER['REQUEST_TIME']);define('DEVER_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
 if (defined('DEVER_SERVER')) {
     Dever::server();
@@ -7,8 +8,15 @@ if (defined('DEVER_SERVER')) {
 }
 class Dever
 {
-    public static $data = [];
-    public static $commit = true;
+    protected static $instances = [];
+    protected static $requestId = null;
+    protected static $requestInstances = [];
+    protected static $counter = 0;
+    protected static $reflectionCache = [];
+    protected static $bindings = [];
+    protected static $commit = [];
+    protected static $data = [];
+
     public static function server()
     {
         spl_autoload_register(['Dever', 'autoload']);
@@ -37,7 +45,7 @@ class Dever
                 // 返回给客户端
                 $connection->send($output);
             } catch (\Throwable $e) {
-                $connection->send(Dever\Output::error($e->getMessage()));
+                $connection->send(self::out()->error($e->getMessage()));
                 Dever::log('server_error', $e->getMessage());
             }
         };*/
@@ -46,10 +54,12 @@ class Dever
     }
     public static function run()
     {
+        self::beginRequest();
         spl_autoload_register(['Dever', 'autoload']);
-        $route = Dever\Route::get();
-        Dever\Debug::init();
-        Dever\Project::register();
+        $route = self::get(Dever\Route::class)->get();
+        $out = self::out();
+        self::get(Dever\Debug::class)->init();
+        self::get(Dever\Project::class)->register();
         if (isset(self::config('setting')['cache'])) {
             $index = DEVER_APP_NAME . DIRECTORY_SEPARATOR . $route['l'];
             if (isset(self::config('setting')['cache'][$index])) {
@@ -59,32 +69,135 @@ class Dever
                 }
                 $key = md5(DEVER_APP_NAME . http_build_query($route));
                 if ($result = self::cache($key)) {
-                    return Dever\Output::success($result);
+                    return self::out()->success($result);
                 }
             }
         }
         if ($route['l'] && strpos($route['l'], '.')) {
             list($class, $method) = explode('.', $route['l']);
-            if (strstr($class, 'manage/') && !strstr($class, 'api')) {
-                Dever\Output::error('route error');
+            $class = strtr(ucwords(strtr($class, '/', ' ')), ' ', '\\');
+            if (strpos($class, 'Manage') === 0) {
+                $class = str_replace('Manage\\', '', $class);
+                $class = DEVER_APP_NAME . '\\Manage\\Api\\' . $class;
+            } else {
+                $class = DEVER_APP_NAME . '\\Api\\' . $class;
             }
-            $result = Dever\Output::success(self::load($class, DEVER_APP_NAME, 'api')->loadDevelop($method, Dever\Route::input(), true));
+            $result = self::out()->success(self::load($class)->loadDevelop($method, self::input(), true));
         } else {
-            $result = Dever\Output::success('ok');
+            $result = self::out()->success('ok');
         }
         if (isset($expire)) {
             self::cache($key, $result, $expire);
         }
     }
+
+    protected static function beginRequest()
+    {
+        self::$counter++;
+        self::$requestId = 'req_' . getmypid() . '_' . self::$counter . '_' . microtime(true);
+        self::$requestInstances[self::$requestId] = [];
+    }
+    public static function endRequest()
+    {
+        if (self::$requestId && isset(self::$requestInstances[self::$requestId])) {
+            unset(self::$requestInstances[self::$requestId]);
+        }
+        self::$requestId = null;
+    }
+    public static function getCommit()
+    {
+        return empty(self::$commit[self::$requestId]);
+    }
+    public static function setCommit()
+    {
+        self::$commit[self::$requestId] = true;
+    }
+    public static function setData($key, $data)
+    {
+        return self::$data[self::$requestId][$key] = $data;
+    }
+    public static function getData($key)
+    {
+        return self::$data[self::$requestId][$key] ?? false;
+    }
+    protected static function resolve($class)
+    {
+        if (isset(self::$bindings[$class])) {
+            $binding = self::$bindings[$class];
+            $concrete = $binding['concrete'];
+            if ($concrete instanceof \Closure) {
+                $object = $concrete();
+            } elseif (is_string($concrete)) {
+                $object = self::make($concrete);
+            } else {
+                $object = $concrete;
+            }
+            if ($binding['shared']) {
+                self::$instances[$class] = $object;
+            }
+            return $object;
+        }
+        return self::build($class);
+    }
+    protected static function build($class)
+    {
+        if (!isset(self::$reflectionCache[$class])) {
+            self::$reflectionCache[$class] = new \ReflectionClass($class);
+        }
+        $refClass = self::$reflectionCache[$class];
+        $constructor = $refClass->getConstructor();
+        if (!$constructor || $constructor->getNumberOfParameters() === 0) {
+            return new $class();
+        }
+
+        $dependencies = [];
+        foreach ($constructor->getParameters() as $param) {
+            $type = $param->getType();
+            if ($type && !$type->isBuiltin()) {
+                $dependencies[] = self::get($type->getName());
+            } elseif ($param->isDefaultValueAvailable()) {
+                $dependencies[] = $param->getDefaultValue();
+            } else {
+                throw new \Exception("无法解析 {$class} 的依赖参数 \${$param->getName()}");
+            }
+        }
+        return $refClass->newInstanceArgs($dependencies);
+    }
+    public static function bind($abstract, $concrete, $shared = false)
+    {
+        self::$bindings[$abstract] = compact('concrete', 'shared');
+    }
+    public static function get($class, $requestScope = true, $key = null)
+    {
+        if ($key === null) $key = $class;
+        if ($requestScope) {
+            if (!self::$requestId) {
+                self::beginRequest();
+            }
+            if (!isset(self::$requestInstances[self::$requestId][$key])) {
+                self::$requestInstances[self::$requestId][$key] = self::resolve($class);
+            }
+            return self::$requestInstances[self::$requestId][$key];
+        } else {
+            if (!isset(self::$instances[$key])) {
+                self::$instances[$key] = self::resolve($class);
+            }
+            return self::$instances[$key];
+        }
+    }
+    public static function make($class)
+    {
+        return self::resolve($class);
+    }
     public static function autoload($class)
     {
         if (strpos($class, 'Dever') === 0 || strpos($class, 'Workerman') === 0) {
             include DEVER_PATH . 'src/' . str_replace('\\', '/', $class) . '.php';
         } else {
-            Dever\Library::autoload($class);
+            self::load($class);
         }
     }
-    public static function call($class, $param = [], $path = '')
+    public static function call($class, $param = [])
     {
         if (!is_array($param)) $param = [$param];
         if (strpos($class, '?')) {
@@ -94,38 +207,18 @@ class Dever
                 array_unshift($param, $v);
             }
         }
-        if (strpos($class, '.')) {
-            list($class, $method) = explode('.', $class);
-            $call = 'load';
-            if (!$path) $path = 'lib';
-        } elseif (strpos($class, '-')) {
-            list($class, $method) = explode('-', $class);
-            $call = 'db';
-            if (!$path) $path = true;
-        }
-        return self::$call($class, '', $path)->$method(...$param);
+        
+        list($class, $method) = explode('.', $class);
+        $class = strtr($class, '/', '\\');
+        return self::load($class)->$method(...$param);
     }
-    public static function load($class, $app = '', $path = 'lib')
+    public static function load($class)
     {
-        if (strpos($class, '/') && !$app) {
-            list($app, $class) = explode('/', $class, 2);
-        }
-        $index = $app . $path . $class;
-        if (empty(self::$data[$index])) {
-            self::$data[$index] = new Dever\Import($class, $app, $path);
-        }
-        return self::$data[$index];
+        return self::get(Dever\App::class, false, $class)->__initialize($class);
     }
-    public static function db($table, $app = '', $cache = true, $store = 'default', $partition = false, $path = 'table')
+    public static function db($table, $store = 'default', $partition = false, $path = 'table')
     {
-        if (strpos($table, '/') && !$app) {
-            list($app, $table) = explode('/', $table);
-        }
-        $index = $app . $path . $table;
-        if (empty(self::$data[$index]) || !$cache) {
-            self::$data[$index] = new Dever\Model($table, $app, $store, $partition, $path);
-        }
-        return self::$data[$index];
+        return self::get(Dever\Model::class, false, $table)->__initialize($table, $store, $partition, $path);
     }
     public static function option($table, $type = '', $where = [])
     {
@@ -148,49 +241,52 @@ class Dever
         return $default;
     }
     # 定义常用方法,这里不用__callStatic
-    public static function config(...$args)
+    public static function input(...$args)
     {
-        return Dever\Config::get(...$args);
+        return self::get(Dever\Route::class)->input(...$args);
     }
-    public static function apply(...$args)
+    public static function url(...$args)
     {
-        return Dever\Library::apply(...$args);
+        return self::get(Dever\Route::class)->url(...$args);
     }
-    public static function success(...$args)
+    public static function host(...$args)
     {
-        return Dever\Output::success(...$args);
+        return self::get(Dever\Route::class)->host(...$args);
     }
-    public static function error(...$args)
+    public static function debug(...$args)
     {
-        return Dever\Output::error(...$args);
+        return self::get(Dever\Debug::class)->add(...$args);
     }
-    public static function out(...$args)
+    public static function page(...$args)
     {
-        return Dever\Output::out(...$args);
+        return self::get(Dever\Paginator::class)->get(...$args);
     }
-    public static function input(...$args)
+    public static function config(...$args)
     {
-        return Dever\Route::input(...$args);
+        return self::get(Dever\Config::class)->get(...$args);
     }
-    public static function url(...$args)
+    public static function project($app)
     {
-        return Dever\Route::url(...$args);
+        return self::get(Dever\Project::class)->load($app);
     }
-    public static function host(...$args)
+    public static function log(...$args)
     {
-        return Dever\Route::host(...$args);
+        return self::get(Dever\Log::class)->add(...$args);
     }
-    public static function project(...$args)
+
+    public static function out()
     {
-        return Dever\Project::load(...$args);
+        return self::get(Dever\Output::class);
     }
-    public static function log(...$args)
+    public static function error(...$args)
     {
-        return Dever\Log::add(...$args);
+        return self::out()->error(...$args);
     }
-    public static function debug(...$args)
+    public static function apply($file)
     {
-        return Dever\Debug::add(...$args);
+        [$app, $file] = explode('/', $file, 2);
+        $project = Dever::project($app);
+        require_once $project['path'] . $file . '.php';
     }
     public static function session(...$args)
     {
@@ -198,20 +294,17 @@ class Dever
     }
     public static function view(...$args)
     {
-        return Dever\View::show(...$args);
+        return self::get(Dever\View::class)->show(...$args);
     }
     public static function file(...$args)
     {
-        return Dever\File::get(...$args);
+        return self::get(Dever\File::class)->get(...$args);
     }
     public static function data()
     {
-        return Dever\File::data();
-    }
-    public static function page(...$args)
-    {
-        return Dever\Paginator::get(...$args);
+        return self::get(Dever\File::class)->data();
     }
+    
     public static function rule(...$args)
     {
         return Dever\Helper\Rule::get(...$args);
@@ -228,8 +321,7 @@ class Dever
     }
     public static function curl(...$args)
     {
-        $curl = new Dever\Helper\Curl();
-        return $curl->load(...$args);
+        return self::get(Dever\Helper\Curl::class)->load(...$args);
     }
     public static function cache($key, $value = false)
     {
@@ -247,11 +339,11 @@ class Dever
     }
     public static function shell($value)
     {
-        return Dever::check(Dever\Route::input('shell'), $value);
+        return self::check(self::input('shell'), $value);
     }
     public static function store($store = 'default', $partition = false)
     {
-        $setting = Dever\Config::get('setting')['database'][$store];
+        $setting = self::config('setting')['database'][$store];
         $class = 'Dever\\Store\\' . $setting['type'];
         return $class::getInstance($store, $setting, $partition);
     }
@@ -281,19 +373,11 @@ class Dever
         if (is_array($var)) {
             $var = implode(',', $var);
         }
-        $var = ',' . $var . ',';
-        $find = ',' . $find . ',';
-        return strpos($var, $find) !== false;
+        return strpos(',' . $var . ',', ',' . $find . ',') !== false;
     }
     public static function json_encode($value)
     {
-        //$value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK | JSON_PRESERVE_ZERO_FRACTION);
-        $value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
-        /*
-        if (strpos($value, '&lt;')) {
-            $value = Dever\Helper\Secure::xss($value);
-        }*/
-        return $value;
+        return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
     }
     public static function json_decode($value)
     {