In the last article, we introduced the operations related to a well-known YAML format configuration file. Today I'll learn another configuration file extension. The writing method of this configuration file is actually similar to the PHP standard configuration format of php.ini, but there are some differences. However, the content is very simple for your reference only.
Yaconf configuration file and format
Did Yaconf see anything from the name? Yes, like Yaf and Yac, it is also the work of our bird brother. I have to say that the great God has contributed a lot of good works to us. Later, we will talk about its niche open source extension, and we will further study the Yaf extension when we learn the framework in the future.
The installation of Yaconf is also a common way to extend the installation, but it requires a version above PHP7. In addition, after installation, you need to specify yaconf.directory in the php.ini file, that is, the directory where the configuration file is stored. This attribute cannot pass ini_set() is configured, that is, it must be loaded into the PHP running environment before the program runs. We configure it as / tmp/conf according to the instructions in the document, and then create the required configuration file in this directory.
Yaconf's syntax is very concise. Brother bird's works focus on performance, so yaconf is also a high-performance configuration management extension. For the specific introduction of yaconf, you can see the description of the second link at the bottom of the article. Here we'll look at its syntax and specific use.
foo="bar" phpversion=PHP_VERSION env=${HOME} arr.0=1 arr.1=2 arr[]=3 arr[3]=4 map.foo=bar map.bar=foo map.foo.name=yaconf
Do you see any characteristics? First, if the content is in double quotation marks, the configuration variable will be treated as a string. If it is not in double quotation marks, it will be parsed in PHP. Then array and HashMap are perfectly supported. It seems to be stronger than php.ini. But more than that.
[parent] parent="base" children="NULL" [children : parent] children="children"
Well, you're right. It can also support such inheritance writing. The contents indicated in brackets can be regarded as a configuration fragment or a section. We'll see the specific functions later.
Get configuration content
That's the configuration syntax. Next, we'll see how to read these configuration information. This extension actually provides two functions, one for reading and the other for querying whether the configuration exists. Let's take a look at how to read data.
var_dump(Yaconf::get("test.foo")); // string(3) "bar" var_dump(Yaconf::get("test.phpversion")); // string(5) "7.4.4" var_dump(Yaconf::get("test.env")); // string(5) "/root"
I believe there is no need to explain this function. Test is our file name, that is, in the file / tmp/conf/test.ini, we write the above test configuration information in this configuration. Of course, we can also define more configuration files in this directory. For example, if we define another configuration file foo.ini, we can read it as follows:
var_dump(Yaconf::get("foo.SectionA.key")); // string(3) "val"
For the array configuration information, the contents directly obtained are returned in array format.
var_dump(Yaconf::get("test.arr")); // array(4) { // [0]=> // string(1) "1" // [1]=> // string(1) "2" // [2]=> // string(1) "3" // [3]=> // string(1) "4" // } var_dump(Yaconf::get("test.arr.1")); // string(1) "2" var_dump(Yaconf::get("test.map")); // array(2) { // ["foo"]=> // array(1) { // ["name"]=> // string(6) "yaconf" // } // ["bar"]=> // string(3) "foo" // } var_dump(Yaconf::get("test.map.foo.name")); // string(6) "yaconf"
When getting the data inside the array, we can directly use. To get the contents of the sequence. Finally, there are the sharding and inherited functions mentioned above.
var_dump(Yaconf::get("test.parent.parent")); // string(4) "base" var_dump(Yaconf::get("test.children.parent")); // string(4) "base" var_dump(Yaconf::get("test.parent.children")); // string(4) "NULL" var_dump(Yaconf::get("test.children.children")); // string(8) "children"
test is the file name, and parent is the partition name defined in square brackets. Then continue to click the name of the configuration item defined under the partition to obtain the configuration information under the partition. The use of inheritance can be seen from the code. After the parent configuration item of a parent is inherited by children, children can directly obtain the content of the configuration item defined in the parent without defining the configuration item. The children configuration item is rewritten in children, so the children configuration item in the children partition displays its own defined content.
Check whether the configuration information exists
As mentioned earlier, there are only two methods in this extension. The second one is a very simple method for detecting the existence of configuration items.
var_dump(Yaconf::has("test.foo")); // bool(true) var_dump(Yaconf::has("test.baz")); // bool(false)
summary
To be honest, this configuration extension is not a very common extension application. Because the frameworks you are currently using, whether Laravel or TP, will have their own set of configuration file formats and operations. Of course, if you are a loyal fan of bird brother or your company's system is based on Yaf, Yac and Yar, then adding Yaconf can be regarded as a complete set of high-performance internal extension architecture. Their main features are strong performance. After all, they are frameworks provided from the perspective of underlying C extensions, rather than frameworks written in PHP through Composer. When we study and explain the framework in the future, we may take it out and make a series alone!
Test code:
Reference documents: