Vars是一個易於使用、輕量級且易於擴展的配置載入器,具有適用於 ENV、INI、JSON、PHP、Toml、XML 和 YAML 檔案類型的內建載入器。它還內建了對 Silex 的支持,以及即將推出的更多框架(Symfony、Laravel 等)。
有時您被迫對設定檔使用不同的格式,而Vars目標之一是透過支援最常見的設定格式來讓這對您來說更簡單,這樣您就不必切換程式庫來處理不同的格式。
另一個目標是支援不同的框架,因此在處理不同的框架時您不必切換庫。目前僅使用服務提供者支援 Silex,很快就會支援 Laravel 和 Symfony。
透過簡單的 API 和直覺的載入選項, Vars嘗試使配置載入和提供盡可能簡單。
Vars需要 PHP 版本5.3+
。
如果您想使用 YAML,則需要symfony/yaml
函式庫,同樣,您需要yosymfony/toml
來使用 Toml 文件,需要m1/env
來使用 Env 檔案。
透過作曲家
$ composer require m1/ Vars
// load single file
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' );
// load from dir
$ Vars = new Vars ( __DIR__ . ' /config ' );
// load from array
$ Vars = new Vars ( array (
__DIR__ . ' /config/config.yml ' ,
__DIR__ . ' /config/sub ' ,
));
這可以透過多種方式完成,您可以將$ Vars
變數視為普通數組,也可以以物件導向的方式使用它
// All return the same thing
$ Vars -> get ( ' db.password ' )
$ Vars [ ' db.password ' ];
$ Vars [ ' db ' ][ ' password ' ]
也可以用同樣的方式設定值
// All do the same thing
$ Vars -> set ( ' db.password ' , ' test ' )
$ Vars [ ' db.password ' ] = ' test ' ;
$ Vars [ ' db ' ][ ' password ' ] = ' test ' ;
您也可以從getenv()
取得變數
// All do the same thing
$ Vars -> toEnv ();
getenv ( ' db.password ' );
有關這方面的更多信息,請檢查環境變數部分
您可以輕鬆地將配置相對和絕對導入到其他配置中,這些配置因配置文件類型而異,因此請檢查 /tests/mocks/ 資料夾中的範例
# example_1.yml
test_key_1 : test_value_1
imports : example_2.yml
# example_2.yml
test_key_2 : test_value_2
將返回:
[
" test_key_1 " => " test_value_1 " ,
" test_key_2 " => " test_value_2 "
]
預設情況下,導入是相對於金鑰導入的,例如:
test_key_1 :
imports : example_2.yml
將返回:
[
" test_key_1 " => [
" test_key_2 " => " test_value_2 "
]
]
但是,您可以透過多種方式變更此設定:
# example 1
test_key_1 :
imports :
- {resource: example.yml, relative: false}
# example 2
test_key_2 :
imports :
resource : example.yml
relative : false
如果匯入各種文件並且您想要設定所有文件的相對性,您可以執行以下操作:
test_key_1 :
imports :
relative : false
resource :
- example_2.yml
- example_3.yml
以上所有內容都會導致example_2.yml
和example_3.yml
變數對設定檔成為絕對變數:
[
" test_key_1 " => []
" test_key_2 " => " test_value_2 " // from example_2.yml
"test_key_3" => "test_value_3" // from example_3.yml
]
您也可以使用上述所有語法匯入目錄:
test_key_1 :
imports : sub/
預設情況下,導入目錄不是遞歸的,並且不會搜尋資料夾內的資料夾,您可以透過新增遞歸切換來變更此設定:
test_key_1 :
imports :
resource : sub/
recursive : true
或透過添加遞歸標誌:
test_key_1 :
imports :
resource : sub/*
與載入檔案一樣,您可以透過一個遞歸切換來批次匯入目錄:
test_key_1 :
imports :
recursive : false
resource :
- sub/
- sub1/
目錄的導入依賴於載入器和載入器支援的擴充功能。有關更多詳細信息,請參閱加載器部分。
匯入時您可以使用各種標誌。
if else 標誌?:
使得如果第一個文件存在,則使用該文件 - 否則使用其他定義的文件,例如:
imports : " example_1.yml ?: example_2.yml "
注意:您需要將字串括在引號中才能使 if else 標誌起作用
抑制異常標誌@
——抑製文件未找到異常。例如:
imports : @file_does_not_exist.yml
遞歸標誌使得在目錄中的目錄中搜尋檔案。例如:
imports :
resource : sub/*
您也可以組合上述標誌,因此如果 else 檔案選項不存在,則不會拋出異常,例如:
imports : " example_1.yml ?: @example_2.yml "
您可以獲得單獨的文件或資源:
// All return the same thing
$ Vars -> getResource ( ' example_2.yml ' )-> get ( ' test_key_2 ' );
$ Vars -> getResource ( ' example_2.yml ' )[ ' test_key_2 ' ];
Vars有多種選項
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
// this will affect how you getResource() and will default to the path
// of the first resource you initiate
' path ' => __DIR__ . ' /config ' ,
// to cache or not -- defaults to true
' cache ' => true ,
// where the cache is stored -- If not set will default to the base path
' cache_path ' => __DIR__ . ' /config/ ' ,
// How long the cache lasts for -- Defaults to 300 seconds (5 minutes)
' cache_expire ' => 300 ,
// Replacement variables -- see variables section for more detail
' replacements ' => [
' foo ' => ' bar ' ,
' foobar ' => ' barfoo '
],
// Merge globals -- see globals section for more detail
' merge_globals ' => true ,
// The file loaders to load the configs -- see loader section for more detail
' loaders ' => [
' default '
]
]);
path
是$ Vars ->getResource($filename)
中$filename
計算方式。例如:
如果您將path
設定為__DIR__.'/config'
並且匯入了__DIR__.'/app/test_1.yml'
:
# example_1.yml
imports : example_2.yml
則example_1.yml
和example_2.yml
$filename
將分別為../app/test_1.yml
和../app/test_1.yml
。
如果沒有設定path
,則第一個檔案資源路徑將用作path
,例如:
// example 1
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' );
// example 2
$ Vars = new Vars ([
__DIR__ . ' /config/config.yml ' ,
__DIR__ . ' /sub/config.yml ' ,
]);
都將使用__DIR__.'/config'
作為path
您可以在Vars
中使用 3 種類型的變數: Replacements
、 In-file
和Environment
,語法為:
變數類型 | 句法 |
---|---|
替換品 | %VARIABLE% |
文件內 | %$VARIABLE% |
環境 | %^VARIABLE% |
為了更好的可讀性,您還可以在變數名稱和前綴/後綴之間放置空格,如下所示:
replacement_variable : % VARIABLE %
infile_variable : %$ VARIABLE %
env_variable : %^ VARIABLE %
替換變數是從外部載入的Vars
,因此它通常用於PHP
函數/邏輯,例如__dir__
:
test_key_1 : %foo%
test_key_2 : /bar/%foobar%/bar
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
' replacements ' => [
' foo ' => ' bar ' ,
' foobar ' => ' barfoo '
],
]);
輸出:
[
" test_key_1 " => " bar " ,
" test_key_2 " => " /bar/barfoo/foobar/ "
]
您的替換內容必須以%
為前綴和後綴
您也可以從檔案載入變數:
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
' replacements ' => __DIR__ . ' /config/variables.yml '
]);
您也可以使用檔案中已定義的鍵中的變量,例如:
test_key_1 : hello
test_key_2 : /bar/%$test_key_1%/bar
輸出:
[
" test_key_1 " => " bar " ,
" test_key_2 " => " /bar/hello/foobar/ "
]
您的替換內容必須以%$
為前綴並以%
為後綴。
對於in-file
和replacements
,您可以使用點符號語法來取得數組,例如:
test_key_1 :
test_key_2 : hello
test_key_3 : /bar/%$test_key_1.test_key_2%/bar
輸出:
[
" test_key_1 " => array (
" test_key_2 " => " hello "
),
" test_key_2 " => " /bar/hello/foobar/ "
]
您也可以使用環境變數進行替換:
test_key_1 : %^DATABASE_USERNAME%
test_key_2 : %^DATABASE_PASSWORD%
# nginx config example
location @site {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root /index.php;
# env variables
fastcgi_param DATABASE_USERNAME test_username;
fastcgi_param DATABASE_PASSWORD test_password;
}
輸出:
[
" test_key_1 " => " test_username " ,
" test_key_2 " => " test_password "
]
您的環境變數必須以%^
為前綴並以%
為後綴
您也可以這樣做,以便您的配置數組可用於getenv()
:
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' );
$ Vars -> toEnv ();
注意:為此,您的配置將被展平為點表示法,例如:
test_key_1 :
test_key_2 : value
將由以下人員造訪:
getenv ( ' test_key_1.test_key_2 ' ); // value
Vars
中的Globals
指的是這樣定義的變數:
_globals :
test_key_1 : test_value_1
基本上它們只是封裝在一個_globals
數組中——使用它們是為了讓你可以從Vars
getGlobals()
存取它們
預設操作是將它們合併到其他文件內容中,以便:
_globals :
test_key_1 : test_value_1
test_key_2 : test_value_2
變成:
[
' test_key_1 ' => ' test_value_1 ' ,
' test_key_2 ' => ' test_value_2 ' ,
]
但您可以透過選項將merge_globals
變更為false
來覆寫此設定。
如果這沒有意義,那麼您可能根本不需要使用全域變量,但它們對於使用封裝了$app
下所有內容的框架非常有用,並且您希望能夠訪問一些鍵 => 值,例如所以: $app['test_key_1']
。有關更多範例,請參閱 Silex 提供者部分。
Vars會自動快取資源 5 分鐘,您可以透過將cache
選項設為false
來關閉此功能。
如果未設置,則將cache_path
設定為path
設定的內容。 cache_path
必須是可寫的。
要讓快取無效,只需刪除cache_path
中名為Vars
資料夾即可,例如: rm -rf /var/www/application/app/cache/ Vars
由於 opcache 的額外加速,快取檔案是一個 .php 檔案。
如果您使用 Silex 提供程序,則在調試模式下將不會使用和設定快取。
載入器使Vars能夠讀取不同的檔案類型(預設為 Ini、Json、Php、Toml、Xml 和 Yaml)。
您可以透過以下選項啟用和停用載入程式:
預設載入所有預設載入器:
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
' loaders ' => ' default '
]);
// You can load individual loaders:
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
' loaders ' => [
' ini ' ,
' json '
[
]);
//You can also create and load custom loaders:
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
' loaders ' => [
' FooBarCustomFooBarLoader ' ,
' ini ' ,
' json '
]
]);
要建立您自己的自訂載入程序,您必須擴展M1 Vars LoaderAbstractLoader
,在public static $supported
數組中具有受支援的擴展,並具有載入檔案內容的public function load()
。
這是載入 .txt 檔案的原始範例:
namespace M1 Foo Bar Loader ;
use M1 Vars Loader AbstractLoader ;
class TextLoader extends AbstractLoader
{
public static $ supported = array ( ' txt ' );
public function load ()
{
$ content = [];
foreach ( file ( $ this -> entity ) as $ line ) {
list ( $ key , $ value ) = explode ( ' : ' , $ line , 2 );
$ content [ trim ( $ key )] = trim ( $ value );
}
$ this -> content = $ content ;
return $ this ;
}
}
然後要使用這個載入器,您只需使用:
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
' loaders ' => [
' M1FooBarLoaderTextLoader ' ,
]
]);
注意:不要真正使用這個加載器,它純粹是為了演示目的
將此庫與 Silex 一起使用非常簡單,只需在註冊其他服務提供者時註冊它即可:
$ app -> register ( new M1 Vars Provider Silex Vars ServiceProvider ( ' example.yml ' ), [
' Vars .path ' => __DIR__ . ' /../../app/config/test/ ' ,
' Vars .options ' => [
' cache ' => true ,
' cache_path ' => __DIR__ . ' /../../app/config/cache/ ' ,
' cache_expire ' => 500 ,
' replacements ' => [
' test ' => ' test_replacement '
],
' loaders ' => [
' yml ' ,
' json '
],
' merge_globals ' => true ,
' replacements ' => __DIR__ . ' /../../app/config/replacements.json ' ,
]]);
然後你可以從$app[' Vars ']
存取你的配置
注意:如果$app['debug'] = true
那麼快取就不會被使用。
您也可以使用點符號從 $app 存取配置值,例如:
test_key_1 :
test_key_2 : value
test_key_3 : value
您可以使用點符號來得到上面的內容,如下所示:
$ app [ ' Vars ' ][ ' test_key_1.test_key_2 ' ]; // value
$ app [ ' Vars ' ][ ' test_key_3 ' ]; // value
您也可以將全域變數合併到$app
中,如下所示:
# example.yml
_globals :
monolog.logfile : log.log
test_key_1 : test_value_2
$ app -> register ( new M1 Vars Provider Silex Vars ServiceProvider ( ' example.yml ' ));
// register monolog here and other service providers
$ app [ ' Vars .merge ' ]();
請注意$app[' Vars .merge']()
——這會覆蓋服務提供者的預設設置,因此在此範例中monolog
將使用Vars配置中定義的日誌檔案。
在呼叫您在配置中為其提供配置值的服務提供者後,您必須呼叫Vars .merge
。
您也可以透過$app[' Vars .test_key_1']
存取test_key_1
,如果需要,您也可以類似地存取全域變數$app['monolog.logfile']
。
Vars ($resource, $options = array())
建立新Vars配置的建構子:
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
// this will affect how you getResource() and will default to the path
// of the first resource you initiate
' path ' => __DIR__ . ' /config ' ,
// to cache or not -- defaults to true
' cache ' => true ,
// where the cache is stored -- If not set will default to the base path
' cache_path ' => __DIR__ . ' /config/ ' ,
// How long the cache lasts for -- Defaults to 300 seconds (5 minutes)
' cache_expire ' => 300 ,
// Replacement variables -- see variables section for more detail
' replacements ' => [
' foo ' => ' bar ' ,
' foobar ' => ' barfoo '
],
// The file loaders to load the configs -- see loader section for more detail
' loaders ' => [
' default '
]
]);
getContent()
傳回所有配置的解析內容。
getResource($resource)
取得指定資源,傳回檔案資源,如果資源不存在則傳回 false。
$resource
名稱是基於基本路徑和檔案名稱中定義的路徑。
# example.yml
imports : example2.yml
test_1 : value
# example2.yml
test_2 : value
$ Vars = new Vars ( ' example.yml ' );
$ Vars -> getResource ( ' example2.yml ' ); // FileResource
$ Vars -> getResource ( ' example2.yml ' )-> getContent ();
# output:
# [
# "test_2" => "value"
# ]
getResources()
傳回所有匯入的資源,它們將是FileResource
物件。
toEnv()
使得配置可以透過getenv()
獲得:
$ Vars = new Vars ( ' example.yml ' );
$ Vars -> toEnv ();
getenv ( ' test_1 ' ); // value
toDots()
使配置扁平化為點表示法數組
test_value_1 :
test_value_2 : value
test_value_3 : value
$ Vars = new Vars ( ' example.yml ' );
$ Vars -> toDots ();
# output:
# [
# "test_value_1.test_value_2" => "value",
# "test_value_1.test_value_3" => "value
# ]
getGlobals()
取得_globals
中定義的值
set($key, $value)
設定配置鍵:
$ Vars = new Vars ( ' example.yml ' );
$ Vars -> set ( ' test_key_1 ' , ' value_2 ' );
get($key)
取得配置金鑰:
$ Vars = new Vars ( ' example.yml ' );
$ Vars -> get ( ' test_key_1 ' ); // value
getRawContent()
從文件中取得原始的、未解析的內容
# example.yml
test_value_1 :
imports : example2.yml
test_value_2 : %root%/foo/%dir%
$ Vars = new Vars ( ' example.yml ' );
$ Vars -> getResource ( ' example.yml ' )-> getRawContent ();
# output:
# [
# test_value_1:
# imports: example2.yml
# test_value_2: %root%/foo/%dir%
# ]
getContent()
請參閱 getContent()
get($key)
參見 get()
請參閱變更日誌以了解最近變更的更多資訊。
$ composer test
詳細資訊請參閱貢獻。
如果您發現任何與安全相關的問題,請發送電子郵件至 [email protected],而不是使用問題追蹤器。
麻省理工學院許可證 (MIT)。請參閱許可證文件以獲取更多資訊。