Application analysis of Config mechanism based on Zend

  • 2020-06-01 09:14:53
  • OfStack

Zend Config class in Zend_Config_Ini

code
$config = new Zend_Config_Ini("/var/www/html/usvn/config/config.ini", "general");

date_default_timezone_set($config- > timezone);

USVN_ConsoleUtils::setLocale($config- > system- > locale);

===

Config.ini file content

[general]

url.base = "/usvn"

translation.locale = "zh_CN"

timezone = "Asia/Shanghai"


A concrete analysis
Here only the Zend_Config_Ini constructor is used, and we can see its s 48en.

The first is to determine if there is a configuration file. The second is to manage option, where option can be set with the allowModifications property (whether the property in the configuration file can be modified) and nestSeparator property (the key delimiter in the configuration file is the default point).

The following is a call to $iniArray = $this- > _loadIniFile ($filename); This function is very important because it parses the configuration file. Let's see what the data returned by _parseIniFile looks like in order to avoid clutter:


Array 
( 
    [general] => Array 
        ( 
            [url.base] => /usvn 
            [translation.locale] => zh_CN 
            [timezone] => Asia/Shanghai 
            [system.locale] => aa_DJ.utf8 
        ) 

) 

The last thing you parse out is a 2-dimensional array.

parseIniFile actually calls the system function parse_ini_file for processing. In particular, note 1 that it actually USES set_error_handler and restore_error_handler before and after calling parse_ini_file to expose the exception handling function. Since it is very easy to make errors when parsing a configuration file, and the user should be friendly enough to prompt the user to make changes there, Zend deliberately exposes error handling functions. If you want to design a friendly system, override method _loadFileErrorHandler in the inheritance class.

Keep looking at it from _loadIniFile

Since our ini configuration file USES [] to represent an setion, the key returned by the 2-dimensional array _loadIniFile is general. But in fact, if we use [general:123] as section in the configuration file, this function will return 123 as val [;extends]. That's true


Array 
( 
    [general] => Array 
        ( 
            [;extends] => 123 
            [url.base] => /usvn 
            [translation.locale] => zh_CN 
        ) 

) 

Now back to s 102en, iniArray has been obtained, which is a 2-dimensional array. Next, if you set s 104en to s 104en, iniArray will be processed _arrayMergeRecursive, which mainly means s 108en in s 107en. S 109en = > aa_DJ. utf8 into array (system = > array( locale= > aa_DJ. utf8)). options, nestSeparator, options, translation, locale, nestSeparator, nestSeparator, nestSeparator, translation, location =... I'm not going to keep chasing it, it's just going to be 1 string operations.

Finally, the dataArray looks like this


Array 
( 
    [url] => Array 
        ( 
            [base] => /usvn 
        ) 

    [translation] => Array 
        ( 
            [locale] => zh_CN 
        ) 

    [timezone] => Asia/Shanghai 
    [system] => Array 
        ( 
            [locale] => aa_DJ.utf8 
        ) 
) 

Next, call the constructor function of the parent class s s 135en, and the parent of Zend_Config_Ini is Zend_Config.


class Zend_Config implements Countable, Iterator

Zend_Config implements the Countable interface (including count() method), Iterator interface (including current, key, next, rewind, valid, etc.)

The constructor for Zend_Config puts the 2-dimensional array parsed above into _data.


I'm going to focus on two functions here

__set and __get

The magic method ensures that config- can be used > field gets the configuration value

The magic method of s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s = s


At this point, look at the demo code at the beginning of the article

date_default_timezone_set($config- > timezone);

And the reason why we can use -- > Instead of using config, timezone USES the properties of s 201en.
Zend's Config mechanism analysis ends.


Related articles: