Currently I'm using the Symfony YAML component, and my code is as follows (note that $blueprint is the data from the YAML file). It works, but I'm not sure of its elegance.
function update\\_blueprint($path, $data = []) {
// Get existing data
$blueprint = get\\\_blueprint($path);
// loop over the data and overwrite or create data
foreach($data as $key => $val) {
$blueprint->$key = $val;
}
$blueprint = (array) $blueprint;
$yaml = \Symfony\Component\Yaml\Yaml::dump($blueprint);
$data = '---' . "\n";
$data .= $yaml;
$data .= '---';
$file = EMILIA\\\_DIR . '/\\\_site/blueprints/' . $path . '.yml';
file\\\_put\\\_contents($file, $data);
return true;
}
j
stuff ;)
why return true ? if there is a fatal error it would be thrown anyway same goes for exceptions :)
it's a generic dump of a non typed object structure :) since it's a flat merge you could just cast
$blueprint = (array) $blueprint;$blueprint += (array) $data;which would be a union and you don't need a foreach
if you want to merge them recursive right over left
$blueprint = array\\\_merge\\\_recursive((array) $blueprint,(array) $data);I would however recommend the function signature to be
\\*\\*function update\\\_blueprint\\*\\* ($path, array $data = [])as well as adding a first check
if (!$data) { return false; } // or void throw an exception based on your error handling modelthat's just my superficial opinion based on what I saw :)