In the last article, we introduced a relatively well-known YAML format configuration file related operations, today I will learn another configuration file extension. The writing of this configuration file is actually similar to the PHP standard configuration format of php.ini, but there are some differences. But the content is very simple, for your reference only.
Yaconf configuration file and format
Did Yaconf see any clues from the name? That's right, like Yaf and Yac, it is also the work of our bird brother. I have to say that the Great God still contributed a lot of good works for us. We will talk about one of its niche open source extensions later, and for the Yaf extension, we will learn more in-depth when we learn about the framework in the future.
The installation of Yaconf is also a normal way of extended installation, but it requires PHP7 or above. In addition, after installation, you need to specify yaconf.directory in the php.ini file, which is the directory where the configuration file is stored. This attribute cannot be configured through ini_set(), that is, it must be loaded into the PHP runtime environment before the program runs. We configure it as /tmp/conf according to the instructions of the document, and then create the configuration file we need in this directory.
The syntax of Yaconf is very concise, and Bird's works are focused on performance, so Yaconf is also a high-performance configuration management extension. For the specific introduction of Yaconf, please refer to the description of the second link at the bottom of the article. Here we will look at some of its syntax and specific usage.
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 of all, if the content is double-quoted, the configuration variable will be treated as a string, if it is not double-quoted, it will try to parse it with PHP. Then the writing methods such as array and HashMap are also perfectly supported. It seems to be more powerful than php.ini. But it's more than that.
[parent]
parent="base"
children="NULL"
[children : parent]
children="children"
Well, you read that right, it can also support this kind of inheritance writing. The content marked in square brackets can be regarded as a configuration fragment, or a section of content. The specific function will be seen later.
Get configuration content
This is the configuration syntax. Next, we will take a look at how to read the configuration information. This extension actually provides two functions, one for reading and one for querying whether the configuration exists. Let's first 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 this function does not need to be explained, 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, then we can read it like this:
var_dump(Yaconf::get("foo.SectionA.key")); // string(3) "val"
For array configuration information, the content obtained directly is 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 directly use. To get the content of the sequence. The last is the function of fragmentation and inheritance 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 name of the shard we defined in square brackets, and then continue to click the name of the configuration item defined under the shard to get the configuration information content under this shard. The use of inheritance can be seen in the code. After the parent configuration item of parent is inherited by children, children can directly obtain the content of the configuration item defined in the parent without defining this configuration item. The children configuration item is rewritten in children, so the children configuration item in the children shard shows the content defined by itself.
Check whether the configuration information exists
As mentioned earlier, there are two methods in this extension. The second one is a method for detecting the existence of configuration items, which is very simple.
var_dump(Yaconf::has("test.foo")); // bool(true)
var_dump(Yaconf::has("test.baz")); // bool(false)
Summarize
To be honest, this configuration extension is not a very common extension application. Because the framework you are currently using, whether it is 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 system is based on Yaf, Yac, and Yar, then adding this Yaconf can be regarded as a complete set of high-performance internal expansion architecture. Their main features are powerful performance. After all, they are frameworks provided from the perspective of underlying C extensions, rather than using frameworks written in PHP through Composer. When we learn and explain the framework in the future, we might take it out and make a separate series!
Test code:
Reference documents:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。