CFMLでINIファイルを丸lurみする
-
06-07-2019 - |
質問
このコードを少し改善する方法はありますか? INIファイルを一気に読み、対応するデータ構造を作成したいと思います。
<cfset INIfile = expandPath(".") & "\jobs.ini">
<cfset profile = GetProfileSections(INIfile)>
<cfloop collection="#profile#" item="section">
<cfloop list="#profile[section]#" index="entry">
<cfset app.workflow[section][entry]=GetProfileString(INIfile, section, entry) >
</cfloop>
</cfloop>
解決
CFMLパワーを使用してこれを改善できるとは思わない。巨大なiniファイルを解析する必要がありますか?そうでない場合、なぜコードを改善したいのですか、私にとっては非常に簡単です。
その他の可能な(CFでは一般的ですが)解決策は、純粋なJavaを試すことです。純粋なJavaのこのSOスレッドを参照例。
PSところで、特別なパフォーマンスが必要な場合は、構成に別のストレージを使用することを検討する必要があります。古くて良いMySQLへの単純なSELECTクエリは、大規模なデータセットに対してはるかに高速です。
他のヒント
Ryberのコメントを拡張するには、代わりにこのアプローチの使用を検討してください。ネストされた暗黙的な構造表記法を使用するため、CF8.01以降を使用していると想定しています。これはCF7 / 6 / etc構文に簡単に変換できますが、それほど簡潔でも簡潔でもありません。
繰り返しますが、これは、iniファイルが他のアプリケーションや人によって使用されておらず、ini形式にする必要がない場合にのみ適用されます。
settings.cfm:
<cfset variables.settings = {
fooSection = {
fooKey = 'fooVal',
fooNumber = 2,
},
fooSection2 = {
//...
},
fooSection3 = {
//...
}
} />
Application.cfc:(onApplicationStartメソッドのみ)
<cffunction name="onApplicationStart">
<cfinclude template="settings.cfm" />
<cfset application.workflow = variables.settings />
<cfreturn true />
</cffunction>
さらに、 CFEncode アプリケーションを使用して、 settings.cfm
のコンテンツを暗号化します。ファイルのコピーを取得し、暗号化されたコンテンツが何であるかを見たい人からあなたを保護することはできません(暗号化はそれほど強力ではなく、コンテンツを解読せずに見る方法があります)せんさく好きな人を排除したい、それはいくつかの人々を阻止するかもしれないエントリに少し余分な障壁を追加します。
更新: CF7を使用しているというコメントを残したので、ネイティブCF7構文を次に示します。
settings.cfm:
<cfset variables.settings = StructNew() />
<cfset variables.settings.fooSection = StructNew() />
<cfset variables.settings.fooSection.fooKey = 'fooVal' />
<cfset variables.settings.fooSection.fooNumber = 2 />
<!--- ... --->
または、 JSONUtil およびCFSaveContentを使用して、JSONに見えるアプローチを引き続き使用できます(元の構文)、CF7:
<cfsavecontent variable="variables.jsonSettings">
{
fooSection = {
fooKey = 'fooVal',
fooNumber = 2,
},
fooSection2 = {
//...
},
fooSection3 = {
//...
}
};
</cfsavecontent>
<cfset variables.settings = jsonUtil.deserializeFromJSON(variables.jsonSettings) />
多くのアプリで使用するCFCを作成しました。初期化するときにiniファイルパスを指定すると、iniファイルに基づいて構造が作成されます。また、オプションで構造をフラットに保つか、iniファイルの[セクション]に基づいてサブ構造を作成します。その後、 getSetting()
メソッドを使用して個々のメソッドを取得するか、 getAllSettings()
を使用して構造全体を返すことができます。役に立つかもしれません。
<cfcomponent hint="converts INI file to a structure">
<cfset variables.settings=structNew() />
<cffunction name="init" access="public" output="false" returntype="any">
<cfargument name="configurationFile" type="string" required="yes" />
<cfargument name="useSections" default="false" type="boolean" />
<cfset var local=structNew() />
<cfif fileExists(arguments.configurationFile)>
<!--- Get the [sections] in the .INI file --->
<cfset local.sectionStruct=getProfileSections(arguments.configurationFile) />
<!--- Loop over each of these sections in turn --->
<cfloop collection="#local.sectionStruct#" item="local.item">
<cfset local.workingStruct=structNew() />
<cfloop list="#local.sectionStruct[local.item]#" index="local.key">
<!--- Loop over the keys in the current section and add the key/value to a temporary structure --->
<cfset local.workingStruct[local.key]=getProfileString(arguments.configurationFile,local.item,local.key) />
</cfloop>
<cfif arguments.useSections>
<!--- Copy the temporary structure to a key in the setting structure for the current section --->
<cfset variables.settings[local.item]=duplicate(local.workingStruct) />
<cfelse>
<!--- Append the temporary structure to the setting structure --->
<cfset structAppend(variables.settings,local.workingStruct,"yes") />
</cfif>
</cfloop>
<cfelse>
<cfthrow
message="Configuration file not found. Must use fully-qualified path."
extendedinfo="#arguments.configurationFile#"
/>
</cfif>
<cfreturn this>
</cffunction>
<cffunction name="getAllSettings" access="public" output="false" returntype="struct">
<cfreturn variables.settings>
</cffunction>
<cffunction name="getSetting" access="public" output="false" returntype="string">
<cfargument name="settingName" required="yes" type="string" />
<cfset var returnValue="" />
<cfif structKeyExists(variables.settings,arguments.settingName)>
<cfset returnValue=variables.settings[arguments.settingName] />
<cfelse>
<cfthrow
message="No such setting '#arguments.settingName#'."
/>
</cfif>
<cfreturn returnValue>
</cffunction>
</cfcomponent>