Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

How to overwrite adapter config? #108

Closed
Estigy opened this issue Jun 30, 2016 · 3 comments
Closed

How to overwrite adapter config? #108

Estigy opened this issue Jun 30, 2016 · 3 comments
Labels

Comments

@Estigy
Copy link

Estigy commented Jun 30, 2016

I have a cache configuration array (defined in some module config file) that is handed over to StorageFactory::factory():

array(
    'adapter' => array(
        'name' => 'filesystem',
        'options' => array(
            'cache_dir' => 'data/cache/navigation',
            'dir_level' => 0,
            'namespace' => '',
        ),
),

Now if I want to use another adapter on my local development machine, I insert the following code into my local.php

array(
    'adapter' => array(
        'name' => 'blackhole',
        ),
),

This overwrites the adapter name, but does not work: All the other, already given, options are being pushed to the blackhole adapter, but the corresponding methods like setCacheDir() don't exist there.
(The BlackHole adapter ist just an example, the problem would be the same if you had configured Memcache but want to use Filecache locally.)

So how do you correctly define adapters together with their options in your global config files so they still can be overwritten in local config files?

Thanks in advance, Estigy

@Estigy
Copy link
Author

Estigy commented Jun 30, 2016

As I see it there are to possible solutions to this: Either we only push those options to the adapter that the adapter can handle, or we push all options to the adapter but ignore the not-known methods (via an empty __call() method).

@weierophinney
Copy link
Member

zend-stdlib provides a way to remove configuration via the class Zend\Stdlib\ArrayUtils\MergeRemoveKey. As an example, in your local.php, you would write it as follows:

use Zend\Stdlib\ArrayUtils\MergeRemoveKey;

return [
    'caches' => [
    'cache-application' => [
        'adapter' => [
            'name' => 'blackhole',
            'options' => new MergeRemoveKey(),
        ],
    ],
    ],
];

When configuration merging occurs, if that key exists, ArrayUtils::merge() will now remove the entry.

@marc-mabe
Copy link
Member

Closing this as @weierophinney commented a working and clean solution

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants