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

Commit 6f0f105

Browse files
author
xiajianjun
committed
update
1 parent 2a24f4f commit 6f0f105

File tree

4 files changed

+54
-38
lines changed

4 files changed

+54
-38
lines changed

src/env/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,10 @@ protected function _userAgentCheck()
262262
} elseif (strpos('linux', $agent)) {
263263

264264
if (strpos('android', $agent)) {
265-
$os = 'Android';
265+
$os = 'Android';
266266
$this->set('isAndroid', true);
267267
} else {
268-
$os = 'Linux';
268+
$os = 'Linux';
269269
$this->set('isLinux', true);
270270
}
271271

src/files/Directory.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,12 @@ static public function getFiles($dirName, $ext=null, $recursive=0, &$list=[])
147147
* 支持层级目录的创建
148148
* @param $path
149149
* @param int|string $mode
150+
* @param bool $recursive
150151
* @return bool
151152
*/
152-
static public function create($path, $mode=0664)
153+
static public function create($path, $mode=0664, $recursive = true)
153154
{
154-
return is_dir($path) || mkdir($path, $mode, true);
155+
return is_dir($path) || mkdir($path, $mode, $recursive);
155156
}
156157

157158
/**
@@ -200,7 +201,7 @@ static public function copy($oldDir, $newDir)
200201
* 删除目录及里面的文件
201202
* @param $dirName
202203
* @param boolean $type [description]
203-
* @return bool [type] [description]
204+
* @return bool
204205
*/
205206
static public function delete($dirName,$type=true)
206207
{

src/files/File.php

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace inhere\tools\files;
1212

1313
use inhere\tools\helpers\StrHelper;
14+
use inhere\tools\exceptions\FileSystemException;
1415

1516
/**
1617
* Class File
@@ -53,7 +54,6 @@ public static function getInfo($filename, $check=true)
5354
'update_time' => filectime($filename), //修改时间
5455
'last_visit_time' => fileatime($filename), //文件的上次访问时间
5556
];
56-
5757
}
5858

5959
/**
@@ -93,8 +93,7 @@ public static function save($data, $filename )
9393
**/
9494
public static function createAndWrite(array $fileData = [],$append=false,$mode=0664)
9595
{
96-
foreach($fileData as $file=>$content)
97-
{
96+
foreach($fileData as $file=>$content) {
9897
$dir = dirname($file); //文件所在目录
9998

10099
//检查目录是否存在,不存在就先创建(多级)目录
@@ -113,7 +112,7 @@ public static function createAndWrite(array $fileData = [],$append=false,$mode=0
113112
file_put_contents($file,$content,FILE_APPEND | LOCK_EX);
114113
@chmod($file,$mode);
115114
} else {
116-
\Trigger::notice('目录'.$dir.'下:'.$fileName." 文件已存在!将跳过".$fileName."的创建") ;
115+
// \Trigger::notice('目录'.$dir.'下:'.$fileName." 文件已存在!将跳过".$fileName."的创建") ;
117116
continue;
118117
}
119118

@@ -125,60 +124,78 @@ public static function createAndWrite(array $fileData = [],$append=false,$mode=0
125124
/**
126125
* @param $url
127126
* @param bool|false $use_include_path
128-
* @param null $stream_context
127+
* @param null $streamContext
129128
* @param int $curl_timeout
130129
* @return bool|mixed|string
131130
*/
132-
public static function file_get_contents($url, $use_include_path = false, $stream_context = null, $curl_timeout = 5)
131+
public static function get($url, $use_include_path = false, $streamContext = null, $curl_timeout = 5)
133132
{
134-
if ($stream_context == null && preg_match('/^https?:\/\//', $url))
135-
$stream_context = @stream_context_create(array('http' => array('timeout' => $curl_timeout)));
136-
if (in_array(ini_get('allow_url_fopen'), array('On', 'on', '1')) || !preg_match('/^https?:\/\//', $url))
137-
return @file_get_contents($url, $use_include_path, $stream_context);
138-
elseif (function_exists('curl_init'))
139-
{
133+
return static::file_get_contents($url, $use_include_path , $streamContext , $curl_timeout);
134+
}
135+
public static function file_get_contents($url, $use_include_path = false, $streamContext = null, $curl_timeout = 5)
136+
{
137+
if ($streamContext == null && preg_match('/^https?:\/\//', $url)) {
138+
$streamContext = @stream_context_create(array('http' => array('timeout' => $curl_timeout)));
139+
}
140+
141+
if (in_array(ini_get('allow_url_fopen'), array('On', 'on', '1')) || !preg_match('/^https?:\/\//', $url)) {
142+
return @file_get_contents($url, $use_include_path, $streamContext);
143+
} elseif (function_exists('curl_init')) {
140144
$curl = curl_init();
141145
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
142146
curl_setopt($curl, CURLOPT_URL, $url);
143147
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
144148
curl_setopt($curl, CURLOPT_TIMEOUT, $curl_timeout);
145149
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
146-
if ($stream_context != null)
147-
{
148-
$opts = stream_context_get_options($stream_context);
149-
if (isset($opts['http']['method']) && StrHelper::strtolower($opts['http']['method']) == 'post')
150-
{
150+
151+
if ($streamContext != null) {
152+
$opts = stream_context_get_options($streamContext);
153+
154+
if (isset($opts['http']['method']) && StrHelper::strtolower($opts['http']['method']) == 'post') {
151155
curl_setopt($curl, CURLOPT_POST, true);
152-
if (isset($opts['http']['content']))
153-
{
156+
157+
if (isset($opts['http']['content'])) {
154158
parse_str($opts['http']['content'], $post_data);
155159
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
156160
}
157161
}
158162
}
163+
159164
$content = curl_exec($curl);
160165
curl_close($curl);
166+
161167
return $content;
162168
}
163-
else
164-
return false;
169+
170+
return false;
165171
}
166172

167-
public static function move()
168-
{}
173+
public static function move($file, $target)
174+
{
175+
Directory::create(dirname($target));
176+
177+
if ( static::copy($file, $target) ) {
178+
unlink($file);
179+
}
180+
}
169181

170182
/**
171183
* @param $source
172184
* @param $destination
173-
* @param null $stream_context
185+
* @param null $streamContext
174186
* @return bool|int
175187
*/
176-
public static function copy($source, $destination, $stream_context = null)
188+
public static function copy($source, $destination, $streamContext = null)
177189
{
178-
if (is_null($stream_context) && !preg_match('/^https?:\/\//', $source))
179-
return @copy($source, $destination);
190+
if (is_null($streamContext) && !preg_match('/^https?:\/\//', $source)) {
191+
if (!is_file($source)) {
192+
throw new FileSystemException("file don't exists. File: $source");
193+
}
194+
195+
return copy($source, $destination);
196+
}
180197

181-
return @file_put_contents($destination, self::file_get_contents($source, false, $stream_context));
198+
return @file_put_contents($destination, self::file_get_contents($source, false, $streamContext));
182199
}
183200

184201
public static function combine($inFile, $outFile)

src/helpers/StrHelper.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ static public function length($str)
6969

7070
if (function_exists('mb_strlen')){
7171
return mb_strlen($str,'utf-8');
72-
}
73-
else {
72+
} else {
7473
preg_match_all("/./u", $str, $arr);
7574

7675
return count($arr[0]);
@@ -94,8 +93,7 @@ static public function abs_length($str)
9493
return mb_strwidth($str,'utf-8');
9594
} else if (function_exists('mb_strlen')){
9695
return mb_strlen($str,'utf-8');
97-
}
98-
else {
96+
} else {
9997
preg_match_all("/./u", $str, $ar);
10098
return count($ar[0]);
10199
}
@@ -151,7 +149,7 @@ static public function utf8_substr($str,$start=0,$end=null)
151149
* @param bool $suffix 是否加尾缀
152150
* @return string
153151
*/
154-
static public function zh_substr($str, $start=0, $length, $charset="utf-8", $suffix=true)
152+
static public function zhSubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
155153
{
156154
if (function_exists("mb_substr"))
157155
{

0 commit comments

Comments
 (0)