Skip to content

Commit

Permalink
refactor(helper): change helper class to a method file 🔨
Browse files Browse the repository at this point in the history
  • Loading branch information
TIGERB committed May 14, 2017
1 parent ea3f7bd commit 408dbee
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 112 deletions.
1 change: 0 additions & 1 deletion app/demo/controllers/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace App\Demo\Controllers;

use Framework\App;
use Framework\Helper;
use Framework\Loger;

/**
Expand Down
14 changes: 6 additions & 8 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
* *
********************************************/

use Framework\Helper;

return [
/* 默认配置 */
'database' => [
'dbtype' => Helper::env('database')['dbtype'],
'dbprefix' => Helper::env('database')['dbprefix'],
'dbname' => Helper::env('database')['dbname'],
'dbhost' => Helper::env('database')['dbhost'],
'username' => Helper::env('database')['username'],
'password' => Helper::env('database')['password']
'dbtype' => env('database')['dbtype'],
'dbprefix' => env('database')['dbprefix'],
'dbname' => env('database')['dbname'],
'dbhost' => env('database')['dbhost'],
'username' => env('database')['username'],
'password' => env('database')['password']
]
];
26 changes: 12 additions & 14 deletions config/nosql.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,42 @@
* nosql相关配置
*/

use Framework\Helper;

return [
// 需要提供支持的nosql种类
// 参数示例:redis,memcached,mongoDB
'nosql' => Helper::env('nosql')['support'],
'nosql' => env('nosql')['support'],

// redis
'redis' => [
// 默认host
'host' => Helper::env('redis')['host'],
'host' => env('redis')['host'],
// 默认端口
'port' => Helper::env('redis')['port'],
'port' => env('redis')['port'],
// 密码
'password' => Helper::env('redis')['password'],
'password' => env('redis')['password'],
],

// memcached
'memcached' => [
// 默认host
'host' => Helper::env('memcached')['host'],
'host' => env('memcached')['host'],
// 默认端口
'port' => Helper::env('memcached')['port'],
'port' => env('memcached')['port'],
// 密码
'password' => Helper::env('memcached')['password'],
'password' => env('memcached')['password'],
],

// mongoDB
'mongoDB' => [
// 默认host
'host' => Helper::env('mongoDB')['host'],
'host' => env('mongoDB')['host'],
// 默认端口
'port' => Helper::env('mongoDB')['port'],
'port' => env('mongoDB')['port'],
// 数据库名称
'database' => Helper::env('mongoDB')['database'],
'database' => env('mongoDB')['database'],
// 用户名
'username' => Helper::env('mongoDB')['username'],
'username' => env('mongoDB')['username'],
// 密码
'password' => Helper::env('mongoDB')['password'],
'password' => env('mongoDB')['password'],
]
];
84 changes: 0 additions & 84 deletions framework/Helper.php

This file was deleted.

9 changes: 7 additions & 2 deletions framework/handles/ConfigHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,16 @@ public function __set($name = '', $value = '')
*/
public function register(App $app)
{
// load helper function file
require($app->rootPath . '/framework/helper.php');

$this->app = $app;
$app::$container->setSingle('config', $this);
$this->loadConfig($app);
// 加载时区
date_default_timezone_set($this->config['default_timezone']);

// 设置时区
// define time zone
date_default_timezone_set($this->config['default_timezone']);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions framework/handles/LogHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Framework\App;
use Framework\Handles\Handle;
use Framework\Helper;
use Framework\Exceptions\CoreHttpException;

/**
Expand Down Expand Up @@ -64,7 +63,7 @@ public function __construct()
*
* check log path env config
*/
$this->logPath = Helper::env('log_path');
$this->logPath = env('log_path');
if (empty($this->logPath) || ! isset($this->logPath['path'])) {
throw new CoreHttpException(400, 'log path is not defined');
}
Expand All @@ -90,7 +89,7 @@ public function __construct()
*/
public function write($data = [])
{
Helper::log(
easy_log(
$data,
$this->logPath . $this->logFileName
);
Expand Down
71 changes: 71 additions & 0 deletions framework/helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/********************************************
* Easy PHP *
* *
* A lightweight PHP framework for studying *
* *
* TIERGB *
* <https://github.com/TIGERB> *
* *
********************************************/

/**
* 框架助手函数文件
*
* 提供高效便捷的框架函数
*
* Framework's helper function file
*
*/

use Framework\App;

/**
* 获取环境参数
*
* @param string $paramName 参数名
* @return mixed
*/
function env($paramName = '')
{
// if (array_key_exists($paramName, self::$envCache)) {
// return self::$envCache[$paramName];
// }
return App::$container->getSingle('request')->env($paramName);
}

/**
* 浏览器友善的打印数据
*
* @param array $data 数据
* @return mixed
*/
function dump($data = [])
{
ob_start();
var_dump($data);
$output = ob_get_clean();
if (!extension_loaded('xdebug')) {
$output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
$output = '<pre>' . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
}
echo ($output);
return null;
}

/**
* log
*
* @param array $data log数据
* @param string $fileName log文件名 绝对路径
* @return void
*/
function easy_log($data = [], $fileName = 'debug')
{
$time = date('Y-m-d H:i:s', time());
error_log(
"[{$time}]: " . json_encode($data, JSON_UNESCAPED_UNICODE)."\n",
3,
$fileName . '.log'
);
}

0 comments on commit 408dbee

Please sign in to comment.