Skip to content

Commit

Permalink
Merge pull request #44 from ipuppet/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ipuppet committed Dec 20, 2021
2 parents 7eab47a + 797c877 commit e85f433
Show file tree
Hide file tree
Showing 55 changed files with 532 additions and 1,615 deletions.
9 changes: 2 additions & 7 deletions src/Component/DatabaseDriver/PdoDatabaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PdoDatabaseDriver
/**
* @var PDO
*/
private PDO $pdo;
public PDO $pdo;

/**
* @var LoggerInterface
Expand All @@ -29,7 +29,7 @@ public function __construct(LoggerInterface $logger, array $config = [])
{
$this->logger = $logger;
try {
//database默认值mysql
//database 默认值 mysql
$config['database'] = $config['database'] ?? 'mysql';
$this->pdo = new PDO(
sprintf(
Expand Down Expand Up @@ -117,9 +117,4 @@ public function rollback(): bool
{
return $this->pdo->rollback();
}

public function getRealPdo(): PDO
{
return $this->pdo;
}
}
95 changes: 94 additions & 1 deletion src/Component/Http/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,100 @@

class File
{
public function __construct(array $files)
private string $name;
private string $tmpName;
private string $type;
private int $error;
private int $size;

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* @param string $name
* @return $this
*/
public function setName(string $name): self
{
// 不可包含空白字符
$this->name = (string)preg_replace('/( | |\s)*/', '', $name);
return $this;
}

/**
* @return string
*/
public function getTmpName(): string
{
return $this->tmpName;
}

/**
* @param string $tmpName
* @return $this
*/
public function setTmpName(string $tmpName): self
{
$this->tmpName = $tmpName;
return $this;
}

/**
* @return string
*/
public function getType(): string
{
return $this->type;
}

/**
* @param string $type
* @return $this
*/
public function setType(string $type): self
{
$this->type = $type;
return $this;
}

/**
* @return int
*/
public function getError(): int
{
return $this->error;
}

/**
* @param int $error
* @return $this
*/
public function setError(int $error): self
{
$this->error = $error;
return $this;
}

/**
* @return int
*/
public function getSize(): int
{
return $this->size;
}

/**
* @param int $size
* @return $this
*/
public function setSize(int $size): self
{
$this->size = $size;
return $this;
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
<?php


namespace Ipuppet\Jade\Plugins\FileHelper;
namespace Ipuppet\Jade\Component\Http;


use Ipuppet\Jade\Foundation\Path\Path;
use Ipuppet\Jade\Component\Path\Path;

class Upload
class FileUploader
{
// 自身实例
private static ?self $instance = null;
private string $key = 'file';
// 存放文件的路径
private ?Path $path = null;
// 单位MB
private ?Path $path;
// 单位 MB
private int $maxSize = 5;
// TODO 格式化文件名
// private string $formatName;
// 如果目录不存在,是否创建目录
private bool $ifCreatDir = false;
private bool $autoCreatDir = false;
// 错误信息
private string $errMsg;
// 允许上传的文件类型
Expand All @@ -32,11 +29,11 @@ class Upload
private array $failed = [];

/**
* @var File[]
* @var Files
*/
private array $files = [];
private Files $files;

private function __construct($options = null)
public function __construct($options = null)
{
// 载入设置
if (null !== $options) $this->setOptions($options);
Expand All @@ -56,73 +53,41 @@ public function setOptions(array $options)
}
}

public static function getInstance($options = null): self
{
if (null == self::$instance)
self::$instance = new self($options);
return self::$instance;
}

/**
* 获取设置
* @param $key
* @return mixed
*/
public function getAttr($key): mixed
public function getOption($key): mixed
{
return $this->$key;
}

/**
* 上传文件
*/
public function uploadFiles(): void
public function upload(): void
{
// 获取文件信息
$this->getFileInfo();
// 循环移动文件
foreach ($this->files as $file) {
if ($this->check($file)) {
$moveState = move_uploaded_file($file->getTmpName(), $this->path . $file->getName());
if ($moveState) {
$this->success[] = $file;
foreach ($this->files->toArray() as $files) {
foreach ($files as $file) {
if ($this->check($file)) {
$moveState = move_uploaded_file($file->getTmpName(), $this->path . $file->getName());
if ($moveState) {
$this->success[] = $file;
} else {
$this->failed[] = [
'errMsg' => "File '{$file->getName()}' move failed.",
'file' => $file
];
}
} else {
$this->failed[] = [
'errMsg' => "File '{$file->getName()}' move failed.",
'errMsg' => $this->errMsg,
'file' => $file
];
}
} else {
$this->failed[] = [
'errMsg' => $this->errMsg,
'file' => $file
];
}
}
}

/**
* 获取文件信息
*/
private function getFileInfo()
{
if (is_array($_FILES[$this->key]['name'])) {
$len = count($_FILES[$this->key]['name']);
for ($i = 0; $i < $len; $i++) {
$file = new File();
$this->files[] = $file->setName($_FILES[$this->key]['name'][$i])
->setSize($_FILES[$this->key]['size'][$i])
->setTmpName($_FILES[$this->key]['tmp_name'][$i])
->setType($_FILES[$this->key]['type'][$i])
->setError($_FILES[$this->key]['error'][$i]);
}
} else {
$file = new File();
$this->files[] = $file->setName($_FILES[$this->key]['name'])
->setSize($_FILES[$this->key]['size'])
->setTmpName($_FILES[$this->key]['tmp_name'])
->setType($_FILES[$this->key]['type'])
->setError($_FILES[$this->key]['error']);
}
}

Expand All @@ -133,21 +98,21 @@ private function getFileInfo()
*/
private function check(File $file): bool
{
if ($this->path === null) {
if (!isset($this->path)) {
$this->errMsg = '是否忘记设定路径?';
return false;
}
// 判断是否含有中文
if (preg_match("/[\x7f-\xff]/", $file->getName())) {
$this->errMsg = "不能含有中文";
return false;
}
// 检查目录是否存在,如果不存在是否需要创建
if ($this->ifCreatDir) {
if ($this->autoCreatDir) {
if (!is_dir($this->path))
mkdir($this->path, 0777, true);
} elseif (!is_dir($this->path)) {
$this->errMsg = "目录'$this->path'不存在,如需创建请设置ifCreatDir属性为true";
$this->errMsg = "目录 '$this->path' 不存在,如需创建请设置 autoCreatDir 为 true";
return false;
}
// 判断是否含有中文
if (preg_match("/[\x7f-\xff]/", $file->getName())) {
$this->errMsg = "不能含有中文";
return false;
}
// 检查是否存在上传错误
Expand All @@ -161,11 +126,11 @@ private function check(File $file): bool
return false;
}
if ($file->getSize() > $this->maxSize * 1024 * 1024) {
$this->errMsg = "文件大小不得超过{$this->maxSize}MB!";
$this->errMsg = "文件大小不得超过 {$this->maxSize}MB!";
return false;
}
if (!in_array($file->getType(), $this->allowType)) {
$this->errMsg = "文件类型'{$file->getType()}'不符合预期";
$this->errMsg = "文件类型 '{$file->getType()}' 不符合预期";
return false;
}
return true;
Expand Down
46 changes: 46 additions & 0 deletions src/Component/Http/Files.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php


namespace Ipuppet\Jade\Component\Http;


use Ipuppet\Jade\Component\Parameter\Parameter;
use Ipuppet\Jade\Component\Parameter\ParameterInterface;

class Files extends Parameter implements ParameterInterface
{
/**
* 文件列表
* 每个值都是 包含该 form 字段上传的所有文件 的数组
* @var array
*/
protected array $parameters;

/**
* @param array $files $_FILE
*/
public function __construct(array $files)
{
foreach ($files as $key => $file) {
$this->parameters[$key] = [];
if (is_array($file['name'])) { // 单个 form 字段包含多个文件
$len = count($file['name']);
for ($i = 0; $i < $len; $i++) {
$fileInstance = new File();
$this->parameters[$key][] = $fileInstance->setName($file['name'][$i])
->setSize($file['size'][$i])
->setTmpName($file['tmp_name'][$i])
->setType($file['type'][$i])
->setError($file['error'][$i]);
}
} else { // 单个 form 字段只有一个文件
$fileInstance = new File();
$this->parameters[$key][] = $fileInstance->setName($file['name'])
->setSize($file['size'])
->setTmpName($file['tmp_name'])
->setType($file['type'])
->setError($file['error']);
}
}
}
}
4 changes: 2 additions & 2 deletions src/Component/Http/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


use DateTime;
use Ipuppet\Jade\Foundation\Parameter\Parameter;
use Ipuppet\Jade\Foundation\Parameter\ParameterInterface;
use Ipuppet\Jade\Component\Parameter\Parameter;
use Ipuppet\Jade\Component\Parameter\ParameterInterface;
use RuntimeException;

class Header extends Parameter implements ParameterInterface
Expand Down

0 comments on commit e85f433

Please sign in to comment.