From 278843d6baa20cf7b6a21551a207610c818abbf9 Mon Sep 17 00:00:00 2001
From: topnuomi <1130395124@qq.com>
Date: Sun, 25 Aug 2019 00:00:28 +0800
Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=85=8D=E7=BD=AE=E5=8A=A0?=
=?UTF-8?q?=E8=BD=BD=E6=96=B9=E5=BC=8F=E3=80=81=E4=BF=AE=E6=94=B9traits?=
=?UTF-8?q?=E3=80=81=E4=BF=AE=E6=94=B9=E8=A3=85=E9=A5=B0=E5=99=A8=E6=A8=A1?=
=?UTF-8?q?=E5=BC=8F=E5=AE=9E=E7=8E=B0=E7=9A=84=E4=B8=AD=E9=97=B4=E4=BB=B6?=
=?UTF-8?q?=E7=9A=84=E4=BD=9C=E7=94=A8=E3=80=81=E6=96=B0=E5=A2=9ERedis?=
=?UTF-8?q?=E9=83=A8=E5=88=86=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
application/home/config/config.php | 32 +-----
application/home/config/tags.php | 2 -
application/home/controller/Index.php | 7 +-
application/home/middleware/Test.php | 19 ++++
application/home/taglib/Extend.php | 11 +-
application/home/view/Index/index.html | 12 +-
application/route.php | 13 ++-
framework/config/config.php | 40 +++++++
framework/create/create.php | 9 +-
framework/create/tpl/config/config.tpl | 29 +----
framework/create/tpl/config/tags.tpl | 2 -
framework/create/tpl/controller/index.tpl | 5 +-
framework/create/tpl/index.tpl | 4 +
framework/create/tpl/model/demo.tpl | 4 +-
framework/create/tpl/route.tpl | 17 +++
framework/create/tpl/view/index.tpl | 14 ++-
framework/library/Config.php | 32 +++---
framework/library/Controller.php | 4 +-
framework/library/Database.php | 11 +-
framework/library/Model.php | 53 +++++----
framework/library/Template.php | 23 +---
framework/library/View.php | 21 +---
framework/library/cache/File.php | 24 +---
framework/library/cache/Redis.php | 77 +++++++++++++
framework/library/cache/ifs/CacheIfs.php | 4 +-
framework/library/database/driver/MySQLi.php | 21 +---
framework/library/http/Request.php | 104 ++++++++----------
framework/library/http/Response.php | 27 +----
.../library/http/response/ResponseData.php | 2 +-
framework/library/template/driver/Smarty.php | 29 ++---
framework/library/template/driver/Top.php | 33 ++----
framework/library/template/driver/Twig.php | 25 ++---
.../library/template/driver/tags/Engine.php | 43 +++-----
framework/middleware/Init.php | 18 +--
framework/middleware/View.php | 4 +-
framework/traits/Instance.php | 27 +++++
framework/{blocks => traits}/Json.php | 2 +-
public/index.php | 2 +-
.../home/6666cd76f96956469e7be39d750cc7d9 | 24 ----
.../home/8b6903ea105d9a35bb03114244786eb8.php | 9 +-
.../session/sess_an31i19gctv7qc8ehmhm25qihh | 0
41 files changed, 419 insertions(+), 420 deletions(-)
delete mode 100644 application/home/config/tags.php
create mode 100644 application/home/middleware/Test.php
create mode 100644 framework/config/config.php
delete mode 100644 framework/create/tpl/config/tags.tpl
create mode 100644 framework/create/tpl/route.tpl
create mode 100644 framework/library/cache/Redis.php
create mode 100644 framework/traits/Instance.php
rename framework/{blocks => traits}/Json.php (93%)
delete mode 100644 public/runtime/cache/application/home/6666cd76f96956469e7be39d750cc7d9
create mode 100644 public/runtime/session/sess_an31i19gctv7qc8ehmhm25qihh
diff --git a/application/home/config/config.php b/application/home/config/config.php
index ca02d0a..15d1c08 100644
--- a/application/home/config/config.php
+++ b/application/home/config/config.php
@@ -1,35 +1,9 @@
[
- // 'Twig' => \top\library\template\driver\Twig::class,
- // 'Smarty' => \top\library\template\driver\Smarty::class,
- 'Top' => \top\library\template\driver\Top::class,
- ],
- 'middleware' => [],
- 'session' => [
- 'open' => true,
- 'prefix' => 'home',
- ],
'db' => [
- 'driver' => 'MySQLi',
- 'host' => '127.0.0.1',
- 'user' => '',
- 'passwd' => '',
- 'dbname' => '',
- 'prefix' => '',
- 'charset' => 'utf8'
+ 'port' => 3306
],
- 'view' => [
- 'engine' => 'Top',
- 'tagLib' => [
- \app\home\taglib\Extend::class
- ],
- 'ext' => 'html',
- 'dir' => APP_PATH . 'home/view/',
- 'cacheDir' => './runtime/cache/application/home/',
- 'compileDir' => './runtime/compile/application/home/',
- 'left' => '<',
- 'right' => '>',
- 'cacheTime' => 5
+ 'middleware' => [
+ \app\home\middleware\Test::class
],
];
\ No newline at end of file
diff --git a/application/home/config/tags.php b/application/home/config/tags.php
deleted file mode 100644
index a1b26e6..0000000
--- a/application/home/config/tags.php
+++ /dev/null
@@ -1,2 +0,0 @@
-fetch();
+ // return $this->view();
return [];
}
}
diff --git a/application/home/middleware/Test.php b/application/home/middleware/Test.php
new file mode 100644
index 0000000..6fdf00d
--- /dev/null
+++ b/application/home/middleware/Test.php
@@ -0,0 +1,19 @@
+ ['attr' => 'what', 'close' => 0]
+ 'say' => ['attr' => 'what', 'close' => 0],
+ 'lists' => ['attr' => 'name', 'close' => 1]
];
public function _say($tag)
@@ -13,4 +14,12 @@ class Extend
return '';
}
+ public function _lists($tag, $content)
+ {
+ $parse = "";
+ $parse .= $content;
+ $parse .= "";
+ return $parse;
+ }
+
}
diff --git a/application/home/view/Index/index.html b/application/home/view/Index/index.html
index 02d24a8..0905015 100644
--- a/application/home/view/Index/index.html
+++ b/application/home/view/Index/index.html
@@ -2,8 +2,6 @@
BODY
-
- {$b}
@@ -16,4 +14,14 @@
{:date('Y-m-d H:i:s', time())}
+ cut
+
+ content
+
+ content
+
+ content
+
+
+
\ No newline at end of file
diff --git a/application/route.php b/application/route.php
index c04464b..bd83500 100644
--- a/application/route.php
+++ b/application/route.php
@@ -1,16 +1,17 @@
[
+ 'login' => [
null,
- 'home/auth/login'
+ 'home/example/login'
],
- 'intention-detail' => [
+ 'example-detail' => [
'[id]',
- 'home/intention/detail'
+ 'home/example/detail'
],
- 'permission' => [
+ 'example' => [
'[:type]',
- 'home/permission/index'
+ 'home/example/index'
],
];
\ No newline at end of file
diff --git a/framework/config/config.php b/framework/config/config.php
new file mode 100644
index 0000000..c859d5e
--- /dev/null
+++ b/framework/config/config.php
@@ -0,0 +1,40 @@
+ [
+ 'Top' => \top\library\template\driver\Top::class,
+ ],
+ 'middleware' => [],
+ 'session' => [
+ 'open' => true,
+ 'prefix' => '',
+ ],
+ 'db' => [
+ 'driver' => 'MySQLi',
+ 'host' => '127.0.0.1',
+ 'user' => '',
+ 'passwd' => '',
+ 'dbname' => '',
+ 'prefix' => '',
+ 'charset' => 'utf8'
+ ],
+ 'redis' => [
+ 'host' => '127.0.0.1',
+ 'port' => 6379,
+ 'auth' => '',
+ ],
+ 'view' => [
+ 'engine' => 'Top',
+ 'tagLib' => [
+ \app\home\taglib\Extend::class
+ ],
+ 'ext' => 'html',
+ 'dir' => '',
+ 'cacheDir' => '',
+ 'compileDir' => '',
+ 'left' => '<',
+ 'right' => '>',
+ 'cacheTime' => 5
+ ],
+];
diff --git a/framework/create/create.php b/framework/create/create.php
index d4f12d1..e53d79f 100644
--- a/framework/create/create.php
+++ b/framework/create/create.php
@@ -55,7 +55,6 @@ class Create
{
$configPath = $this->projectPath . 'config/';
$configFile = $configPath . 'config.php';
- $tagsFile = $configPath . 'tags.php';
if (!is_dir($configPath)) {
mkdir($configPath, 0777, true);
}
@@ -67,12 +66,6 @@ class Create
exit('error -2');
}
}
- if (!file_exists($tagsFile)) {
- $content = file_get_contents($this->dir . 'tpl/config/tags.tpl');
- if (!file_put_contents($configPath . 'tags.php', $content)) {
- exit('error -3');
- }
- }
return true;
}
@@ -130,7 +123,7 @@ class Create
{
$file = $this->projectPath . '../route.php';
if (!file_exists($file)) {
- if (!file_put_contents($file, "dir . 'route.tpl'))) {
exit('-8');
}
}
diff --git a/framework/create/tpl/config/config.tpl b/framework/create/tpl/config/config.tpl
index 9ec2e92..05e0b10 100644
--- a/framework/create/tpl/config/config.tpl
+++ b/framework/create/tpl/config/config.tpl
@@ -1,31 +1,4 @@
[
- 'Twig' => \top\library\template\driver\Twig::class,
- // 'Smarty' => \top\library\template\driver\Smarty::class,
- // 'Top' => \top\library\template\driver\Top::class,
- ],
- 'decorator' => [],
- 'session' => [
- 'open' => true,
- 'prefix' => '{name}',
- ],
- 'db' => [
- 'driver' => 'MySQLi',
- 'host' => '',
- 'user' => '',
- 'passwd' => '',
- 'dbname' => '',
- 'charset' => 'utf8'
- ],
- 'view' => [
- 'engine' => 'Twig',
- 'ext' => 'html',
- 'dir' => '../application/{name}/view/',
- 'cacheDir' => './runtime/cache/application/{name}/',
- 'compileDir' => './runtime/compile/application/{name}/',
- 'left' => '{',
- 'right' => '}',
- 'cacheTime' => 5
- ],
+
];
\ No newline at end of file
diff --git a/framework/create/tpl/config/tags.tpl b/framework/create/tpl/config/tags.tpl
deleted file mode 100644
index a1b26e6..0000000
--- a/framework/create/tpl/config/tags.tpl
+++ /dev/null
@@ -1,2 +0,0 @@
- $model->get(1)
+ 'hello' => $model->get()
];
}
}
diff --git a/framework/create/tpl/index.tpl b/framework/create/tpl/index.tpl
index 655f2d7..961dd3b 100644
--- a/framework/create/tpl/index.tpl
+++ b/framework/create/tpl/index.tpl
@@ -18,6 +18,10 @@ require '../framework/Framework.php';
// Framework::appNameSpace('app');
// 可使用常量APP_NS取得该值
+// session保存目录,缺省值:./runtime/session/
+// Framework::sessionPath('./runtime/session/');
+// 可使用常量SESSION_PATH取得该值
+
// 框架目录,缺省值:Framework.php的绝对路径
// Framework::frameworkPath('../framework');
// 可使用常量FRAMEWORK_PATH取得该值
diff --git a/framework/create/tpl/model/demo.tpl b/framework/create/tpl/model/demo.tpl
index cb7273f..2bc1361 100644
--- a/framework/create/tpl/model/demo.tpl
+++ b/framework/create/tpl/model/demo.tpl
@@ -10,8 +10,8 @@ class Demo extends Model
protected $pk = '';
protected $map = [];
- public function get($id)
+ public function get()
{
- return $id;
+ return 'Hello';
}
}
diff --git a/framework/create/tpl/route.tpl b/framework/create/tpl/route.tpl
new file mode 100644
index 0000000..bd83500
--- /dev/null
+++ b/framework/create/tpl/route.tpl
@@ -0,0 +1,17 @@
+ [
+ null,
+ 'home/example/login'
+ ],
+ 'example-detail' => [
+ '[id]',
+ 'home/example/detail'
+ ],
+ 'example' => [
+ '[:type]',
+ 'home/example/index'
+ ],
+];
\ No newline at end of file
diff --git a/framework/create/tpl/view/index.tpl b/framework/create/tpl/view/index.tpl
index 0e9d6dd..e0050d0 100644
--- a/framework/create/tpl/view/index.tpl
+++ b/framework/create/tpl/view/index.tpl
@@ -1 +1,13 @@
-{{ data }}
\ No newline at end of file
+
+
+
+
+
+
+ Document
+
+
+{$hello}
+
+
\ No newline at end of file
diff --git a/framework/library/Config.php b/framework/library/Config.php
index 9e17ca1..ddfa0b3 100644
--- a/framework/library/Config.php
+++ b/framework/library/Config.php
@@ -3,6 +3,7 @@
namespace top\library;
use top\library\http\Request;
+use top\traits\Instance;
/**
* 配置类
@@ -11,28 +12,19 @@ use top\library\http\Request;
class Config
{
+ use Instance;
+
// 已加载的文件
private static $files;
- private static $instance;
-
// 保存配置的变量
private $config = [];
private function __construct()
{
- }
-
- private function __clone()
- {
- }
-
- public static function instance()
- {
- if (!self::$instance) {
- self::$instance = new self();
- }
- return self::$instance;
+ // 加载默认配置文件
+ $configFile = FRAMEWORK_PATH . 'config/config.php';
+ $this->config = require $configFile;
}
/**
@@ -59,14 +51,20 @@ class Config
*/
public function get($name = '')
{
- // 加载文件
+ // 加载用户配置文件
$module = Request::instance()->module();
$file = APP_PATH . $module . '/config/config.php';
if (!isset(self::$files[$file])) {
if (file_exists($file)) {
$config = require $file;
- // 与原有的配置项合并
- $this->config = array_merge($this->config, $config);
+ // 合并配置项
+ foreach ($config as $key => $value) {
+ if (array_key_exists($key, $this->config)) {
+ $this->config[$key] = array_merge($this->config[$key], $config[$key]);
+ } else {
+ $this->config[$key] = $value;
+ }
+ }
self::$files[$file] = true;
}
}
diff --git a/framework/library/Controller.php b/framework/library/Controller.php
index 221bca2..c2e1055 100644
--- a/framework/library/Controller.php
+++ b/framework/library/Controller.php
@@ -60,7 +60,7 @@ abstract class Controller
* @param bool $cache
* @return mixed
*/
- protected function fetch($file = '', $param = [], $cache = false)
+ protected function view($file = '', $param = [], $cache = false)
{
return Register::get('View')->fetch($file, $param, $cache);
}
@@ -89,7 +89,7 @@ abstract class Controller
$viewConfig = Register::get('Config')->get('view');
$tipsTemplate = $viewConfig['dir'] . 'tips.' . $viewConfig['ext'];
(!file_exists($tipsTemplate)) && file_put_contents($tipsTemplate, '');
- return $this->fetch('tips', [
+ return $this->view('tips', [
'message' => $message,
'url' => $url,
'sec' => $sec
diff --git a/framework/library/Database.php b/framework/library/Database.php
index 746e03a..a40bc47 100644
--- a/framework/library/Database.php
+++ b/framework/library/Database.php
@@ -52,8 +52,6 @@ class Database
// 关联
private $on = [];
- private $data = null;
-
/**
* Database constructor.
* @param $table
@@ -68,10 +66,6 @@ class Database
$this->setDriver($driver, $this->config);
}
- private function __clone()
- {
- }
-
/**
* 指定数据库驱动
*
@@ -425,4 +419,9 @@ class Database
}
return $this->pk;
}
+
+ private function __clone()
+ {
+ }
+
}
diff --git a/framework/library/Model.php b/framework/library/Model.php
index 3d79f02..e213ca7 100644
--- a/framework/library/Model.php
+++ b/framework/library/Model.php
@@ -54,7 +54,16 @@ class Model
$table = get_table_name(get_called_class());
$this->table = $table;
}
- $this->db = Database::table($this->table, $this->pk);
+ // $this->getDb() = Database::table($this->table, $this->pk);
+ }
+
+ /**
+ * 获取Database实例
+ * @return mixed
+ */
+ private function getDb()
+ {
+ return Database::table($this->table, $this->pk);
}
/**
@@ -64,7 +73,7 @@ class Model
*/
public function effect($effect)
{
- $this->db->effect($effect);
+ $this->getDb()->effect($effect);
return $this;
}
@@ -75,7 +84,7 @@ class Model
*/
public function distinct($field)
{
- $this->db->distinct($field);
+ $this->getDb()->distinct($field);
return $this;
}
@@ -86,7 +95,7 @@ class Model
*/
public function field($field)
{
- $this->db->field($field);
+ $this->getDb()->field($field);
return $this;
}
@@ -97,7 +106,7 @@ class Model
public function where()
{
call_user_func_array([
- $this->db,
+ $this->getDb(),
'where'
], func_get_args());
return $this;
@@ -110,7 +119,7 @@ class Model
public function order()
{
call_user_func_array([
- $this->db,
+ $this->getDb(),
'order'
], func_get_args());
return $this;
@@ -123,7 +132,7 @@ class Model
public function limit()
{
call_user_func_array([
- $this->db,
+ $this->getDb(),
'limit'
], func_get_args());
return $this;
@@ -138,7 +147,7 @@ class Model
*/
public function join($type, $table, $name)
{
- $this->db->join($type, $table, $name);
+ $this->getDb()->join($type, $table, $name);
return $this;
}
@@ -149,7 +158,7 @@ class Model
*/
public function on($on)
{
- $this->db->on($on);
+ $this->getDb()->on($on);
return $this;
}
@@ -166,7 +175,7 @@ class Model
// 此处取消了数据验证,在$this->>data()方法中验证,减少一次数据库查询
// 入库时最后的数据处理
$data = $this->inHandle($data);
- return $this->db->insert($data);
+ return $this->getDb()->insert($data);
}
return false;
}
@@ -178,7 +187,7 @@ class Model
*/
public function delete($param = false)
{
- return $this->db->delete($param);
+ return $this->getDb()->delete($param);
}
/**
@@ -195,7 +204,7 @@ class Model
// 此处取消了数据验证,在$this->data()方法中验证,减少一次数据库查询
// 入库时最后的数据处理
$data = $this->inHandle($data);
- return $this->db->update($data, $param);
+ return $this->getDb()->update($data, $param);
}
return false;
}
@@ -208,7 +217,7 @@ class Model
*/
public function find($param = false, $notRaw = true)
{
- $result = $this->db->find($param);
+ $result = $this->getDb()->find($param);
if ($notRaw) {
if (is_array($result)) {
$result = $this->outHandle($result);
@@ -225,7 +234,7 @@ class Model
*/
public function select($param = false, $notRaw = true)
{
- $result = $this->db->select($param);
+ $result = $this->getDb()->select($param);
if ($notRaw) {
if (is_array($result)) {
$result = $this->outHandle($result);
@@ -241,7 +250,7 @@ class Model
*/
public function count($param = '')
{
- return $this->db->common($param, 'count');
+ return $this->getDb()->common($param, 'count');
}
/**
@@ -251,7 +260,7 @@ class Model
*/
public function avg($param = '')
{
- return $this->db->common($param, 'avg');
+ return $this->getDb()->common($param, 'avg');
}
/**
@@ -261,7 +270,7 @@ class Model
*/
public function max($param = '')
{
- return $this->db->common($param, 'max');
+ return $this->getDb()->common($param, 'max');
}
/**
@@ -271,7 +280,7 @@ class Model
*/
public function min($param = '')
{
- return $this->db->common($param, 'min');
+ return $this->getDb()->common($param, 'min');
}
/**
@@ -281,7 +290,7 @@ class Model
*/
public function sum($param = '')
{
- return $this->db->common($param, 'sum');
+ return $this->getDb()->common($param, 'sum');
}
/**
@@ -291,7 +300,7 @@ class Model
*/
public function query($query)
{
- return $this->db->query($query);
+ return $this->getDb()->query($query);
}
/**
@@ -301,7 +310,7 @@ class Model
*/
public function _sql()
{
- return $this->db->_sql();
+ return $this->getDb()->_sql();
}
/**
@@ -513,7 +522,7 @@ class Model
*/
public function tableDesc($table)
{
- return $this->db->tableDesc($table);
+ return $this->getDb()->tableDesc($table);
}
/**
diff --git a/framework/library/Template.php b/framework/library/Template.php
index 8e85489..a2de458 100644
--- a/framework/library/Template.php
+++ b/framework/library/Template.php
@@ -3,6 +3,7 @@
namespace top\library;
use top\library\template\ifs\TemplateIfs;
+use top\traits\Instance;
/**
* 模板类
@@ -11,12 +12,11 @@ use top\library\template\ifs\TemplateIfs;
class Template
{
+ use Instance;
+
// 操作的具体实现
private $template;
- // 当前类的实例
- private static $instance;
-
private $param = [];
/**
@@ -27,23 +27,6 @@ class Template
$this->template = $template->run();
}
- private function __clone()
- {
- }
-
- /**
- * 获取实例
- * @param TemplateIfs $template
- * @return \top\library\Template
- */
- public static function instance($template)
- {
- if (!self::$instance) {
- self::$instance = new self($template);
- }
- return self::$instance;
- }
-
/**
* 是否开启页面静态缓存
* @param $status
diff --git a/framework/library/View.php b/framework/library/View.php
index 29f2ac6..87382a4 100644
--- a/framework/library/View.php
+++ b/framework/library/View.php
@@ -2,6 +2,8 @@
namespace top\library;
+use top\traits\Instance;
+
/**
* 基础视图类
* @author topnuomi 2018年11月22日
@@ -9,7 +11,7 @@ namespace top\library;
class View
{
- private static $instance;
+ use Instance;
// 用户的配置
private $config = [];
@@ -17,19 +19,6 @@ class View
// 视图类实例
private $template;
- /**
- * 获取实例
- * @return View
- * @throws \Exception
- */
- public static function instance()
- {
- if (!self::$instance) {
- self::$instance = new self();
- }
- return self::$instance;
- }
-
/**
* View constructor.
* @throws \Exception
@@ -41,10 +30,6 @@ class View
$this->template = Template::instance($driver);
}
- private function __clone()
- {
- }
-
/**
* 传递参数
* @param $name
diff --git a/framework/library/cache/File.php b/framework/library/cache/File.php
index e8de2f3..1f8e28e 100644
--- a/framework/library/cache/File.php
+++ b/framework/library/cache/File.php
@@ -3,35 +3,21 @@
namespace top\library\cache;
use top\library\cache\ifs\CacheIfs;
+use top\traits\Instance;
class File implements CacheIfs
{
- private static $instance;
-
- public static function instance()
- {
- if (!self::$instance) {
- self::$instance = new self();
- }
- return self::$instance;
- }
-
- private function __construct()
- {
- }
-
- private function __clone()
- {
- }
+ use Instance;
/**
* 设置缓存
* @param string $name
* @param string $value
+ * @param bool $timeout
* @return bool
*/
- public function set($name = '', $value = '')
+ public function set($name = '', $value = '', $timeout = 0)
{
// TODO Auto-generated method stub
$dirArray = explode('/', $name);
@@ -58,7 +44,7 @@ class File implements CacheIfs
* 删除缓存
* @param string $name
*/
- public function _unset($name = '')
+ public function remove($name = '')
{
}
diff --git a/framework/library/cache/Redis.php b/framework/library/cache/Redis.php
new file mode 100644
index 0000000..7f4c06b
--- /dev/null
+++ b/framework/library/cache/Redis.php
@@ -0,0 +1,77 @@
+get('redis');
+ $this->redis = new \Redis();
+ $this->redis->connect($config['host'], $config['port']);
+ if ($config['auth']) {
+ $this->redis->auth($config['auth']);
+ }
+ }
+
+ /**
+ * 设置缓存
+ * @param string $name
+ * @param string $value
+ * @return bool
+ */
+ public function set($name = '', $value = '', $timeout = 0)
+ {
+ if (is_array($value) || is_object($value)) {
+ $value = json_encode($value);
+ }
+ return $this->redis->set($name, $value, $timeout);
+ }
+
+ /**
+ * 获取缓存的值
+ * @param string $name
+ * @return bool|mixed|string
+ */
+ public function get($name = '')
+ {
+ $value = $this->redis->get($name);
+ $jsonDecode = json_decode($value);
+ if (is_null($jsonDecode)) {
+ return $value;
+ } else {
+ return $jsonDecode;
+ }
+ }
+
+ /**
+ * 删除缓存
+ * @param string $name
+ * @return int
+ */
+ public function remove($name = '')
+ {
+ return $this->redis->del($name);
+ }
+
+ /**
+ * 判断缓存是否设置
+ * @param $name
+ * @return bool
+ */
+ public function exists($name)
+ {
+ return $this->redis->exists($name);
+ }
+}
diff --git a/framework/library/cache/ifs/CacheIfs.php b/framework/library/cache/ifs/CacheIfs.php
index abd2eb8..da2460a 100644
--- a/framework/library/cache/ifs/CacheIfs.php
+++ b/framework/library/cache/ifs/CacheIfs.php
@@ -5,9 +5,9 @@ namespace top\library\cache\ifs;
interface CacheIfs
{
- public function set($name = '', $value = '');
+ public function set($name = '', $value = '', $timeout = 0);
public function get($name = '');
- public function _unset($name = '');
+ public function remove($name = '');
}
diff --git a/framework/library/database/driver/MySQLi.php b/framework/library/database/driver/MySQLi.php
index acece94..47952d2 100644
--- a/framework/library/database/driver/MySQLi.php
+++ b/framework/library/database/driver/MySQLi.php
@@ -4,36 +4,21 @@ namespace top\library\database\driver;
use top\library\database\ifs\DatabaseIfs;
use top\library\exception\DatabaseException;
+use top\traits\Instance;
/**
- * Mysqli数据库驱动
+ * MySQLi数据库驱动
* @author topnuomi 2018年11月20日
*/
class MySQLi implements DatabaseIfs
{
- private static $instance;
+ use Instance;
private $link;
private $sql;
- public static function instance()
- {
- if (!self::$instance) {
- self::$instance = new self();
- }
- return self::$instance;
- }
-
- private function __construct()
- {
- }
-
- private function __clone()
- {
- }
-
/**
* 连接数据库
* @param array $config
diff --git a/framework/library/http/Request.php b/framework/library/http/Request.php
index aa63e3a..2abf492 100644
--- a/framework/library/http/Request.php
+++ b/framework/library/http/Request.php
@@ -9,6 +9,7 @@ use top\library\route\driver\Command;
use top\library\route\driver\Pathinfo;
use top\library\Router;
use top\middleware\View;
+use top\traits\Instance;
/**
* 请求类
@@ -17,11 +18,7 @@ use top\middleware\View;
class Request
{
- /**
- * 当前实例
- * @var null
- */
- private static $instance = null;
+ use Instance;
/**
* 保存$_SERVER变量
@@ -71,26 +68,11 @@ class Request
*/
private $except = [];
- /**
- * @return null|Request
- */
- public static function instance()
- {
- if (!self::$instance) {
- self::$instance = new self();
- }
- return self::$instance;
- }
-
private function __construct()
{
$this->server = (!empty($_SERVER)) ? $_SERVER : [];
}
- private function __clone()
- {
- }
-
/**
* 当前请求方式
* @return mixed|string
@@ -334,8 +316,13 @@ class Request
private function beforeRoute()
{
foreach ($this->middleware as $middleware) {
- $middleware->before();
+ $returnData = $middleware->before();
+ if ($returnData !== true) {
+ return $returnData;
+ }
+ unset($returnData);
}
+ return true;
}
/**
@@ -386,6 +373,15 @@ class Request
// 实例化路由,并执行对应方法
$routeDriver = $this->routeDriver($type);
$this->router = (new Router($routeDriver, $defaultModule))->handler();
+
+ $userMiddleware = Register::get('Config')->get('middleware');
+ $systemMiddleware = [Init::class, View::class];
+
+ $middleware = array_merge($systemMiddleware, $userMiddleware);
+ foreach ($middleware as $key => $value) {
+ $this->middleware(new $value());
+ }
+
$data = $this->runAction();
return $data;
}
@@ -397,47 +393,43 @@ class Request
*/
private function runAction()
{
- $userMiddleware = Register::get('Config')->get('middleware');
- $systemMiddleware = [Init::class, View::class];
+ $middlewareData = $this->beforeRoute();
- $middleware = array_merge($systemMiddleware, $userMiddleware);
- foreach ($middleware as $key => $value) {
- $this->middleware(new $value());
- }
+ if ($middlewareData === true) {
+ $ctrl = $this->router->class;
+ $method = $this->router->method;
+ $params = $this->router->params;
- $this->beforeRoute();
-
- $ctrl = $this->router->class;
- $method = $this->router->method;
- $params = $this->router->params;
-
- $data = null;
- $object = new $ctrl();
- $reflectionClass = new \ReflectionClass($ctrl);
- if ($reflectionClass->hasMethod('_init')) {
- $data = $object->_init();
- }
-
- if ($data === null || $data === '') {
- // 前置方法
- $beforeReturnData = null;
- $beforeMethod = 'before_' . $method;
- if ($reflectionClass->hasMethod($beforeMethod)) {
- $beforeReturnData = $object->{$beforeMethod}();
+ $data = null;
+ $object = new $ctrl();
+ $reflectionClass = new \ReflectionClass($ctrl);
+ if ($reflectionClass->hasMethod('_init')) {
+ $data = $object->_init();
}
- if ($beforeReturnData === null || $beforeReturnData === '') {
- $reflectionMethod = new \ReflectionMethod($ctrl, $method);
- $data = $reflectionMethod->invokeArgs($object, $params);
-
- // 后置方法
- $afterMethod = 'after_' . $method;
- if ($reflectionClass->hasMethod($afterMethod)) {
- $object->{$afterMethod}();
+ if ($data === null || $data === '') {
+ // 前置方法
+ $beforeReturnData = null;
+ $beforeMethod = 'before_' . $method;
+ if ($reflectionClass->hasMethod($beforeMethod)) {
+ $beforeReturnData = $object->{$beforeMethod}();
+ }
+
+ if ($beforeReturnData === null || $beforeReturnData === '') {
+ $reflectionMethod = new \ReflectionMethod($ctrl, $method);
+ $data = $reflectionMethod->invokeArgs($object, $params);
+
+ // 后置方法
+ $afterMethod = 'after_' . $method;
+ if ($reflectionClass->hasMethod($afterMethod)) {
+ $object->{$afterMethod}();
+ }
+ } else {
+ $data = $beforeReturnData;
}
- } else {
- $data = $beforeReturnData;
}
+ } else {
+ $data = $middlewareData;
}
$this->afterRoute($data);
diff --git a/framework/library/http/Response.php b/framework/library/http/Response.php
index c739377..9366890 100644
--- a/framework/library/http/Response.php
+++ b/framework/library/http/Response.php
@@ -3,6 +3,7 @@
namespace top\library\http;
use top\library\http\response\ResponseData;
+use top\traits\Instance;
/**
* 响应类
@@ -12,11 +13,7 @@ use top\library\http\response\ResponseData;
class Response
{
- /**
- * 当前类实例
- * @var null
- */
- private static $instance = null;
+ use Instance;
/**
* 响应内容
@@ -30,26 +27,6 @@ class Response
*/
private $header = [];
- private function __construct()
- {
- }
-
- private function __clone()
- {
- }
-
- /**
- * 获取当前类单例
- * @return null|Response
- */
- public static function instance()
- {
- if (!self::$instance) {
- self::$instance = new self();
- }
- return self::$instance;
- }
-
/**
* 设置Header
* @param array $header
diff --git a/framework/library/http/response/ResponseData.php b/framework/library/http/response/ResponseData.php
index 1439aef..6dbba94 100644
--- a/framework/library/http/response/ResponseData.php
+++ b/framework/library/http/response/ResponseData.php
@@ -2,8 +2,8 @@
namespace top\library\http\response;
-use top\blocks\Json;
use top\library\Register;
+use top\traits\Json;
/**
* 处理响应数据
diff --git a/framework/library/template/driver/Smarty.php b/framework/library/template/driver/Smarty.php
index 73784e0..e23526c 100644
--- a/framework/library/template/driver/Smarty.php
+++ b/framework/library/template/driver/Smarty.php
@@ -4,38 +4,27 @@ namespace top\library\template\driver;
use top\library\Register;
use top\library\template\ifs\TemplateIfs;
+use top\traits\Instance;
class Smarty implements TemplateIfs
{
- private static $instance;
+ use Instance;
private $config = [];
private $smarty;
- private function __construct()
- {
- }
-
- private function __clone()
- {
- }
-
- public static function instance()
- {
- if (!self::$instance) {
- self::$instance = new self();
- }
- return self::$instance;
- }
-
public function run()
{
$this->config = Register::get('Config')->get('view');
+ $module = request()->module();
+ (!$this->config['dir']) && $this->config['dir'] = APP_PATH . 'home/view/';
+ (!$this->config['cacheDir']) && $this->config['cacheDir'] = './runtime/cache/application/' . $module . '/';
+ (!$this->config['compileDir']) && $this->config['compileDir'] = './runtime/compile/application/' . $module . '/';
$this->smarty = new \Smarty();
- (isset($this->config['cacheDir'])) && $this->smarty->setCacheDir($this->config['cacheDir']);
- (isset($this->config['compileDir'])) && $this->smarty->setCompileDir($this->config['compileDir']);
+ $this->smarty->setCacheDir($this->config['cacheDir']);
+ $this->smarty->setCompileDir($this->config['compileDir']);
return $this;
}
@@ -51,7 +40,7 @@ class Smarty implements TemplateIfs
foreach ($params as $k => $v) {
$this->smarty->assign($k, $v);
}
- $templateFile = $this->config['dir'] . $file . '.' . $this->config['ext'];
+ $templateFile = $this->config['dir'] . $file . '.' . ltrim($this->config['ext'], '.');
return $this->smarty->fetch($templateFile);
}
}
diff --git a/framework/library/template/driver/Top.php b/framework/library/template/driver/Top.php
index 5bfda25..0fdb0ae 100644
--- a/framework/library/template/driver/Top.php
+++ b/framework/library/template/driver/Top.php
@@ -5,14 +5,12 @@ namespace top\library\template\driver;
use top\library\Register;
use top\library\template\driver\tags\Engine;
use top\library\template\ifs\TemplateIfs;
+use top\traits\Instance;
class Top implements TemplateIfs
{
- /**
- * @var null 当前实例
- */
- private static $instance = null;
+ use Instance;
/**
* @var null 模板引擎实现
@@ -29,30 +27,14 @@ class Top implements TemplateIfs
*/
private $cache = false;
- /**
- * 构造方法
- * Top constructor.
- */
- private function __construct()
- {
- }
-
- /**
- * 外部获取当前类实例
- * @return null|Top
- */
- public static function instance()
- {
- if (!self::$instance) {
- self::$instance = new self();
- }
- return self::$instance;
- }
-
public function run()
{
- $this->engine = Engine::instance();
$this->config = Register::get('Config')->get('view');
+ $module = request()->module();
+ (!$this->config['dir']) && $this->config['dir'] = APP_PATH . $module . '/view/';
+ (!$this->config['cacheDir']) && $this->config['cacheDir'] = './runtime/cache/application/' . $module . '/';
+ (!$this->config['compileDir']) && $this->config['compileDir'] = './runtime/compile/application/' . $module . '/';
+ $this->engine = Engine::instance($this->config);
return $this;
}
@@ -98,7 +80,6 @@ class Top implements TemplateIfs
*/
private function cacheFile($filename, $params)
{
-
if (isset($_SERVER['REQUEST_URI'])) {
$fileIdent = md5($_SERVER['REQUEST_URI']);
} else {
diff --git a/framework/library/template/driver/Twig.php b/framework/library/template/driver/Twig.php
index 4dc4f00..5d51f5c 100644
--- a/framework/library/template/driver/Twig.php
+++ b/framework/library/template/driver/Twig.php
@@ -4,35 +4,24 @@ namespace top\library\template\driver;
use top\library\Register;
use top\library\template\ifs\TemplateIfs;
+use top\traits\Instance;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class Twig implements TemplateIfs
{
- private static $instance;
+ use Instance;
private $config = [];
- private function __construct()
- {
- }
-
- private function __clone()
- {
- }
-
- public static function instance()
- {
- if (!self::$instance) {
- self::$instance = new self();
- }
- return self::$instance;
- }
-
public function run()
{
$this->config = Register::get('Config')->get('view');
+ $module = request()->module();
+ (!$this->config['dir']) && $this->config['dir'] = APP_PATH . 'home/view/';
+ (!$this->config['cacheDir']) && $this->config['cacheDir'] = './runtime/cache/application/' . $module . '/';
+ (!$this->config['compileDir']) && $this->config['compileDir'] = './runtime/compile/application/' . $module . '/';
return $this;
}
@@ -51,7 +40,7 @@ class Twig implements TemplateIfs
'auto_reload' => true,
'debug' => DEBUG
]);
- $templateFile = '@base/' . $file . '.' . $this->config['ext'];
+ $templateFile = '@base/' . $file . '.' . ltrim($this->config['ext'], '.');
return $template->render($templateFile, $params);
}
}
diff --git a/framework/library/template/driver/tags/Engine.php b/framework/library/template/driver/tags/Engine.php
index 74aa6d7..77a5fce 100644
--- a/framework/library/template/driver/tags/Engine.php
+++ b/framework/library/template/driver/tags/Engine.php
@@ -3,6 +3,7 @@
namespace top\library\template\driver\tags;
use top\library\Register;
+use top\traits\Instance;
/**
* 模板标签库(支持模板继承)
@@ -11,10 +12,8 @@ use top\library\Register;
*/
class Engine
{
- /**
- * @var null 单一实例
- */
- private static $instance = null;
+
+ use Instance;
/**
* @var string 左定界符
@@ -60,11 +59,11 @@ class Engine
/**
* 构造方法
* Engine constructor.
- * @throws \Exception
+ * @param array $config
*/
- private function __construct()
+ private function __construct($config = [])
{
- $this->config = Register::get('Config')->get('view');
+ $this->config = $config;
if (isset($this->config['left']) && $this->config['left']) {
$this->left = $this->config['left'];
}
@@ -73,18 +72,6 @@ class Engine
}
}
- /**
- * 获取类单一实例
- * @return null|Engine
- */
- public static function instance()
- {
- if (!self::$instance) {
- self::$instance = new self();
- }
- return self::$instance;
- }
-
/**
* 处理模板继承
* @param $template
@@ -98,7 +85,7 @@ class Engine
$blockPattern = '/' . $this->left . 'block.*?name=[\'"](.*?)[\'"]' . $this->right;
$blockPattern .= '([\s\S]*?)' . $this->left . '\/block' . $this->right . '/is';
// 获得被继承的模板内容
- $file = $this->config['dir'] . $matches[1] . '.html';
+ $file = $this->config['dir'] . $matches[1] . '.' . ltrim($this->config['ext'], '.');
$extendFileContent = null;
if (file_exists($file)) {
$extendFileContent = file_get_contents($file);
@@ -148,7 +135,7 @@ class Engine
$pattern = '/' . $this->left . 'include.*?file=[\'"](.*?)[\'"].*?\/' . $this->right . '/is';
$template = preg_replace_callback($pattern, function ($result) {
$string = null;
- $file = $this->config['dir'] . $result[1] . '.html';
+ $file = $this->config['dir'] . $result[1] . '.' . ltrim($this->config['ext'], '.');
if (file_exists($file)) {
$string = file_get_contents($file);
}
@@ -363,8 +350,8 @@ class Engine
$this->left, $this->right,
'{', '}'
], [
- '',
- ''
+ '',
+ ''
], $matches[1]);
}, $template);
return $template;
@@ -378,8 +365,8 @@ class Engine
public function returnRaw($template)
{
$template = str_replace([
- '',
- ''
+ '',
+ ''
], [
$this->left, $this->right,
'{', '}'
@@ -443,6 +430,12 @@ class Engine
return $parse;
}
+ private function _assign($tag)
+ {
+ $parse = '';
+ return $parse;
+ }
+
/**
* 获取编译后的内容
* @param $template
diff --git a/framework/middleware/Init.php b/framework/middleware/Init.php
index 81b3b7c..7dcca25 100644
--- a/framework/middleware/Init.php
+++ b/framework/middleware/Init.php
@@ -21,6 +21,15 @@ class Init implements MiddlewareIfs
*/
public function before()
{
+ // 加载系统函数库
+ require FRAMEWORK_PATH . 'library/functions/functions.php';
+
+ // 加载用户函数库
+ $funcFile = APP_PATH . request()->module() . '/functions.php';
+ if (file_exists($funcFile)) {
+ require $funcFile;
+ }
+
$sessionConfig = Register::get('Config')->get('session');
if (!empty($sessionConfig) && $sessionConfig['open'] === true) {
session_save_path(SESSION_PATH);
@@ -55,14 +64,7 @@ class Init implements MiddlewareIfs
return View::instance();
});
- // 加载系统函数库
- require FRAMEWORK_PATH . 'library/functions/functions.php';
-
- // 加载用户函数库
- $funcFile = APP_PATH . request()->module() . '/functions.php';
- if (file_exists($funcFile)) {
- require $funcFile;
- }
+ return true;
}
/**
diff --git a/framework/middleware/View.php b/framework/middleware/View.php
index 1c5d519..eef094c 100644
--- a/framework/middleware/View.php
+++ b/framework/middleware/View.php
@@ -22,10 +22,10 @@ class View implements MiddlewareIfs
$filename = $config['cacheDir'] . $fileIdent;
$cache = Register::get('FileCache');
if ($cache->check($filename, $config['cacheTime'])) {
- echo Response::instance()->dispatch(file_get_contents($filename));
- exit;
+ return Response::instance()->dispatch(file_get_contents($filename));
}
}
+ return true;
}
public function after($data)
diff --git a/framework/traits/Instance.php b/framework/traits/Instance.php
new file mode 100644
index 0000000..2d2e467
--- /dev/null
+++ b/framework/traits/Instance.php
@@ -0,0 +1,27 @@
+
-
-
-
- Title
-
-
-父级模板
-
- BODY
- 1
-
-
- {$a}
-
- {$b}
-
-
-
- 2019-07-28 16:22:50 a.html
-b.html
- 你好
-
-
\ No newline at end of file
diff --git a/public/runtime/compile/application/home/8b6903ea105d9a35bb03114244786eb8.php b/public/runtime/compile/application/home/8b6903ea105d9a35bb03114244786eb8.php
index 98fe610..04c8706 100644
--- a/public/runtime/compile/application/home/8b6903ea105d9a35bb03114244786eb8.php
+++ b/public/runtime/compile/application/home/8b6903ea105d9a35bb03114244786eb8.php
@@ -8,7 +8,6 @@
父级模板
BODY
-
@@ -22,6 +21,14 @@
a.html
b.html
+ cut
+
+ content
+
+ content
+
+ content
+