add processFactory
This commit is contained in:
parent
bdea675003
commit
045b849dbf
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
namespace Westdc\Dataimport;
|
||||
|
||||
class Upload{
|
||||
class File{
|
||||
|
||||
private $config;
|
||||
|
||||
|
@ -10,8 +10,24 @@ class Upload{
|
|||
$this->config = \Zend_Registry::get('config');
|
||||
}
|
||||
|
||||
//上传數據
|
||||
public function __invoke($file)
|
||||
/**
|
||||
* 獲取文件存儲目錄
|
||||
* @return string
|
||||
*/
|
||||
private function getSavePath()
|
||||
{
|
||||
if(!preg_match("/(\\/|\\\)$/",$this->config->dataimport->path))
|
||||
return $this->config->dataimport->path . DIRECTORY_SEPARATOR;
|
||||
else
|
||||
return $this->config->dataimport->path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上傳文件
|
||||
* @param $file
|
||||
* @return array
|
||||
*/
|
||||
public function upload($file)
|
||||
{
|
||||
try{
|
||||
if (empty($file) !== false) {
|
||||
|
@ -30,12 +46,7 @@ class Upload{
|
|||
$ext = $this->getFileTextExt($file['name']);
|
||||
$filename = $this->gen_uuid().".".$ext;
|
||||
|
||||
if(!preg_match("/(\\/|\\\)$/",$this->config->dataimport->path))
|
||||
$save_path = $this->config->dataimport->path . DIRECTORY_SEPARATOR;
|
||||
else
|
||||
$save_path = $this->config->dataimport->path;
|
||||
|
||||
$new_filepath = $save_path.$filename;
|
||||
$new_filepath = $this->getSavePath().$filename;
|
||||
|
||||
if (move_uploaded_file($file['tmp_name'], $new_filepath) === false) {
|
||||
return array("error"=>"上传失败,请重试");
|
||||
|
@ -43,16 +54,20 @@ class Upload{
|
|||
|
||||
return array(
|
||||
"success" => 1,
|
||||
"file"=>$new_filepath
|
||||
"file"=>$filename
|
||||
);
|
||||
|
||||
}catch(Exception $e)
|
||||
{
|
||||
return array("error"=>$e->getMessage());
|
||||
}
|
||||
}
|
||||
}//upload()
|
||||
|
||||
//获取文件扩展名
|
||||
/**
|
||||
* 获取文件扩展名
|
||||
* @param $file_name
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function getFileTextExt($file_name)
|
||||
{
|
||||
$temp_arr = explode(".", $file_name);
|
||||
|
@ -62,7 +77,11 @@ class Upload{
|
|||
return $file_ext;
|
||||
}
|
||||
|
||||
//获取文件Mime,通过finfo扩展
|
||||
/**
|
||||
* 獲取文件Mime
|
||||
* @param $file_name
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileMime($file_name)
|
||||
{
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
|
@ -71,7 +90,10 @@ class Upload{
|
|||
return $filetype;
|
||||
}
|
||||
|
||||
//文件名uuid
|
||||
/**
|
||||
* 生成文件名UUID
|
||||
* @return string
|
||||
*/
|
||||
public function gen_uuid() {
|
||||
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||||
// 32 bits for "time_low"
|
||||
|
@ -94,4 +116,64 @@ class Upload{
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 獲得已經上傳的文件
|
||||
* @return array
|
||||
*/
|
||||
public function getUploadFiles()
|
||||
{
|
||||
return $this->fetchDir($this->getSavePath());
|
||||
}
|
||||
|
||||
/**
|
||||
* 掃描文件夾
|
||||
* @param $directory
|
||||
* @return array
|
||||
*/
|
||||
public function fetchDir($directory){
|
||||
$mydir = dir($directory);
|
||||
|
||||
$files = [];
|
||||
|
||||
while($file = $mydir->read())
|
||||
{
|
||||
if($file == '.' || $file == "..")
|
||||
continue;
|
||||
|
||||
if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!=".."))
|
||||
{
|
||||
$files = array_merge($files,$this->fetchDir("$directory/$file"));
|
||||
}
|
||||
else
|
||||
$files[] = $file;
|
||||
}
|
||||
|
||||
$mydir->close();
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* 刪除文件
|
||||
* @param $file
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteFile($file)
|
||||
{
|
||||
if(empty($file))
|
||||
return false;
|
||||
|
||||
return unlink($this->getSavePath() . $file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 獲取文件真實路徑
|
||||
* @param $file
|
||||
* @return string
|
||||
*/
|
||||
public function getRealName($file)
|
||||
{
|
||||
return $this->getSavePath().$file;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: liujin834
|
||||
* Date: 15-1-27
|
||||
* Time: 下午2:37
|
||||
*/
|
||||
|
||||
namespace Westdc\Dataimport;
|
||||
|
||||
|
||||
final class ProcessFactory {
|
||||
|
||||
protected static $instance = null;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static function Bootstrap($fileExt)
|
||||
{
|
||||
$namespace_root = "\\Westdc\\Dataimport\\Processing\\";
|
||||
|
||||
$recordType = ucfirst(strtolower($fileExt));
|
||||
|
||||
$_class = $namespace_root . $recordType;
|
||||
|
||||
if(!class_exists($_class))
|
||||
throw new \RuntimeException("未知的數據文件");
|
||||
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new $_class;
|
||||
}
|
||||
return self::$instance;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,124 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: liujin834
|
||||
* Date: 15-1-27
|
||||
* Time: 下午3:12
|
||||
*/
|
||||
namespace Westdc\Dataimport\Processing;
|
||||
|
||||
use Westdc\Dataimport\ProcessingInterface;
|
||||
use League\Csv\Reader;
|
||||
|
||||
class Csv implements ProcessingInterface{
|
||||
|
||||
private $source_file;
|
||||
private $csv_object;
|
||||
private $csv_line_data;
|
||||
|
||||
const ERROR_INDEX_NOT_MATCHED = "值的個數於其它行不匹配";
|
||||
const ERROR_INDEX_TYPE_DIFFERENT = "值的類型於上一行的類型不同";
|
||||
const ERROR_LINE_TITLE = "行:";
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @param $file
|
||||
*/
|
||||
public function init($file)
|
||||
{
|
||||
$this->source_file = $file;
|
||||
$this->csv_object = Reader::createFromPath($this->source_file);
|
||||
$this->csv_object->setDelimiter(',');
|
||||
$this->csv_object->setEncodingFrom("utf-8");
|
||||
}
|
||||
|
||||
/**
|
||||
* 獲取總行數
|
||||
* @return int
|
||||
*/
|
||||
public function getLineCount(){
|
||||
return count($this->csv_object->fetchAll());
|
||||
}//getLineCount()
|
||||
|
||||
/**
|
||||
* 獲取文件大小
|
||||
* @return int
|
||||
*/
|
||||
public function getSize(){
|
||||
|
||||
$size = filesize($this->source_file);
|
||||
|
||||
$type = array('Bytes','KB','MB','GB','TB');
|
||||
for($i = 0; $size >= 1024; $i++)//单位每增大1024,则单位数组向后移动一位表示相应的单位
|
||||
{
|
||||
$size/=1024;
|
||||
}
|
||||
return (floor($size*100)/100).$type[$i];//floor是取整函数,为了防止出现一串的小数,这里取了两位小数
|
||||
}//getSize()
|
||||
|
||||
/**
|
||||
* 獲取文件類型
|
||||
* @return mixed
|
||||
*/
|
||||
public function getType(){
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$filetype = finfo_file($finfo, $this->source_file) ; //文件mime类型
|
||||
finfo_close($finfo);
|
||||
return $filetype;
|
||||
}//getType()
|
||||
|
||||
/**
|
||||
* 獲取內容
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLines()
|
||||
{
|
||||
return $this->csv_object->fetchAll();
|
||||
}//getLines()
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getHtmlTable()
|
||||
{
|
||||
return $this->csv_object->toHTML('table-csv-data with-header');
|
||||
}//getTable()
|
||||
|
||||
/**
|
||||
* 檢查文件內容是否規則
|
||||
* @return array|bool
|
||||
*/
|
||||
public function checkRegularity()
|
||||
{
|
||||
$lines = $this->getLines();
|
||||
|
||||
$error_stack = [];
|
||||
|
||||
foreach($lines as $k=>$v){
|
||||
if($k < 1 || empty($v))
|
||||
continue;
|
||||
|
||||
if(count($v) != count($lines[$k-1]))
|
||||
{
|
||||
$error_stack[] = self::ERROR_LINE_TITLE . ($k + 1) .":".self::ERROR_INDEX_NOT_MATCHED;
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach($v as $vIndex => $item){
|
||||
if(gettype($item) != gettype($lines[$k-1][$vIndex]))
|
||||
{
|
||||
$error_stack[] = self::ERROR_LINE_TITLE . ($k + 1) .":".self::ERROR_INDEX_TYPE_DIFFERENT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(count($error_stack) < 1)
|
||||
return true;
|
||||
else
|
||||
return $error_stack;
|
||||
|
||||
}//checkRegularity()
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: liujin834
|
||||
* Date: 15-1-27
|
||||
* Time: 下午3:10
|
||||
*/
|
||||
|
||||
namespace Westdc\Dataimport;
|
||||
|
||||
interface ProcessingInterface {
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @param $file
|
||||
*/
|
||||
public function init($file);
|
||||
|
||||
}
|
Loading…
Reference in New Issue