This project is actually not a new idea. This is a small tool I made in my first optimization project after coming to Weibo. It is called Weibo_Conf. But because Weibo_Conf is a Weibo extension, there are also Some other functions are specially customized for Weibo. Therefore, they are not suitable for direct open source.
With the release of PHP7, many new persistence types have been added, such as IS_IMMUTABLE_ARRAY, so I redeveloped Yaconf under PHP7 , open source for everyone to use.
Introduction
First of all, let’s talk about what this does.
I have seen many projects , using PHP files for configuration, there may be a dozen or even dozens of .php configuration files in a config directory, which contain various arrays, and even some dictionary files (such as Chinese/English Control) is also put into the configuration. This causes the parsing of the configuration file to consume a lot of performance (admittedly, using opcache can be better, but in fact there is still an execution process).
Except PHP There are also those using json and yaml. A common feature is that the readability of these configurations is relatively poor. In addition, they also require runtime analysis.
The config directory is often together with the code. First there is Security risks (there are often sensitive information in the configuration). Secondly, if the configuration and code belong to the same project, this will cause the modification of the configuration to go through the process of code online.
Some resource configuration files, such as mysql/memcache Configuration information, these contents should be transparent to development, and operation and maintenance can be directly responsible. However, if it is put into the code, if operation and maintenance wants to initiate some changes, development and cooperation must be modified to modify the configuration file and go online.
So, Yaconf is a tool created to solve these problems.
It uses a separate configuration directory (specified in yaconf.directory), not together with the code.
It handles all the configurations to be processed when PHP starts, and then these configurations will reside in memory and live and die with the life cycle of PHP. It avoids parsing the configuration file every time it is requested.
All The configuration contents are all immutable, which can reduce memory usage with the help of Fork's COW, and when accessing the configuration, there is almost no need for any memory copy, and there will be no unnecessary increase or decrease in the reference count
The most important thing is that after the configuration directory and code are separated, a configuration management background can be used to achieve unified management of the configuration.
It supports (for non-ZTS) reloading of configuration changes, that is to say, the configuration If there are changes (it is recommended to use mv instead of cp to change the configuration), it will reload and does not need to be restarted (the frequency of detection is controlled by yaconf.check_delay).
It supports a variety of configuration types, including strings , array, section, section inheritance, and you can also write PHP constants and environment variables directly in the configuration.
The most important thing is, it is very simple.
API
Yaconf only provides two methods,
mixed Yaconf::get(string $name, mixed $default = NULL)
This is to get a configuration, the name is the name of the configuration, generally speaking, if you have an ini file called foo.ini , then if $name uses foo, it will get all the contents in this file and return it in the form of an array. default is the default value returned when the configuration does not exist.
bool Yaconf::has(string $name)
This is to detect whether a configuration exists .
Yaconf configuration items
yaconf.directory
Configuration file directory, this configuration cannot be specified through ini_set because it must be determined when PHP is started.
yaconf.check_delay
How often (seconds) to detect file changes, if it is 0, it will not be detected, that is to say, if it is 0, file changes can only be reloaded by restarting PHP
Configured format
Yaconf uses ini files as configuration files. This is because I have always felt that ini is the most suitable for configuration files. It is in key-value format and is clear and readable.
Simple configuration writing It looks like this (all the following assumes that the name of the ini file is test):
foo="bar" phpversion=PHP_VERSION env=${HOME}
As shown above, we use quotation marks for general configurations. For those that are not quoted, we will try to use PHP constants. Explanation, that is to say, we can write PHP constants directly in the configuration.
In addition, you can also see that we can write environment variables directly in the configuration, such as the above env:
Yaconf::get("test.env"); //test是配置文件名字 //string(16) "/home/huixinchen"
As shown above, you can see that assuming that the value of foo can be accessed through the following code:
Yaconf::get("test.foo"); //test是配置文件名字
Yaconf also supports array type configuration, which is written as follows:
arr.0=1 arr.1=2
If it is For a continuous array, you can also write directly:
arr[]=1 arr[]=2
Then for the value of the array, you can get it through the following code:
Yaconf::get("test.arr");
This will get the arr array in the test configuration file. Of course You can also directly obtain a specific value in the array. For example, if you want to directly obtain the 0th element of the arr array in the test configuration file:
$arr = Yaconf::get("test.arr.0");
Yaconf also supports map type configuration, which is written as follows:
map.foo=bar map.bar=foo
;You can use semicolons to write comments
map2.foo.name=yaconf map2.foo.year=2015
The name value of the foo submap of map2 can be accessed in the following form:
Yaconf::get("test.map2.foo.name"); //test是配置文件名字
And, the configuration file can also be divided into Section, and section inheritance:
[parent] parent="base" children="NULL" [children : parent] children="children"
Please pay attention to the configuration section inheritance syntax children: (colon) parent, which means that the children section inherits all base configuration items. Then you define it in the children section The configuration with the same name as the parent section will overwrite the content defined in the parent.
The value of the children configuration in the childlren section can be accessed in the following form:
Yaconf::get("test.children.children"); //test是配置文件名字
样例
首先, 假设我们的所有的配置文件都放置在/tmp/yaconf中, 那么我们就需要在php.ini中增加如下配置:
yaconf.directory=/tmp/yaconf
这样yaconf在PHP启动的时候, 就会在这个目录下找所有的*.ini文件, 然后尝试处理他们. 这里要注意的是不支持多级目录, 也就是说, yaconf只会处理yaconf.directory内的*.ini文件, 不会处理子目录里面的(这主要是为了简单考虑, 因为有分节, 你就可以一个项目定义一个ini文件).
假设/tmp/yaconf下有俩个ini文件, 分别是:
foo.ini
name="yaconf" year=2015 features[]="fast" features.1="light" features.plus="zero-copy" features.constant=PHP_VERSION bar.ini
[base]
parent="yaconf" children="NULL" [children:base] children="set"
然后对于foo的内容:
php7 -r 'var_dump(Yaconf::get("foo"));' /* array(3) { ["name"]=> string(6) "yaconf" ["year"]=> string(4) "2015" ["features"]=> array(4) { [0]=> string(4) "fast" [1]=> string(5) "light" ["plus"]=> string(9) "zero-copy" ["constant"]=> string(9) "7.0.0-dev" } } */
对于bar的内容:
php7 -r 'var_dump(Yaconf::get("bar"));' /* array(2) { ["base"]=> array(2) { ["parent"]=> string(6) "yaconf" ["children"]=> string(4) "NULL" } ["children"]=> array(2) { ["parent"]=> string(6) "yaconf" ["children"]=> string(3) "set" } } */
当然你可以用 (.)链接语法精确访问任何一个特定的值.
最后
我的Ya系列扩展从此又多了一个新成员, 算上之前的Yaf(PHP框架), Yar(PHP RPC框架), Yac(PHP单机缓存), 大家就可以很容易搭建一套高性能的LAMP应用解决方案出来.
注: Yaconf要求PHP7才能用
推荐教程:《PHP7》
The above is the detailed content of Yaconf usage tutorial for PHP7. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)
