Skip to content

Commit

Permalink
增加一个上传组件
Browse files Browse the repository at this point in the history
  • Loading branch information
tourze committed Sep 30, 2015
1 parent a85c578 commit d734deb
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 10 deletions.
7 changes: 7 additions & 0 deletions config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,12 @@
'call' => [
],
],
'upload' => [
'class' => 'tourze\Base\Component\Upload',
'params' => [
],
'call' => [
],
],
],
];
11 changes: 11 additions & 0 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ public static function getMail()
return self::get('mail');
}

/**
* 获取上传组件
*
* @return \tourze\Base\Component\UploadInterface
* @throws \tourze\Base\Exception\ComponentNotFoundException
*/
public static function getUpload()
{
return self::get('upload');
}

/**
* @var string 时区
*/
Expand Down
16 changes: 6 additions & 10 deletions src/Component/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @package tourze\Base\Component
*/
class Http extends Component
class Http extends Component implements HttpInterface
{

// HTTP方法列表
Expand Down Expand Up @@ -240,7 +240,6 @@ class Http extends Component
// Informational 1xx
self::CONTINUES => 'Continue',
self::SWITCHING_PROTOCOLS => 'Switching Protocols',

// Success 2xx
self::OK => 'OK',
self::CREATED => 'Created',
Expand All @@ -249,7 +248,6 @@ class Http extends Component
self::NO_CONTENT => 'No Content',
self::RESET_CONTENT => 'Reset Content',
self::PARTIAL_CONTENT => 'Partial Content',

// Redirection 3xx
self::MULTIPLE_CHOICES => 'Multiple Choices',
self::MOVED_PERMANENTLY => 'Moved Permanently',
Expand All @@ -258,9 +256,8 @@ class Http extends Component
self::SEE_OTHER => 'See Other',
self::NOT_MODIFIED => 'Not Modified',
self::USE_PROXY => 'Use Proxy',
// 306 is deprecated but reserved
// 306依然保留
self::TEMPORARY_REDIRECT => 'Temporary Redirect',

// Client Error 4xx
self::BAD_REQUEST => 'Bad Request',
self::UNAUTHORIZED => 'Unauthorized',
Expand All @@ -280,15 +277,14 @@ class Http extends Component
self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type',
self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable',
self::EXPECTATION_FAILED => 'Expectation Failed',

// Server Error 5xx
self::INTERNAL_SERVER_ERROR => 'Internal Server Error',
self::NOT_IMPLEMENTED => 'Not Implemented',
self::BAD_GATEWAY => 'Bad Gateway',
self::SERVICE_UNAVAILABLE => 'Service Unavailable',
self::GATEWAY_TIMEOUT => 'Gateway Timeout',
self::HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version Not Supported',
self::BANDWIDTH_LIMIT_EXCEEDED => 'Bandwidth Limit Exceeded'
self::BANDWIDTH_LIMIT_EXCEEDED => 'Bandwidth Limit Exceeded',
];

/**
Expand Down Expand Up @@ -412,7 +408,7 @@ public function setCookie($name, $value = '', $maxAge = 0, $path = '', $domain =
'path' => $path,
'domain' => $domain,
'secure' => $secure,
'http_only' => $httpOnly
'http_only' => $httpOnly,
]);
return setcookie($name, $value, $maxAge, $path, $domain, $secure, $httpOnly);
}
Expand Down Expand Up @@ -467,7 +463,7 @@ public function header($string, $replace = true, $httpResponseCode = null)
Base::getLog()->debug(__METHOD__ . ' response header', [
'header' => $string,
'replace' => $replace,
'code' => $httpResponseCode
'code' => $httpResponseCode,
]);
header($string, $replace, $httpResponseCode);
}
Expand All @@ -478,7 +474,7 @@ public function header($string, $replace = true, $httpResponseCode = null)
public function headerRemove($name = null)
{
Base::getLog()->debug(__METHOD__ . ' remove header', [
'name' => $name,
'name' => $name,
]);
header_remove($name);
}
Expand Down
48 changes: 48 additions & 0 deletions src/Component/Upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace tourze\Base\Component;

use tourze\Base\Component;
use tourze\Base\Component\Upload\Entity;
use tourze\Base\Helper\Arr;

/**
* 上传处理
*
* @package tourze\Base\Component
*/
class Upload extends Component implements UploadInterface
{

/**
* {@inheritdoc}
*/
public $persistence = false;

/**
* {@inheritdoc}
*/
public function get($name)
{
if ( ! isset($_FILES[$name]))
{
return false;
}

return new Entity([
'name' => Arr::get($_FILES[$name], 'name'),
'type' => Arr::get($_FILES[$name], 'type'),
'path' => Arr::get($_FILES[$name], 'tmp_name'),
'error' => Arr::get($_FILES[$name], 'error', 0),
'size' => Arr::get($_FILES[$name], 'size', 0),
]);
}

/**
* {@inheritdoc}
*/
public function all()
{
return array_keys($_FILES);
}
}
39 changes: 39 additions & 0 deletions src/Component/Upload/Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace tourze\Base\Component\Upload;

use tourze\Base\Object;

/**
* 单个上传文件的实例
*
* @package tourze\Base\Component\Upload
*/
class Entity extends Object
{

/**
* @var string 文件名
*/
public $name;

/**
* @var string 文件类型
*/
public $type;

/**
* @var string 文件路径
*/
public $path;

/**
* @var int 错误?
*/
public $error = 0;

/**
* @var int 文件字节数
*/
public $size = 0;
}
30 changes: 30 additions & 0 deletions src/Component/UploadInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace tourze\Base\Component;

use tourze\Base\Component\Upload\Entity;
use tourze\Base\ComponentInterface;

/**
* 上传处理接口
*
* @package tourze\Base\Component
*/
interface UploadInterface extends ComponentInterface
{

/**
* 获取指定key的上传文件
*
* @param string $name
* @return bool|Entity
*/
public function get($name);

/**
* 获取所有已经上传的文件
*
* @return array
*/
public function all();
}

0 comments on commit d734deb

Please sign in to comment.