用CFML覆盖INI文件

时间:2009-10-27 19:23:14

标签: coldfusion initialization

有人能找到一种方法来改进这段代码吗?我想在一个感觉猛扑中读入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>

3 个答案:

答案 0 :(得分:4)

我不相信你可以使用CFML电源来改善这一点。你需要解析巨大的ini文件吗?如果没有,为什么你要改进你的代码,对我来说它看起来非常简单。

其他可能的(虽然CF很常见)解决方案是尝试纯Java。请参阅此SO线程以获取纯Java examples

P.S。顺便说一句,如果有特殊性能需求,您应该考虑使用另一个存储进行配置。对于大型数据集,对旧的MySQL的简单SELECT查询可以快得多。

答案 1 :(得分:1)

要扩展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) />

答案 2 :(得分:1)

我创建了一个我在一堆应用中使用的CFC。在初始化时为它提供一个ini文件路径,它会根据ini文件创建一个结构。它还可以选择保持结构平坦或根据ini文件中的[Sections]创建子结构。然后,您可以使用其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>