如何在没有硬编码的情况下区分文件名?

时间:2017-10-25 04:57:47

标签: php foreach

我有这样的配置:

$CONF['log']['TSEL_MV_GW_SERV101_MT_CONNECTOR_STATUS'] = array(
    'folder'    => $CONF['mainFolder']."/log_sample/tsel",
    'files'     => array(
        'connector_access_log*.log',
        'engine_dr_to_app_hit_dr_url*.log'
    )
);

然后我获得具有今天日期的那种模式的所有文件。

foreach($CONF['log'] as $label => $arrayData){
    $filePaths = array();
    $today = date("Ymd", time());
    foreach($arrayData['files'] as $filePrefix){    
            $glob       = glob($arrayData['folder']."/".$filePrefix, GLOB_ERR);
            if(count($glob) >= 1){  
                foreach($glob as $file){
                    if(strpos($file, $today)){
                        $filePaths[] = $file;
                    }
                }
            }
        }
     }

$ filePaths数组内容:

Array
(
  [0] =>/dirpath/connector_access_log_20171025.log
  [1] =>/dirpath/engine_dr_to_app_hit_dr_url_20171025.log
)

现在,我需要以不同的方式处理每个文件,如何使用foreach循环执行此操作?

不允许使用硬编码如if(preg_match(" connector_access_log"))。

这是我之前的做法:

foreach($filePaths as $filePath){
  if(preg_match("/connector_access_log_/", $filePath, $match)){
     #some code
  } else if (preg_match("/engine_dr_to_app_hit_dr_url_/", $filePath, $match)){
   #some code  
  }
}

2 个答案:

答案 0 :(得分:0)

您可以做的是,您可以调整配置以在assosiative array中存储文件。

$CONNECTOR_ACCESS_LOG = 'connector_access_log'
$ENGINE_URL_LOG = 'engine_dr_to_app_hit_dr_url'
$CONF['log']['TSEL_MV_GW_SERV101_MT_CONNECTOR_STATUS'] = array(
    'folder'    => $CONF['mainFolder']."/log_sample/tsel",
    'files'     => array(
        $CONNECTOR_ACCESS_LOG => 'connector_access_log*.log',
        $ENGINE_URL_LOG => 'engine_dr_to_app_hit_dr_url*.log'
    )
);

然后在循环中你可以获得这些键并在它们之间切换。例如

foreach($CONF['log'] as $label => $arrayData){
    $today = date("Ymd", time());
    foreach($arrayData['files'] as $filetype => $filePrefix){    
            $glob       = glob($arrayData['folder']."/".$filePrefix, GLOB_ERR);
            if(count($glob) >= 1){  
                foreach($glob as $file){
                    if(strpos($file, $today)){
                        switch($filetype) {
                         case $CONNECTOR_ACCESS_LOG: doConnection($file); break;
                         case $ENGINE_URL_LOG : doEngine($file); break;
                        }
                    }
                }
            }
        }
     }

请原谅我的php语法,我只想尝试一下。

答案 1 :(得分:0)

如果您真的不想使用硬编码,则应通过配置为每种文件类型传递处理程序。像大多数框架一样。有关更多示例,请参阅yii2 logger。这是一段伪代码:

$CONF['log']['TSEL_MV_GW_SERV101_MT_CONNECTOR_STATUS'] = array(
    new FirstLogger($CONF['mainFolder']."/log_sample/tsel", 'connector_access_log*.log'),
    new SecondLogger($CONF['mainFolder']."/log_sample/tsel", 'engine_dr_to_app_hit_dr_url*.log'),
);

接下来,处理程序类中的处理程序处理您的日志文件。

class FirstLogger
{
    protected $dir = null;
    protected $fileWildCard = null;

    public function __construct($dir, $fileWildcard)
    {
        $this->dir = $dir;
        $this->fileWildCard = $fileWildcard;
    }

    public function getFiles()
    {
        $filePaths = [];
        $today = date("Ymd", time());
        $glob = glob($this->dir."/".$this->fileWildCard, GLOB_ERR);
        if(count($glob) >= 1){  
            foreach($glob as $file){
                if(strpos($file, $today)){
                    $filePaths[] = $file;
                }
            }
        }
        return $filePaths;
    }

    public function handleLogs()
    {
        $files = $this->getFiles();
        //some of your logic
    }
}

接下来,您可能希望为所有记录器和一个从中继承记录器的抽象类创建一个接口。