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

Commit d734deb

Browse files
committed
增加一个上传组件
1 parent a85c578 commit d734deb

File tree

6 files changed

+141
-10
lines changed

6 files changed

+141
-10
lines changed

config/main.php

+7
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,12 @@
4545
'call' => [
4646
],
4747
],
48+
'upload' => [
49+
'class' => 'tourze\Base\Component\Upload',
50+
'params' => [
51+
],
52+
'call' => [
53+
],
54+
],
4855
],
4956
];

src/Base.php

+11
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,17 @@ public static function getMail()
236236
return self::get('mail');
237237
}
238238

239+
/**
240+
* 获取上传组件
241+
*
242+
* @return \tourze\Base\Component\UploadInterface
243+
* @throws \tourze\Base\Exception\ComponentNotFoundException
244+
*/
245+
public static function getUpload()
246+
{
247+
return self::get('upload');
248+
}
249+
239250
/**
240251
* @var string 时区
241252
*/

src/Component/Http.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @package tourze\Base\Component
1414
*/
15-
class Http extends Component
15+
class Http extends Component implements HttpInterface
1616
{
1717

1818
// HTTP方法列表
@@ -240,7 +240,6 @@ class Http extends Component
240240
// Informational 1xx
241241
self::CONTINUES => 'Continue',
242242
self::SWITCHING_PROTOCOLS => 'Switching Protocols',
243-
244243
// Success 2xx
245244
self::OK => 'OK',
246245
self::CREATED => 'Created',
@@ -249,7 +248,6 @@ class Http extends Component
249248
self::NO_CONTENT => 'No Content',
250249
self::RESET_CONTENT => 'Reset Content',
251250
self::PARTIAL_CONTENT => 'Partial Content',
252-
253251
// Redirection 3xx
254252
self::MULTIPLE_CHOICES => 'Multiple Choices',
255253
self::MOVED_PERMANENTLY => 'Moved Permanently',
@@ -258,9 +256,8 @@ class Http extends Component
258256
self::SEE_OTHER => 'See Other',
259257
self::NOT_MODIFIED => 'Not Modified',
260258
self::USE_PROXY => 'Use Proxy',
261-
// 306 is deprecated but reserved
259+
// 306依然保留
262260
self::TEMPORARY_REDIRECT => 'Temporary Redirect',
263-
264261
// Client Error 4xx
265262
self::BAD_REQUEST => 'Bad Request',
266263
self::UNAUTHORIZED => 'Unauthorized',
@@ -280,15 +277,14 @@ class Http extends Component
280277
self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type',
281278
self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable',
282279
self::EXPECTATION_FAILED => 'Expectation Failed',
283-
284280
// Server Error 5xx
285281
self::INTERNAL_SERVER_ERROR => 'Internal Server Error',
286282
self::NOT_IMPLEMENTED => 'Not Implemented',
287283
self::BAD_GATEWAY => 'Bad Gateway',
288284
self::SERVICE_UNAVAILABLE => 'Service Unavailable',
289285
self::GATEWAY_TIMEOUT => 'Gateway Timeout',
290286
self::HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version Not Supported',
291-
self::BANDWIDTH_LIMIT_EXCEEDED => 'Bandwidth Limit Exceeded'
287+
self::BANDWIDTH_LIMIT_EXCEEDED => 'Bandwidth Limit Exceeded',
292288
];
293289

294290
/**
@@ -412,7 +408,7 @@ public function setCookie($name, $value = '', $maxAge = 0, $path = '', $domain =
412408
'path' => $path,
413409
'domain' => $domain,
414410
'secure' => $secure,
415-
'http_only' => $httpOnly
411+
'http_only' => $httpOnly,
416412
]);
417413
return setcookie($name, $value, $maxAge, $path, $domain, $secure, $httpOnly);
418414
}
@@ -467,7 +463,7 @@ public function header($string, $replace = true, $httpResponseCode = null)
467463
Base::getLog()->debug(__METHOD__ . ' response header', [
468464
'header' => $string,
469465
'replace' => $replace,
470-
'code' => $httpResponseCode
466+
'code' => $httpResponseCode,
471467
]);
472468
header($string, $replace, $httpResponseCode);
473469
}
@@ -478,7 +474,7 @@ public function header($string, $replace = true, $httpResponseCode = null)
478474
public function headerRemove($name = null)
479475
{
480476
Base::getLog()->debug(__METHOD__ . ' remove header', [
481-
'name' => $name,
477+
'name' => $name,
482478
]);
483479
header_remove($name);
484480
}

src/Component/Upload.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace tourze\Base\Component;
4+
5+
use tourze\Base\Component;
6+
use tourze\Base\Component\Upload\Entity;
7+
use tourze\Base\Helper\Arr;
8+
9+
/**
10+
* 上传处理
11+
*
12+
* @package tourze\Base\Component
13+
*/
14+
class Upload extends Component implements UploadInterface
15+
{
16+
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public $persistence = false;
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function get($name)
26+
{
27+
if ( ! isset($_FILES[$name]))
28+
{
29+
return false;
30+
}
31+
32+
return new Entity([
33+
'name' => Arr::get($_FILES[$name], 'name'),
34+
'type' => Arr::get($_FILES[$name], 'type'),
35+
'path' => Arr::get($_FILES[$name], 'tmp_name'),
36+
'error' => Arr::get($_FILES[$name], 'error', 0),
37+
'size' => Arr::get($_FILES[$name], 'size', 0),
38+
]);
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function all()
45+
{
46+
return array_keys($_FILES);
47+
}
48+
}

src/Component/Upload/Entity.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace tourze\Base\Component\Upload;
4+
5+
use tourze\Base\Object;
6+
7+
/**
8+
* 单个上传文件的实例
9+
*
10+
* @package tourze\Base\Component\Upload
11+
*/
12+
class Entity extends Object
13+
{
14+
15+
/**
16+
* @var string 文件名
17+
*/
18+
public $name;
19+
20+
/**
21+
* @var string 文件类型
22+
*/
23+
public $type;
24+
25+
/**
26+
* @var string 文件路径
27+
*/
28+
public $path;
29+
30+
/**
31+
* @var int 错误?
32+
*/
33+
public $error = 0;
34+
35+
/**
36+
* @var int 文件字节数
37+
*/
38+
public $size = 0;
39+
}

src/Component/UploadInterface.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace tourze\Base\Component;
4+
5+
use tourze\Base\Component\Upload\Entity;
6+
use tourze\Base\ComponentInterface;
7+
8+
/**
9+
* 上传处理接口
10+
*
11+
* @package tourze\Base\Component
12+
*/
13+
interface UploadInterface extends ComponentInterface
14+
{
15+
16+
/**
17+
* 获取指定key的上传文件
18+
*
19+
* @param string $name
20+
* @return bool|Entity
21+
*/
22+
public function get($name);
23+
24+
/**
25+
* 获取所有已经上传的文件
26+
*
27+
* @return array
28+
*/
29+
public function all();
30+
}

0 commit comments

Comments
 (0)