55 * Date: 2016/8/10 0010
66 * Time: 00:41
77 */
8-
98namespace inhere \librarys \helpers ;
109use 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}
0 commit comments