diff --git a/application/home/config/config.php b/application/home/config/config.php index 66e6fd5..b03a47b 100644 --- a/application/home/config/config.php +++ b/application/home/config/config.php @@ -1,12 +1,12 @@ [ - 'Twig' => \framework\library\template\Twig::class, - // 'Smarty' => \framework\library\template\Smarty::class, - // 'Top' => \framework\library\template\Top::class, + 'Twig' => top\library\template\Twig::class, + // 'Smarty' => top\library\template\Smarty::class, + // 'Top' => top\library\template\Top::class, ], 'decorator' => [ - application\home\decorator\Log::class + app\home\decorator\Log::class ], 'session' => [ 'open' => true, @@ -16,9 +16,9 @@ return [ 'driver' => 'MySQLi', 'host' => '127.0.0.1', 'user' => 'root', - 'passwd' => '888888', - 'dbname' => 'by_zh', - 'prefix' => 'ot_', + 'passwd' => 'root', + 'dbname' => 'hongzheng', + 'prefix' => 'cms_', 'charset' => 'utf8' ], 'view' => [ @@ -26,9 +26,9 @@ return [ 'ext' => 'html', 'dir' => '../application/home/view/', 'cacheDir' => './runtime/cache/application/home/', -// 'compileDir' => './runtime/compile/application/home/', -// 'left' => '{', -// 'right' => '}', -// 'cacheTime' => 5 + 'compileDir' => './runtime/compile/application/home/', + 'left' => '{', + 'right' => '}', + 'cacheTime' => 5 ], ]; \ No newline at end of file diff --git a/application/home/controller/Common.php b/application/home/controller/Common.php index 3d58a80..ec09b3e 100644 --- a/application/home/controller/Common.php +++ b/application/home/controller/Common.php @@ -1,8 +1,8 @@ where(['id' => ['>', 9]])->delete; - $db = Database::table('category'); + $model = model(Job::class); return [ 'title' => '测试模型高级操作', - // 'lists' => $model->where('id', '>', 1)->order('id', 'desc')->limit(0, 100)->all, - 'lists' => $db->where('id', '<', 5)->order('id', 'asc')->select(), + 'lists' => $model->select(), 'query' => $model->sql ]; } public function testPage() { - // return '测试页面'; return $this->fetch('', [ 'a' => '测试页面', ]); diff --git a/application/home/decorator/Log.php b/application/home/decorator/Log.php index b36c211..95b531b 100644 --- a/application/home/decorator/Log.php +++ b/application/home/decorator/Log.php @@ -1,9 +1,9 @@ get('session'); - if (!empty($sessionConfig) && $sessionConfig['open'] === true) + if (!empty($sessionConfig) && $sessionConfig['open'] === true) { session_start(); + } + // 数据库驱动 $config = Register::get('Config')->get('db'); $driver = $config['driver'] ? $config['driver'] : 'MySQLi'; Register::set('DBDriver', function () use ($driver) { - $class = '\\framework\\library\\database\\driver\\' . $driver; + $class = '\\top\\library\\database\\driver\\' . $driver; return $class::instance(); }); + // 视图文件缓存 Register::set('ViewCache', function () { return FileCache::instance(); }); + // 配置文件中配置的注册 $initRegister = Register::get('Config')->get('register'); if (!empty($initRegister)) { @@ -43,14 +48,17 @@ class InitDecorator implements DecoratorIfs { }); } } + // 注册视图 Register::set('View', function () { return View::instance(); }); + // 加载系统函数库 - require BASEDIR . '/framework/library/functions/functions.php'; + require FRAMEWORK_PATH . 'library/functions/functions.php'; + // 加载用户函数库 - $funcFile = BASEDIR . '/' . APPNS . '/' . $route->module . '/functions.php'; + $funcFile = APP_PATH . $route->module . '/functions.php'; if (file_exists($funcFile)) { require $funcFile; } diff --git a/framework/decorator/ReturnDecorator.php b/framework/decorator/ReturnDecorator.php index b902f98..e1ea3d8 100644 --- a/framework/decorator/ReturnDecorator.php +++ b/framework/decorator/ReturnDecorator.php @@ -1,9 +1,9 @@ set('top', FRAMEWORK_PATH); + $loader->set('app', APP_PATH); + $loader->register(); + // composer自动加载 - $composerLoadFile = BASEDIR . '/framework/vendor/autoload.php'; - if (file_exists($composerLoadFile)) + $composerLoadFile = FRAMEWORK_PATH . '/vendor/autoload.php'; + if (file_exists($composerLoadFile)) { require $composerLoadFile; + } + // 使用whoops美化异常输出 $whoops = new \Whoops\Run; $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler); $whoops->register(); - // if (PHP_VERSION > 5.6) + + // if (PHP_VERSION > 5.6) { // set_error_handler([new BaseError(), 'handler']); + // } // set_exception_handler([new BaseException(), 'handler']); + $routeDriver = ''; if (php_sapi_name() == 'cli') { // 命令行运行程序 @@ -43,14 +50,8 @@ class App { // 其他 } } - try { - // 实例化路由 - $route = new Router($routeDriver, $defaultAddress); - $route->handler(); - } catch (RouteException $route) { - exit($route->handler()); - } catch (DatabaseException $db) { - exit($db->handler()); - } + // 实例化路由 + $route = new Router($routeDriver, $defaultAddress); + $route->handler(); } } \ No newline at end of file diff --git a/framework/library/Config.php b/framework/library/Config.php index 197dd02..88f2b99 100644 --- a/framework/library/Config.php +++ b/framework/library/Config.php @@ -1,5 +1,5 @@ $value ]; + // 与原有的配置项合并 $this->config = array_merge($this->config, $config); } @@ -36,9 +53,9 @@ class Config { * @throws \Exception */ public function get($name = '') { - // 加载的文件名 + // 加载文件 $module = Register::get('Router')->module; - $file = BASEDIR . '/' . APPNS . '/' . $module . '/config/config.php'; + $file = APP_PATH . $module . '/config/config.php'; if (! isset(self::$files[$file])) { if (file_exists($file)) { $config = require $file; @@ -47,6 +64,7 @@ class Config { self::$files[$file] = true; } } + if (empty($this->config) || ! isset($this->config) || ! $this->config @@ -55,6 +73,7 @@ class Config { ) { return []; } + return $this->config[$name]; } diff --git a/framework/library/Controller.php b/framework/library/Controller.php index e9e381d..c364fd6 100644 --- a/framework/library/Controller.php +++ b/framework/library/Controller.php @@ -1,7 +1,6 @@ $msg, 'code' => $code, 'data' => $data, 'ext' => $ext - ]); + ]; + return json_encode($array); } - + /** * 缓存页面(具体视图驱动完成此功能) - * @param string $status + * @param bool $status + * @return $this */ public function cache($status = true) { Register::get('View')->cache($status); @@ -40,8 +41,8 @@ abstract class Controller { /** * 赋值到视图 - * @param string $name - * @param int|string|array $value + * @param $name + * @param $value */ public function param($name, $value) { Register::get('View')->param($name, $value); @@ -51,8 +52,8 @@ abstract class Controller { * 渲染视图 * @param string $file * @param array $param - * @param string $cache - * @return unknown + * @param bool $cache + * @return mixed */ public function fetch($file = '', $param = [], $cache = false) { return Register::get('View')->fetch($file, $param, $cache); @@ -68,10 +69,10 @@ abstract class Controller { /** * 显示提示页面 - * @param string $message + * @param $message * @param string $url - * @param number $sec - * @return string|\system\top\unknown + * @param int $sec + * @return false|mixed|string */ public function tips($message, $url = '', $sec = 3) { if (request()->isAjax()) { diff --git a/framework/library/Database.php b/framework/library/Database.php index f2ca539..4dc3faa 100644 --- a/framework/library/Database.php +++ b/framework/library/Database.php @@ -1,8 +1,8 @@ setDriver($driver, Register::get('Config')->get('db')); } + private function __clone() { + // TODO: Implement __clone() method. + } + /** * 指定数据库驱动 * @@ -94,7 +97,7 @@ class Database { /** * 指定多张表 * @param $effect - * @return \system\library\Database + * @return \top\library\Database */ public function effect($effect) { $this->effect = $effect; @@ -103,7 +106,7 @@ class Database { /** * @param $field - * @return \system\library\Database + * @return \top\library\Database */ public function distinct($field) { $this->distinct = $field; @@ -113,7 +116,7 @@ class Database { /** * 设置操作字段 * @param $field - * @return \system\library\Database + * @return \top\library\Database */ public function field($field) { $this->field = $field; @@ -122,7 +125,7 @@ class Database { /** * 设置条件 - * @return \system\library\Database + * @return \top\library\Database */ public function where() { $where = func_get_args(); @@ -151,7 +154,7 @@ class Database { /** * 设置排序 - * @return \system\library\Database + * @return \top\library\Database */ public function order() { $order = func_get_args(); @@ -167,7 +170,7 @@ class Database { /** * 设置记录范围 - * @return \system\library\Database + * @return \top\library\Database */ public function limit() { $limit = func_get_args(); @@ -187,7 +190,7 @@ class Database { * @param string $type * @param string $table * @param string $name - * @return \system\library\Database + * @return \top\library\Database */ public function join($type, $table, $name) { $this->join[] = [ @@ -201,7 +204,7 @@ class Database { /** * 多表关联 * @param string $on - * @return \system\library\Database + * @return \top\library\Database */ public function on($on) { $this->on[] = $on; @@ -291,17 +294,20 @@ class Database { * @return int|boolean */ public function delete($param = false) { - if (is_callable($param)) + if (is_callable($param)) { $param($this); + } $field = $this->getPk(); if (!empty($this->join)) { $this->table .= ' as this'; $field = 'this.' . $field; } - if (!is_bool($param) && !is_callable($param)) + if (!is_bool($param) && !is_callable($param)) { $this->where([$field => $param]); + } $result = self::$driver->delete($this->effect, $this->table, $this->join, $this->on, $this->where, $this->order, $this->limit); $this->_reset(); + return $result; } @@ -313,14 +319,18 @@ class Database { * @return mixed */ public function common($param, $type) { - if (is_callable($param)) + if (is_callable($param)) { $param($this); - if (!empty($this->join)) + } + if (!empty($this->join)) { $this->table .= ' as this'; - if (empty($this->field) && $param && !is_callable($param)) + } + if (empty($this->field) && $param && !is_callable($param)) { $this->field = $param; + } $result = self::$driver->common($this->table, $this->distinct, $this->field, $this->join, $this->on, $this->where, $type); $this->_reset(); + return $result; } @@ -332,8 +342,10 @@ class Database { */ public function tableDesc($table = '') { $table = ($table) ? $table : $this->table; - if (!isset(self::$tableDesc[$table])) + if (!isset(self::$tableDesc[$table])) { self::$tableDesc[$table] = self::$driver->tableDesc($table); + } + return self::$tableDesc[$table]; } @@ -382,8 +394,10 @@ class Database { $tableInfo = $this->tableDesc(); $pk = ''; foreach ($tableInfo as $value) { - if ($value['Key'] == 'PRI') + if ($value['Key'] == 'PRI') { $pk = $value['Field']; + break; + } } return $pk; } diff --git a/framework/library/Loader.php b/framework/library/Loader.php index df9de81..60a493a 100644 --- a/framework/library/Loader.php +++ b/framework/library/Loader.php @@ -1,55 +1,61 @@ prefixes[$name])) { + array_push($this->prefixes[$name], $path); + } else { + $this->prefixes[$name] = [$path]; + } + } + + protected function loadClass($class) { + // 首次,将前缀等于当前类名 + $prefix = $class; + // 从最后一个反斜杠开始分割前缀与类名 + while (($pos = strrpos($prefix, '\\')) !== false) { + // 取出当前位置反斜杠分割的前缀 + $prefix = substr($class, 0, $pos + 1); + // 取出分割出的实际类名 + $className = substr($class, $pos + 1); + // 尝试去加载文件 + $loadFile = $this->loadFile($prefix, $className); + if ($loadFile) { + return true; + } + $prefix = rtrim($prefix, '\\'); + } + // 未找到文件 + return false; + } + + protected function loadFile($prefix, $class) { + // echo $class . '
'; + $prefix = trim($prefix, '\\'); + // 如果存在此前缀 + if (isset($this->prefixes[$prefix])) { + // 遍历当前前缀下的目录 + foreach ($this->prefixes[$prefix] as $key => $value) { + // 拼接文件名 + $file = str_replace('\\', '/', $value . $class) . '.php'; + /*echo '
'; + echo $file . '
';*/ + // 如果文件存在则加载文件 if (file_exists($file)) { - // 文件存在 - self::$files[$className] = $file; require $file; - } else if (file_exists(BASEDIR . '/composer.json')) { - self::$files[$className] = $file; - } else { - return false; + return true; } } - return true; - }; - spl_autoload_register($autoload); - } - - /** - * 手动加载模型 - * @param $name - * @param string $module - * @return mixed - */ - public static function model($name, $module = '') { - (!$module) && $module = Register::get('Router')->module; - if (!isset(self::$classInstance[$module . $name])) { - $className = '\\' . APPNS . '\\' . $module . '\\model\\' . $name; - if (class_exists($className)) { - self::$classInstance[$module . $name] = new $className(); - } else { - self::$classInstance[$module . $name] = new Model($name); - } } - return self::$classInstance[$module . $name]; + return false; } } \ No newline at end of file diff --git a/framework/library/Model.php b/framework/library/Model.php index 094681a..b78e586 100644 --- a/framework/library/Model.php +++ b/framework/library/Model.php @@ -1,6 +1,6 @@ db->effect($effect); @@ -72,8 +69,8 @@ class Model { /** * 过滤重复值的字段 - * @param string|array $field - * @return \system\top\Model + * @param $field + * @return $this */ public function distinct($field) { $this->db->distinct($field); @@ -82,8 +79,8 @@ class Model { /** * 指定字段 - * @param string|array $field - * @return \system\top\Model + * @param $field + * @return $this */ public function field($field) { $this->db->field($field); @@ -92,7 +89,7 @@ class Model { /** * 查询条件 - * @return \system\top\Model + * @return $this */ public function where() { call_user_func_array([ @@ -104,7 +101,7 @@ class Model { /** * 排序 - * @return \system\top\Model + * @return $this */ public function order() { call_user_func_array([ @@ -116,7 +113,7 @@ class Model { /** * 限制 - * @return \system\top\Model + * @return $this */ public function limit() { call_user_func_array([ @@ -131,7 +128,7 @@ class Model { * @param $type * @param $table * @param $name - * @return \system\top\Model + * @return $this */ public function join($type, $table, $name) { $this->db->join($type, $table, $name); @@ -141,7 +138,7 @@ class Model { /** * 多表 * @param $on - * @return \system\top\Model + * @return $this */ public function on($on) { $this->db->on($on); @@ -462,7 +459,7 @@ class Model { * @param $data * @return bool */ - private function validateCallUserFunction($key = '', $validate, $data) { + private function validateCallUserFunction($key, $validate, $data) { $funcName = $validate[0]; $tips = end($validate); // 将第一个值赋值为将要检查的值 @@ -484,17 +481,16 @@ class Model { /** * 获取表结构 - * - * @return array + * @param $table + * @return mixed */ - public function tableDesc($table = '') { + public function tableDesc($table) { return $this->db->tableDesc($table); } /** * 获取信息 - * - * @return string|mixed + * @return string */ public function getMessage() { return $this->message; diff --git a/framework/library/Register.php b/framework/library/Register.php index a42c120..d7e5902 100644 --- a/framework/library/Register.php +++ b/framework/library/Register.php @@ -1,6 +1,6 @@ route = $route; $this->route->default = $default; $this->route->processing(); + $this->module = $this->route->module; - $this->className = $this->route->className; + $this->class = $this->route->class; $this->ctrl = $this->route->ctrl; $this->action = $this->route->action; $this->param = $this->route->param; + $this->check(); + Register::set('Router', function () { return $this->route; }); Register::set('Config', function () { - return new Config(); + return Config::instance(); }); } @@ -91,36 +94,45 @@ class Router { */ public function check() { // 检查模块是否存在 - if (!is_dir(BASEDIR . '/' . APPNS . '/' . $this->module)) + if (!is_dir(APP_PATH . $this->module)) { throw new RouteException('模块' . $this->module . '不存在'); + } // 检查控制器是否存在 - if (!class_exists($this->className)) - throw new RouteException('控制器' . $this->className . '不存在'); + if (!class_exists($this->class)) { + throw new RouteException('控制器' . $this->class . '不存在'); + } // 检查方法在控制器中是否存在 - if (!in_array($this->action, get_class_methods($this->className))) + if (!in_array($this->action, get_class_methods($this->class))) { throw new RouteException('方法' . $this->action . '在控制器' . $this->ctrl . '中不存在'); + } } /** * 调用方法并执行程序 - * @throws exception\BaseException + * @throws \Exception */ public function handler() { $userDecorators = Register::get('Config')->get('decorator'); $systemDecorators = [InitDecorator::class, ReturnDecorator::class, StringDecorator::class]; + $decorators = array_merge($systemDecorators, $userDecorators); - foreach ($decorators as $key => $value) + foreach ($decorators as $key => $value) { $this->decorator(new $value()); + } + $this->beforeRoute(); - $object = new $this->className(); - if (method_exists($object, '_init')) + + $object = new $this->class(); + if (method_exists($object, '_init')) { $data = $object->_init(); + } if (!isset($data) || $data == null) { $data = call_user_func_array([ $object, $this->action ], $this->param); } + $this->afterRoute($data); } } \ No newline at end of file diff --git a/framework/library/Template.php b/framework/library/Template.php index 4ef2f29..84f3830 100644 --- a/framework/library/Template.php +++ b/framework/library/Template.php @@ -1,7 +1,7 @@ template = $template->run(); } + private function __clone() { + // TODO: Implement __clone() method. + } + /** * 获取实例 * * @param TemplateIfs $template - * @return \system\library\Template + * @return \top\library\Template */ public static function instance($template) { if (! self::$instance) { diff --git a/framework/library/View.php b/framework/library/View.php index 9bff0f6..96978b3 100644 --- a/framework/library/View.php +++ b/framework/library/View.php @@ -1,6 +1,6 @@ template = Template::instance($driver); } + private function __clone() { + // TODO: Implement __clone() method. + } + /** * 传递参数 * @param $name diff --git a/framework/library/cache/FileCache.php b/framework/library/cache/FileCache.php index 2ea9846..12c3f08 100644 --- a/framework/library/cache/FileCache.php +++ b/framework/library/cache/FileCache.php @@ -1,7 +1,7 @@ 64 && $ord < 91 && $i != 0) - $arr[$i-1] = $arr[$i-1] . '_'; + if ($ord > 64 && $ord < 91 && $i != 0) { + $arr[$i - 1] = $arr[$i - 1] . '_'; + } } $table = implode('', $arr); return strtolower($table); @@ -69,7 +82,7 @@ function get_table_name($classname) { * @return NULL|number|string */ function get_client_ip() { - return \request()->ip(); + return request()->ip(); } /** @@ -121,12 +134,12 @@ function filter($str) { * @param $name * @param string $value * @return bool - * @throws \framework\library\exception\BaseException + * @throws Exception */ function session($name, $value = '') { - $config = \framework\library\Register::get('Config')->get('session'); + $config = \top\library\Register::get('Config')->get('session'); if (empty($config) || !$config['prefix']) { - $route = \framework\library\Register::get('Route'); + $route = \top\library\Register::get('Route'); $prefix = $route->module; } else { $prefix = $config['prefix']; diff --git a/framework/library/http/Request.php b/framework/library/http/Request.php index ed2bdf6..4eaa543 100644 --- a/framework/library/http/Request.php +++ b/framework/library/http/Request.php @@ -1,6 +1,6 @@ uriArray[0]) && $this->uriArray[0]) { @@ -58,10 +55,7 @@ class Pathinfo implements RouteIfs { /** * 控制器名 - * - * {@inheritdoc} - * - * @see \system\core\route\ifs\RouteIfs::ctrl() + * @return string */ public function ctrl() { if (isset($this->uriArray[1]) && $this->uriArray[1]) { @@ -73,10 +67,7 @@ class Pathinfo implements RouteIfs { /** * 具体执行的方法名 - * - * {@inheritdoc} - * - * @see \system\core\route\ifs\RouteIfs::action() + * @return mixed|string */ public function action() { if (isset($this->uriArray[2]) && $this->uriArray[2]) { @@ -95,8 +86,8 @@ class Pathinfo implements RouteIfs { unset($this->uriArray[1]); unset($this->uriArray[2]); $this->uriArray = array_merge($this->uriArray, []); - if (!empty($this->uriArray) && class_exists($this->className)) { - $paramName = (new \ReflectionMethod($this->className, $this->action))->getParameters(); + if (!empty($this->uriArray) && class_exists($this->class)) { + $paramName = (new \ReflectionMethod($this->class, $this->action))->getParameters(); $paramNameArray = []; for ($i = 0; $i < count($paramName); $i++) { $paramNameArray[$paramName[$i]->name] = ''; @@ -119,8 +110,7 @@ class Pathinfo implements RouteIfs { /** * 处理URI - * - * @return string + * @return mixed|string */ private function getUri() { if (isset($_SERVER['PATH_INFO'])) { @@ -133,7 +123,7 @@ class Pathinfo implements RouteIfs { $this->rawUri = $uri; $paramArray = explode('/', $uri); $name = $paramArray[0]; - $file = BASEDIR . '/' . APPNS . '/route.php'; + $file = APP_PATH . 'route.php'; if (file_exists($file)) { $routeConfig = require $file; if (isset($routeConfig[$name])) { @@ -165,7 +155,6 @@ class Pathinfo implements RouteIfs { /** * 根据URI得到带参数的数组 - * * @return array */ private function processUriArray() { @@ -174,13 +163,13 @@ class Pathinfo implements RouteIfs { /** * 返回解析出的数据 home/controller/index - * @throws \Exception + * @throws \ReflectionException */ public function processing() { $this->uriArray = $this->processUriArray(); $this->module = $this->module(); $this->ctrl = $this->ctrl(); - $this->className = '\\' . APPNS . '\\' . $this->module . '\\controller\\' . $this->ctrl; + $this->class = '\app\\' . $this->module . '\\controller\\' . $this->ctrl; $this->action = $this->action(); $this->param = $this->param(); unset($this->uriArray); diff --git a/framework/library/route/ifs/RouteIfs.php b/framework/library/route/ifs/RouteIfs.php index 32698c2..bd30a52 100644 --- a/framework/library/route/ifs/RouteIfs.php +++ b/framework/library/route/ifs/RouteIfs.php @@ -1,5 +1,5 @@