Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit e10db11

Browse files
committed
some update and modify
1 parent 3a1b319 commit e10db11

File tree

9 files changed

+133
-129
lines changed

9 files changed

+133
-129
lines changed

src/collections/Config.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
/**
3-
* @referee windwalker-registry {@link https://github.com/ventoviro/windwalker-registry}
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2016/12/14
6+
* Time: 19:44
47
*/
58

69
namespace inhere\librarys\collections;

src/collections/DataCollector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
/**
3-
* @referee windwalker-registry {@link https://github.com/ventoviro/windwalker-registry}
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2016/3/14
6+
* Time: 19:44
47
*/
58

69
namespace inhere\librarys\collections;

src/collections/Local.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
<?php
3+
/**
4+
* Created by PhpStorm.
5+
* User: Inhere
6+
* Date: 2016/12/14
7+
* Time: 19:44
8+
*/
9+
10+
namespace inhere\librarys\collections;
11+
12+
/**
13+
* app local config read
14+
*
15+
* in local env file(must is 'ini' file):
16+
*
17+
* ```
18+
* env=dev
19+
* debug=true
20+
* ... ...
21+
* ```
22+
* in code:
23+
*
24+
* ```
25+
* $debug = Local::env('debug', false);// can also use function: local_env('debug', false)
26+
* $env = Local::env('env', 'pdt');
27+
* ```
28+
*/
29+
class Local
30+
{
31+
/**
32+
* app local env config
33+
* @var array
34+
*/
35+
private static $_local;
36+
37+
/**
38+
* config File
39+
* @var string
40+
*/
41+
private static $configFile = '';
42+
43+
/**********************************************************
44+
* app local env config
45+
**********************************************************/
46+
47+
/**
48+
* get local env config
49+
* @param string $name
50+
* @param mixed $default
51+
* @return mixed
52+
*/
53+
public static function env($name = null, $default = null)
54+
{
55+
// init loading ...
56+
if ( self::$_local === null ) {
57+
self::$_local = [];
58+
59+
if ( ($file = self::getConfigFile()) && is_file($file) ) {
60+
self::$_local = array_merge(self::$_local, parse_ini_file($file, true));
61+
}
62+
}
63+
64+
// get all
65+
if ( null === $name ) {
66+
return self::$_local;
67+
}
68+
69+
// get one by key name
70+
return isset(self::$_local[$name]) ? self::$_local[$name] : $default;
71+
}
72+
73+
/**
74+
* setConfigFile
75+
* @param string $file
76+
*/
77+
public static function setConfigFile($file)
78+
{
79+
if ( $file ) {
80+
self::$configFile = $file;
81+
}
82+
}
83+
84+
/**
85+
* getConfigFile
86+
* @return string
87+
*/
88+
public static function getConfigFile()
89+
{
90+
if ( !self::$configFile && defined('PROJECT_PATH')) {
91+
self::$configFile = PROJECT_PATH . '/.env';
92+
}
93+
94+
return self::$configFile;
95+
}
96+
}

src/env/Config.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/env/Server.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ public function getRoot($full = true)
142142
return dirname($this->getEntry($full));
143143
}
144144

145+
/**
146+
* get $_SERVER value
147+
* @param string $name
148+
* @param string $default
149+
* @return mixed
150+
*/
145151
public static function value($name, $default = '')
146152
{
147153
$name = strtoupper($name);
@@ -172,4 +178,4 @@ public function isUrl($str)
172178

173179
return preg_match($rule,trim($str))===1;
174180
}
175-
}// end class Server
181+
}// end class Server

src/functions.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
* function collection
44
*/
55

6+
if ( !function_exists('local_env') ) {
7+
function local_env($name = null, $default = null)
8+
{
9+
return inhere\librarys\collections\Local::env($name, $default);
10+
}
11+
}
12+
613
if ( !function_exists('html_minify') ) {
714
function html_minify($body)
815
{

src/helpers/DataHelper.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,6 @@
1414
*/
1515
abstract class DataHelper
1616
{
17-
/**
18-
* @var array
19-
*/
20-
private static $_local = [
21-
'__loaded' => false,
22-
'env' => 'pdt',
23-
];
24-
25-
/**
26-
* @param null $name
27-
* @param null $default
28-
* @param null|string $file The local config ini file
29-
* @return array|mixed|null
30-
*/
31-
public static function localEnv($name = null, $default = null, $file = null)
32-
{
33-
if ( !self::$_local['__loaded'] && $file && is_file($file) ) {
34-
self::$_local = array_merge(self::$_local, parse_ini_file($file, true));
35-
self::$_local['__loaded'] = true;
36-
}
37-
38-
// get all
39-
if ( null === $name ) {
40-
return self::$_local;
41-
}
42-
43-
// get one by key name
44-
return isset(self::$_local[$name]) ? self::$_local[$name] : $default;
45-
}
4617

4718
/**
4819
* Get a value from $_POST / $_GET

src/helpers/JsonHelper.php

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
* Date: 2016/8/10 0010
66
* Time: 00:41
77
*/
8-
98
namespace inhere\librarys\helpers;
109
use inhere\librarys\exceptions\NotFoundException;
11-
1210
/**
1311
* Class JsonHelper
1412
* @package inhere\librarys\helpers
@@ -23,42 +21,32 @@ class JsonHelper
2321
public static function loadFile($file, $toArray=true)
2422
{
2523
if (!file_exists($file)) {
26-
throw new NotFoundException("没有找到或不存在资源文件{$file}");
24+
throw new NotFoundException("没有找到或不存在资源文件{$file}");
2725
}
28-
2926
$data = file_get_contents($file);
30-
3127
if ( !$data ) {
3228
return null;
3329
}
34-
3530
$data = preg_replace(array(
36-
37-
// 去掉所有多行注释/* .... */
31+
// 去掉所有多行注释/* .... */
3832
'/\/\*.*?\*\/\s*/is',
39-
40-
// 去掉所有单行注释//....
33+
// 去掉所有单行注释//....
4134
'/\/\/.*?[\r\n]/is',
42-
43-
// 去掉空白
35+
// 去掉空白
4436
'/(?!\w)\s*?(?!\w)/is'
45-
4637
), array('','',' '), $data);
47-
4838
if ($toArray) {
4939
return json_decode($data, true);
5040
}
51-
5241
return $data;
5342
}
54-
5543
/**
56-
* @param string $input 文件 或 数据
57-
* @param bool $output 是否输出到文件, 默认返回格式化的数据
58-
* @param array $options 当 $output=true,此选项有效
44+
* @param string $input 文件 或 数据
45+
* @param bool $output 是否输出到文件, 默认返回格式化的数据
46+
* @param array $options 当 $output=true,此选项有效
5947
* $options = [
60-
* 'type' => 'min' // 输出数据类型 min 压缩过的 raw 正常的
61-
* 'file' => 'xx.json' // 输出文件路径;仅是文件名,则会取输入路径
48+
* 'type' => 'min' // 输出数据类型 min 压缩过的 raw 正常的
49+
* 'file' => 'xx.json' // 输出文件路径;仅是文件名,则会取输入路径
6250
* ]
6351
* @return string | bool
6452
*/
@@ -67,50 +55,36 @@ public static function json($input, $output=false, array $options=[])
6755
if (!is_string($input)) {
6856
return false;
6957
}
70-
7158
$data = trim($input);
72-
7359
if ( file_exists($input) ) {
7460
$data = file_get_contents($input);
7561
}
76-
7762
if ( !$data ) {
7863
return false;
7964
}
80-
8165
$data = preg_replace(array(
82-
83-
// 去掉所有多行注释/* .... */
66+
// 去掉所有多行注释/* .... */
8467
'/\/\*.*?\*\/\s*/is',
85-
86-
// 去掉所有单行注释//....
68+
// 去掉所有单行注释//....
8769
'/\/\/.*?[\r\n]/is',
88-
89-
// 去掉空白行
70+
// 去掉空白行
9071
"/(\n[\r])+/is"
91-
9272
), array('','',"\n"), $data);
93-
9473
if (!$output) {
9574
return $data;
9675
}
97-
9876
$default = [ 'type' => 'min' ];
9977
$options = array_merge($default, $options);
100-
10178
if ( file_exists($input) && (empty($options['file']) || !is_file($options['file']) ) )
10279
{
10380
$dir = dirname($input);
10481
$name = basename($input, '.json');
10582
$file = $dir . '/' . $name . '.' . $options['type'].'.json';
10683
$options['file'] = $file;
10784
}
108-
10985
static::saveAs($data, $options['file'], $options['type']);
110-
11186
return $data;
11287
}
113-
11488
/**
11589
* @param $data
11690
* @param $output
@@ -120,22 +94,16 @@ public static function saveAs($data, $output, array $options = [])
12094
{
12195
$default = [ 'type' => 'min', 'file' => '' ];
12296
$options = array_merge($default, $options);
123-
12497
$dir = dirname($output);
125-
12698
if ( !file_exists($dir) ) {
127-
trigger_error('设置的json文件输出'.$dir.'目录不存在!');
99+
trigger_error('设置的json文件输出'.$dir.'目录不存在!');
128100
}
129-
130101
$name = basename($output, '.json');
131102
$file = $dir . '/' . $name . '.' . $options['type'].'.json';
132-
133103
if ( $options['type '] === 'min' ) {
134-
// 去掉空白
104+
// 去掉空白
135105
$data = preg_replace('/(?!\w)\s*?(?!\w)/i', '',$data);
136106
}
137-
138107
@file_put_contents($file, $data);
139-
140108
}
141109
}

src/helpers/StrHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ public static function formatBytes($size, $precision = 2)
578578
}
579579

580580
$base = log($size) / log(1024);
581-
$suffixes = array('', 'k', 'M', 'G', 'T');
581+
$suffixes = array('b', 'k', 'M', 'G', 'T');
582582
$floorBase = floor($base);
583583

584584
return round(pow(1024, $base - $floorBase), $precision).$suffixes[(int)$floorBase];

0 commit comments

Comments
 (0)