Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 这个MVC控制器代码是否需要重构?_Php_Model View Controller_Controller_Helpers - Fatal编程技术网

Php 这个MVC控制器代码是否需要重构?

Php 这个MVC控制器代码是否需要重构?,php,model-view-controller,controller,helpers,Php,Model View Controller,Controller,Helpers,我正在为MVC应用程序(Kohana/PHP)编写一个CSV/Excel-->MySQL导入管理器 我有一个名为“ImportManager”的控制器,它有一个名为“index”(默认)的操作,该操作在网格中显示特定目录中并准备导入的所有有效.csv和.xls文件。然后,用户可以选择要导入的文件 但是,由于.csv文件导入到一个数据库表中,而.xls文件导入到多个数据库表中,因此我需要处理这个抽象。因此,我创建了一个名为SmartImportFile的助手类,我将每个文件发送到该类,无论是.cs

我正在为MVC应用程序(Kohana/PHP)编写一个CSV/Excel-->MySQL导入管理器

我有一个名为“ImportManager”的控制器,它有一个名为“index”(默认)的操作,该操作在网格中显示特定目录中并准备导入的所有有效
.csv
.xls
文件。然后,用户可以选择要导入的文件

但是,由于
.csv
文件导入到一个数据库表中,而
.xls
文件导入到多个数据库表中,因此我需要处理这个抽象。因此,我创建了一个名为
SmartImportFile
助手类,我将每个文件发送到该类,无论是
.csv
还是
.xls
,然后我得到一个“智能”对象,要求该对象将该文件中的工作表(是一个还是多个)添加到我的集合中。以下是我在PHP代码中的操作方法:

public function action_index()
{
    $view = new View('backend/application/importmanager');

    $smart_worksheets = array();
    $raw_files = glob('/data/import/*.*');
    if (count($raw_files) > 0)
    {
        foreach ($raw_files as $raw_file)
        {
            $smart_import_file = new Backend_Application_Smartimportfile($raw_file);
            $smart_worksheets = $smart_import_file->add_smart_worksheets_to($smart_worksheets); 
        }
    }
    $view->set('smart_worksheets', $smart_worksheets);

    $this->request->response = $view;
}
SmartImportFile类如下所示:

class Backend_Application_Smartimportfile
{
    protected $file_name;
    protected $file_extension;
    protected $file_size;
    protected $when_file_copied;
    protected $file_name_without_extension;
    protected $path_info;
    protected $current_smart_worksheet = array();

    protected $smart_worksheets = array();

    public function __construct($file_name)
    {
        $this->file_name = $file_name;
        $this->file_name_without_extension = current(explode('.', basename($this->file_name)));

        $this->path_info = pathinfo($this->file_name);
        $this->when_file_copied = date('Y-m-d H:i:s', filectime($this->file_name));
        $this->file_extension = strtolower($this->path_info['extension']);
        $this->file_extension = strtolower(pathinfo($this->file_name, PATHINFO_EXTENSION));
        if(in_array($this->file_extension, array('csv','xls','xlsx')))
        {
            $this->current_smart_worksheet = array();
            $this->process_file();
        }
    }

    private function process_file()
    {
        $this->file_size = filesize($this->file_name);
        if(in_array($this->file_extension, array('xls','xlsx')))
        {
            if($this->file_size < 4000000)
            {
                $this->process_all_worksheets_of_excel_file();
            }
        }
        else if($this->file_extension == 'csv')
        {
            $this->process_csv_file();
        }

    }

    private function process_all_worksheets_of_excel_file()
    {
        $worksheet_names = Import_Driver_Excel::get_worksheet_names_as_array($this->file_name);
        if (count($worksheet_names) > 0)
        {
            foreach ($worksheet_names as $worksheet_name)
            {
                $this->current_smart_worksheet['name'] = basename($this->file_name).' ('.$worksheet_name.')';
                $this->current_smart_worksheet['kind'] = strtoupper($this->file_extension);
                $this->current_smart_worksheet['file_size'] = $this->file_size;
                $this->current_smart_worksheet['when_file_copied'] = $this->when_file_copied;
                $this->current_smart_worksheet['table_name'] = $this->file_name_without_extension.'__'.$worksheet_name;
                $this->assign_database_table_fields();
                $this->smart_worksheets[] = $this->current_smart_worksheet;
            }
        }
    }

    private function process_csv_file()
    {
        $this->current_smart_worksheet['name'] = basename($this->file_name);
        $this->current_smart_worksheet['kind'] = strtoupper($this->file_extension);
        $this->current_smart_worksheet['file_size'] = $this->file_size;
        $this->current_smart_worksheet['when_file_copied'] = $this->when_file_copied;

        $this->current_smart_worksheet['table_name'] = $this->file_name_without_extension;
        $this->assign_database_table_fields();


        $this->smart_worksheets[] = $this->current_smart_worksheet;
    }

    private function assign_database_table_fields()
    {
        $db = Database::instance('import_excel');
        $sql = "SHOW TABLE STATUS WHERE name = '".$this->current_smart_worksheet['table_name']."'";
        $result = $db->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($result))
        {
            $when_table_created = $result[0]['Create_time'];
            $when_file_copied_as_date = strtotime($this->when_file_copied);
            $when_table_created_as_date = strtotime($when_table_created);
            if($when_file_copied_as_date > $when_table_created_as_date)
            {
                $this->current_smart_worksheet['status'] = 'backend.application.import.status.needtoreimport';
            }
            else
            {
                $this->current_smart_worksheet['status'] = 'backend.application.import.status.isuptodate';
            }
            $this->current_smart_worksheet['when_table_created'] = $when_table_created;
        }
        else
        {
            $this->current_smart_worksheet['when_table_created'] = 'backend.application.import.status.tabledoesnotexist';
            $this->current_smart_worksheet['status'] = 'backend.application.import.status.needtoimport';
        }
    }

    public function add_smart_worksheets_to(Array $smart_worksheets = array())
    {
        return array_merge($smart_worksheets, $this->get_smart_worksheets());
    }

    public function get_smart_worksheets()
    {
        if ( ! is_array($this->smart_worksheets))
        {
            return array();
        }

        return $this->smart_worksheets;
    }

}
类后端\u应用程序\u Smartimportfile
{
受保护的$file\u名称;
受保护的$file\u扩展名;
受保护的$file\u大小;
复制文件时受保护的$;
受保护的$file\u name\u,不带扩展名;
受保护的$path\u信息;
受保护的$current\u smart\u工作表=数组();
受保护的$smart_工作表=数组();
公共函数构造($file\u name)
{
$this->file\u name=$file\u name;
$this->file\u name\u不带扩展名=current(分解('.',basename($this->file\u name));
$this->path\u info=pathinfo($this->file\u name);
$this->when_file_copied=date('Y-m-d H:i:s',filectime($this->file_name));
$this->file_extension=strtolower($this->path_info['extension']);
$this->file_extension=strtolower(路径信息($this->file_name,路径信息_extension));
if(在数组中($this->file_扩展名,数组('csv','xls','xlsx'))
{
$this->current_smart_工作表=array();
$this->process_file();
}
}
私有函数进程_文件()
{
$this->file\u size=filesize($this->file\u name);
if(在数组中($this->file_扩展名,数组('xls','xlsx'))
{
如果($this->file_size<4000000)
{
$this->处理excel文件()的所有工作表;
}
}
else if($this->file_extension==“csv”)
{
$this->process_csv_file();
}
}
专用函数处理\u excel\u文件()的所有\u工作表
{
$worksheet\u names=Import\u Driver\u Excel::get\u worksheet\u names\u as\u array($this->file\u name);
如果(计数($工作表名称)>0)
{
foreach($worksheet\u名称为$worksheet\u名称)
{
$this->current_smart_工作表['name']=basename($this->file_name)。'('.$sheet_name.');
$this->current\u smart\u工作表['kind']=strtoupper($this->file\u扩展名);
$this->current\u smart\u工作表['file\u size']=$this->file\u size;
$this->current_smart_工作表['when_file_copied']=$this->when_file_copied;
$this->current_smart_工作表['table_name']=$this->file_name_,不带扩展名。'''.''.$worksheet_name;
$this->assign_database_table_fields();
$this->smart\u工作表[]=$this->current\u smart\u工作表;
}
}
}
私有函数进程\u csv\u文件()
{
$this->current_smart_工作表['name']=basename($this->file_name);
$this->current\u smart\u工作表['kind']=strtoupper($this->file\u扩展名);
$this->current\u smart\u工作表['file\u size']=$this->file\u size;
$this->current_smart_工作表['when_file_copied']=$this->when_file_copied;
$this->current_smart_工作表['table_name']=$this->file_name_,不带扩展名;
$this->assign_database_table_fields();
$this->smart\u工作表[]=$this->current\u smart\u工作表;
}
私有函数分配\数据库\表\字段()
{
$db=数据库::实例('import_excel');
$sql=“显示表格状态,其中名称=”。$this->current_smart_工作表['TABLE_name']。”;
$result=$db->query(数据库::SELECT,$sql,FALSE)->as_array();
如果(计数($result))
{
$when_table_created=$result[0]['Create_time'];
$when\u file\u copied\u as\u date=strotime($this->when\u file\u copied);
$when\u table\u created\u as\u date=strottime($when\u table\u created);
if($when_file_copied_as_date>$when_table_created_as_date)
{
$this->current_smart_工作表['status']='backend.application.import.status.needtoreimport';
}
其他的
{
$this->current_smart_工作表['status']=“backend.application.import.status.isupdate”;
}
$this->current_smart_工作表['when_table_created']=$when_table_created;
}
其他的
{
$this->current_smart_工作表['when_table_created']=“backend.application.import.status.tabledoesnotexist';
$this->current_smart_工作表['status']='backend.application.import.status.needtoimport';
}
}
公共函数将\u智能\u工作表\u添加到(数组$smart\u工作表=数组())
{
返回数组\u merge($smart\u工作表,$this->get\u smart\u工作表());
}
公共功能获取智能工作表()
{
如果(!is_数组($this->smart_工作表))
{
返回数组();
}
返回$this->smart\u工作表;
}
}
在一次代码审查中我被告知,最好不要像这样有一个助手类
,而是将大部分代码保留在控制器操作方法本身中。其论点是,您应该能够查看控制器操作中的代码,并查看它的功能,而不是使用c
class Backend_Application_SmartImport {

    public function __construct( $raw_files ) {
    }

    public function process() {     
        foreach ($raw_files as $raw_file) {
            // (...)
            $oSmartImportFileInstance = $this->getSmartImportFileInstance( $smart_import_file_extension );
        }
    }   

    protected function getSmartImportFileInstance( $smart_import_file_extension ) {
        switch ( $smart_import_file_extension ) {
            case 'xml':
                return new Backend_Application_SmartImportFileXml();
            // (...)
        }
    }
}

abstract class Backend_Application_SmartImportFile {
    // common methods for importing from xml or cvs
    abstract function process();
}

class Backend_Application_SmartImportFileCVS extends Backend_Application_SmartImportFile {
    // methods specified for cvs importing
}

class Backend_Application_SmartImportFileXls extends Backend_Application_SmartImportFile {
    // methods specified for xls importing
}