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/*
เช่นเดียวกับการโหลดไฟล์ คุณสามารถนำเข้า dirs จำนวนมากได้ด้วยการสลับแบบเรียกซ้ำเพียงครั้งเดียว:
test_key_1 :
imports :
recursive : false
resource :
- sub/
- sub1/
การนำเข้าไดเรกทอรีอาศัยตัวโหลดและส่วนขยายที่ตัวโหลดรองรับ ดูส่วนตัวโหลดสำหรับรายละเอียดเพิ่มเติม
คุณสามารถใช้แฟล็กต่างๆ เมื่อนำเข้า
แฟล็ก if else ?:
ทำให้เป็นเช่นนั้นหากไฟล์แรกมีอยู่ ให้ใช้สิ่งนั้น -- 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
คือวิธีคำนวณ $filename
ใน $ Vars ->getResource($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
คุณสามารถใช้ตัวแปรได้ 3 ประเภทใน Vars
: 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 "
]
ตัวแปรสภาพแวดล้อมของคุณต้องนำหน้าด้วย %^
และต่อท้ายด้วย %
คุณยังสามารถทำให้มันอาร์เรย์ config ของคุณพร้อมใช้งาน 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
Globals
ใน Vars
อ้างถึงตัวแปรที่กำหนดดังนี้:
_globals :
test_key_1 : test_value_1
โดยพื้นฐานแล้วพวกมันจะถูกห่อหุ้มไว้ในอาร์เรย์ _globals
- การใช้สิ่งเหล่านี้เพื่อให้คุณสามารถเข้าถึงได้จาก getGlobals()
จาก Vars
การดำเนินการเริ่มต้นคือการรวมเข้ากับเนื้อหาไฟล์อื่นๆ เพื่อให้:
_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
ผ่านตัวเลือก
หากสิ่งนี้ไม่สมเหตุสมผล คุณอาจไม่จำเป็นต้องใช้ globals เลย แต่มันมีประโยชน์สำหรับการทำงานกับเฟรมเวิร์กที่สรุปทุกอย่างภายใต้คำว่า $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
ไฟล์แคชเป็นไฟล์ .php เนื่องจากการเร่งความเร็วของ opcache เป็นพิเศษ
หากคุณใช้ผู้ให้บริการ 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
คุณยังสามารถรวม globals เข้ากับ $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
หลังจากที่คุณโทรหาผู้ให้บริการที่คุณระบุค่าการกำหนดค่าไว้ในการกำหนดค่าของคุณ
คุณยังสามารถเข้าถึง test_key_1
ผ่าน $app[' Vars .test_key_1']
และในทำนองเดียวกันหากคุณต้องการ คุณสามารถเข้าถึง globals เช่นนั้น $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)
รับทรัพยากรที่ระบุ ส่งคืนทรัพยากรไฟล์ หรือเป็นเท็จหากไม่มีทรัพยากร
ชื่อ $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)
ดูรับ()
โปรดดู CHANGELOG สำหรับข้อมูลเพิ่มเติมว่ามีอะไรเปลี่ยนแปลงเมื่อเร็วๆ นี้
$ composer test
โปรดดูการมีส่วนร่วมเพื่อดูรายละเอียด
หากคุณพบปัญหาที่เกี่ยวข้องกับความปลอดภัย โปรดส่งอีเมลไปที่ [email protected] แทนการใช้ตัวติดตามปัญหา
ใบอนุญาตเอ็มไอที (MIT) โปรดดูไฟล์ใบอนุญาตสำหรับข้อมูลเพิ่มเติม