由於有些 open source 把 Javascript Code 分成很多個目錄,然後我又懶的一個個加入使用,所以,又偷懶寫了個小程式,用來取得指定目錄裡頭的檔案,搭配 Regular Expression 來決定哪些檔名要紀錄,哪些 file path 要捨棄。

程式碼:

<?php
function getFileList( $file_path , $filename_regexp_pattern , $skip_filepath_regexp_pattern )
{
        $_file_list = array();
        if( !file_exists( $file_path ) || !is_dir( $file_path ) )
                return $_file_list;
        if ( $handle = opendir( $file_path ) )
        {
                while (false !== ($file = readdir($handle)))
                {
                        if( $file == '.' || $file == '..' )
                                continue;
                        $target = $file_path . DIRECTORY_SEPARATOR . $file;
                        if( is_dir( $target ) )
                                foreach( getFileList( $target , $filename_regexp_pattern , $skip_filepath_regexp_pattern ) as $k => $v )
                                        $_file_list[ $v ] = $v;
                        if( !empty( $skip_filepath_regexp_pattern ) && preg_match( $skip_filepath_regexp_pattern , $target ) )
                                continue;
                        if( empty( $filename_regexp_pattern ) || preg_match( $filename_regexp_pattern , $file ) )
                                $_file_list[ $target ] = $target;
                }
                closedir($handle);
        }
        return $_file_list;
}

//print_r( getFileList( '.' , '/\.js$/is' , '/test/is' ) );
foreach( getFileList( '.' , '/\.js$/is' , '/test/is' ) as $js_file )
        echo '<script src="'.$js_file.'" type="text/javascript"></script>'."\n";
?>


changyy 發表在 痞客邦 PIXNET 留言(0) 人氣()