tomphp / container-configurator

Configure your application and the Dependency Injection Container (DIC) via config arrays or config files.
MIT License
20 stars 3 forks source link

When merging config there's no way to remove anything #79

Open tomphp opened 8 years ago

tomphp commented 8 years ago

Say you have these 2 configs:

$config1 = [
  'di' => [
    'services' => [
      'db' => [
        MySQLDB::class,
        'arguments' => [
          'username',
          'password',
          'hostname',
        ],
      ],
    ],
  ],
];
$config2 = [
  'di' => [
    'services' => [
      'db' => [
        FileDB::class,
        'arguments' => [
          'filename',
        ],
      ],
    ],
  ],
];

When loading the configs like so:

Configurator::apply()
  ->configFromArray($config1)
  ->configFromArray($config2)
  ->to($container);

You end up with a config definition like this:

$config2 = [
  'di' => [
    'services' => [
      'db' => [
        FileDB::class,
        'arguments' => [
          'filename',  // from $config2
          'password', // from $config1
          'hostname', // from $config1
        ],
      ],
    ],
  ],
];

Sometimes this is desired behaviour, sometimes it is not. I'm trying to decide if this is a problem and how it should be addressed.

Current options:

1. Treat the config as additive only?

Basically, claim that this isn't a problem and replacing services in this way is not the intended use case.

2. Add method to wrap specific sections

For example:

$config2 = [
  'di' => [
    'services' => [
      'db' => Configurator::replace([
        FileDB::class,
        'arguments' => [
          'filename',
        ],
      ]),
    ],
  ],
];

It's a decent idea but it won't work for non-PHP config files.

kuzmenko1256 commented 6 years ago

+1