Here to demo how to use Symfony Yaml Component to read Yaml file.
Install
composer require symfony/yaml
Parse Yaml file
#config.yaml
database:
hostname: 'localhost'
port: 3306
name: 'db_name'
username: 'db_username'
password: 'db_secret_password'
serviceTopic:
region: us-west-2
topic: arn:aws:sns:us-west-2:12345678:production-good-service-topic
//test.php
<?php
require "./vendor/autoload.php";
use Symfony\Component\Yaml\Yaml;
//$value will be an associate array
$value = Yaml::parseFile('./config.yaml');
var_dump($value);
//another way to parse file. $data and $value are identical
$data = \Symfony\Component\Yaml\Yaml::parse(file_get_contents( "./config.yaml"));
var_dump($data);
//after run: php test.php. Here is the result
array(2) {
["database"]=>
array(5) {
["hostname"]=>
string(9) "localhost"
["port"]=>
int(3306)
["name"]=>
string(7) "db_name"
["username"]=>
string(11) "db_username"
["password"]=>
string(18) "db_secret_password"
}
["serviceTopic"]=>
array(2) {
["region"]=>
string(9) "us-west-2"
["topic"]=>
string(60) "arn:aws:sns:us-west-2:12345678:production-good-service-topic"
}
}
No comments:
Post a Comment