hassankhan / config

Config is a lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files
MIT License
972 stars 136 forks source link

Support for merging duplicate keys in a config file #89

Closed jhalterman closed 7 years ago

jhalterman commented 7 years ago

I'm wondering if you'd consider adding support for merging duplicate keys in a config file. My use case for this is wanting to have multiple environment config sections in a file since it makes config easier to organize by functionality. Example:

# Messaging config
dev:
  msg-host: foo

prod:
  msg-host: bar

# Database config
dev:
  db-host: foo

prod:
  db-host: bar

Ideally, this would be merged when loaded to:

dev:
  msg-host: foo
  db-host: foo

prod:
  msg-host: bar
  db-host: bar

While I could just write my config fully merged by environment, with a lot of config and multiple environments it's easier to maintain when it's organized by functionality.

DavidePastore commented 7 years ago

Hi @jhalterman, sorry for the late reply. This is not supported because the yaml config reader (Symfony Yaml component) takes the last value of the key and overwrites the old one. The specification writes:

JSON's RFC4627 requires that mappings keys merely “SHOULD” be unique, while YAML insists they “MUST” be. Technically, YAML therefore complies with the JSON spec, choosing to treat duplicates as an error. In practice, since JSON is silent on the semantics of such duplicates, the only portable JSON files are those with unique keys, which are therefore valid YAML files.

Personally I'd prefer to use different files for different environments,