Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
路由大改,服务组件大改,完工一般,回家继续
Browse files Browse the repository at this point in the history
  • Loading branch information
suyar committed Mar 21, 2017
1 parent d430b60 commit 9533080
Show file tree
Hide file tree
Showing 21 changed files with 299 additions and 261 deletions.
4 changes: 3 additions & 1 deletion config/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
'suffix' => '.html',
'pathinfo' => false,
'rule' => [
'u/:time/:id'=>['index/index/user-name', 'time'=>'/^\d{4}-\d{1,2}-\d{1,2}$/'],
'admin/:id' => ['admin/index/index'],
'user' => ['index/index/name'],
'u/:time/:id' => ['index/index/user-name', 'time'=>'/^\d{4}-\d{1,2}-\d{1,2}$/'],
],
],
'admin.lying.com' => [
Expand Down
25 changes: 17 additions & 8 deletions kernel/Lying.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?php

/**
* 框架基类,用来做各种初始化和自动加载
*
* @author carolkey <[email protected]>
* @since 2.0
* @link https://carolkey.github.io/
* @license MIT
*/
class Lying
{
/**
* @var array 类文件映射
* @var array 核心类文件映射
*/
private static $classMap = [];

/**
* @var array 命名空间映射
* @var array 加载方式配置
*/
private static $extend = [];

Expand All @@ -29,7 +38,7 @@ public static function boot()

self::$maker = new \lying\service\Maker(require DIR_CONF . '/service.php');

self::$extend = self::$maker->createService('config')->read('loader');
self::$extend = self::$maker->config()->read('loader');
}

/**
Expand All @@ -53,7 +62,7 @@ private static function autoload($className)
/**
* classMap加载
* @param string $className 类名
* @return string|boolean 成功返回文件绝对路径,失败返回false
* @return string|boolean 成功返回文件绝对路径失败返回false
*/
private static function classMapLoader($className)
{
Expand All @@ -64,9 +73,9 @@ private static function classMapLoader($className)
}

/**
* PSR-4自动加载,参考 http://www.php-fig.org/psr/psr-4/
* PSR-4自动加载参考 http://www.php-fig.org/psr/psr-4/
* @param string $className 类名
* @return string|boolean 成功返回文件绝对路径,失败返回false
* @return string|boolean 成功返回文件绝对路径失败返回false
*/
private static function psr4Loader($className)
{
Expand Down Expand Up @@ -96,9 +105,9 @@ private static function psr4Loader($className)
}

/**
* PSR-0自动加载,参考 http://www.php-fig.org/psr/psr-0/
* PSR-0自动加载参考 http://www.php-fig.org/psr/psr-0/
* @param string $className 类名
* @return string|boolean 成功返回文件绝对路径,失败返回false
* @return string|boolean 成功返回文件绝对路径失败返回false
*/
private static function psr0Loader($className)
{
Expand Down
6 changes: 3 additions & 3 deletions kernel/base/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ final protected function redirect($url, $params = [])
$q = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
$url .= empty($q) ? '' : (strpos($url, '?') === false ? "?$q" : "&$q");
} else {
$url = router()->createUrl($url, $params);
$url = \Lying::$maker->router()->createUrl($url, $params);
}

while (ob_get_level() !== 0) ob_end_clean();
http_response_code(302);
if (request()->isPjax()) {
if (\Lying::$maker->request()->isPjax()) {
header("X-Pjax-Url: $url");
} else if (request()->isAjax()) {
} else if (\Lying::$maker->request()->isAjax()) {
header("X-Redirect: $url");
} else {
header("Location: $url");
Expand Down
2 changes: 1 addition & 1 deletion kernel/base/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private function renderFile($file, $params)
private function findViewPath($view)
{
$path = explode('/', trim($view, '/'));
list($m, $c, $a) = router()->router();
list($m, $c, $a) = \Lying::$maker->router()->router();
switch (count($path)) {
case 1:
$file = DIR_MODULE . '/' . $m . '/view/' . $c . '/' . $view . '.php';
Expand Down
2 changes: 1 addition & 1 deletion kernel/cache/DbCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DbCache extends Cache
*/
protected function init()
{
$this->connection = db($this->connection);
$this->connection = \Lying::$maker->db($this->connection);
}

/**
Expand Down
1 change: 0 additions & 1 deletion kernel/classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@
'lying\service\Router' => DIR_LYING . '/service/Router.php',
'lying\service\Secure' => DIR_LYING . '/service/Secure.php',
'lying\service\Service' => DIR_LYING . '/service/Service.php',
'lying\service\Session' => DIR_LYING . '/service/Session.php',
];
2 changes: 1 addition & 1 deletion kernel/db/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ActiveRecord extends Service
*/
public static function db()
{
return db();
return \Lying::$maker->db();
}

/**
Expand Down
101 changes: 12 additions & 89 deletions kernel/function.php
Original file line number Diff line number Diff line change
@@ -1,84 +1,9 @@
<?php
/**
* 配置服务
* @return \lying\service\Config
*/
function config()
{
return Lying::$maker->createService('config');
}

/**
* 请求服务
* @return \lying\service\Request
*/
function request()
{
return Lying::$maker->createService('request');
}

/**
* 路由服务
* @return \lying\service\Router
*/
function router()
{
return Lying::$maker->createService('router');
}

/**
* 加密服务
* @return \lying\service\Secure
*/
function secure()
{
return Lying::$maker->createService('secure');
}

/**
* COOKIE服务
* @return \lying\service\Cookie
*/
function cookie()
{
return Lying::$maker->createService('cookie');
}

/**
* 数据库服务
* @param string $id 服务ID
* @return \lying\db\Connection
*/
function db($id = 'db')
{
return Lying::$maker->createService($id);
}

/**
* 日志服务
* @param string $id 服务ID
* @return \lying\logger\Logger
*/
function logger($id = 'logger')
{
return Lying::$maker->createService($id);
}

/**
* 缓存服务
* @param string $id 服务ID
* @return \lying\cache\Cache
*/
function cache($id = 'cache')
{
return Lying::$maker->createService($id);
}

/**
* 获取GET参数
* @param string $key GET参数,放空为获取所有GET参数
* @param string $key GET参数放空为获取所有GET参数
* @param string $default 默认值
* @return string|null|array
* @return string|null|array 成功返回键值,键不存在返回null,没有传入key返回GET数组
*/
function get($key = null, $default = null)
{
Expand All @@ -89,35 +14,33 @@ function get($key = null, $default = null)
* 获取POST参数
* @param string $key POST参数,放空为获取所有POST参数
* @param string $default 默认值
* @return string|null|array
* @return string|null|array 成功返回键值,键不存在返回null,没有传入key返回POST数组
*/
function post($key = null, $default = null)
{
return $key === null ? (isset($_POST[$key]) ? $_POST[$key] : $default) : $_POST;
}

/**
* url生成,支持反解析
* URL生成
* @see \lying\service\Router::createUrl()
* @param string $path 要生成的相对路径
* 如果路径post,则生成当前module,当前控制器下的post方法;
* 如果路径post/index,则生成当前module,控制器为Post下的index方法;
* 如果路径admin/post/index,则生成当前module为admin,控制器为Post下的index方法;
* @param array $params 要生成的参数,一个关联数组,如果有路由规则,参数中必须包含rule中的参数才能反解析
* @return string
* @param array $params URL带的参数,为一个关联数组
* @return string 返回生成的URL
*/
function url($path, $params = [])
{
return router()->createUrl($path, $params);
return \Lying::$maker->router()->createUrl($path, $params);
}

/**
* 锁函数
* @param strings $name 锁名称
* @param string $name 锁名称
* @param integer $type 锁类型
* LOCK_SH 共享锁
* LOCK_EX 独占锁
* LOCK_NB 非阻塞(Windows 上不支持),用法LOCK_EX | LOCK_NB
* @return resource|boolean 成功返回锁文件句柄,失败返回false
* LOCK_NB 非阻塞(Windows上不支持),用法LOCK_EX | LOCK_NB
* @return resource|boolean 成功返回锁文件句柄失败返回false
*/
function lock($name, $type)
{
Expand All @@ -137,7 +60,7 @@ function lock($name, $type)
/**
* 解锁
* @param resource $handle 锁句柄
* @return boolean 成功返回true,失败返回false
* @return boolean 成功返回true失败返回false
*/
function unlock($handle)
{
Expand Down
2 changes: 1 addition & 1 deletion kernel/logger/DbLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DbLog extends Logger
*/
protected function init()
{
$this->connection = db($this->connection);
$this->connection = \Lying::$maker->db($this->connection);
$this->qb = $this->connection->createQuery();
parent::init();
}
Expand Down
4 changes: 2 additions & 2 deletions kernel/logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public function log($data, $level = LOG_DEBUG)
$trace = current(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1));
$trace = [
'time' => date('Y-m-d H:i:s'),
'ip' => request()->remoteAddr(),
'ip' => \Lying::$maker->request()->remoteAddr(),
'level' => self::$levels[$level],
'request' => request()->requestUri(),
'request' => \Lying::$maker->request()->requestUri(),
'file' => $trace['file'],
'line' => $trace['line'],
'data' => self::formatData($data),
Expand Down
18 changes: 13 additions & 5 deletions kernel/service/Config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?php
namespace lying\service;

/**
* 配置读取组件
*
* @author carolkey <[email protected]>
* @since 2.0
* @link https://carolkey.github.io/
* @license MIT
*/
class Config
{
/**
Expand All @@ -10,9 +18,9 @@ class Config

/**
* 返回某个配置的内容
* @param string $name 配置文件名,如果文件不存在,抛出异常
* @param string $key 要获取的键,如果键不存在,抛出异常
* @return mixed 返回配置数组或者某个键值
* @param string $name 配置文件名如果文件不存在,抛出异常
* @param string $key 要获取的键如果键不存在抛出异常
* @return mixed 返回配置数组或者某个键的值
* @throws \Exception 当配置文件不存在或者配置键不存在抛出异常
*/
public function read($name, $key = null)
Expand All @@ -26,9 +34,9 @@ public function read($name, $key = null)
}

/**
* 设置某个配置,配置的改变并不会改变配置文件,只会改变运行时的配置
* 设置某个配置配置的改变并不会改变配置文件只会改变运行时的配置
* @param string $name 配置文件名或者临时配置键名
* @param string $params 参数数组,默认为空
* @param array $params 配置数组,默认为空数组
*/
public function write($name, $params = [])
{
Expand Down
Loading

0 comments on commit 9533080

Please sign in to comment.