zendframework / zend-config

Config component from Zend Framework
BSD 3-Clause "New" or "Revised" License
90 stars 38 forks source link

Allow processing class constants #37

Closed weierophinney closed 7 years ago

weierophinney commented 7 years ago

This patch updates the Constant processor to allow processing class constants, including the ::class pseudo-constant; additionally, it ensures that both the Token and Constant processors can apply to Config keys as well as the values by calling their enableKeyProcessing() methods.

To allow this to work, I've changed the visibility of Token::doProcess() from private to protected, to allow overriding the method within the Constant class. I've also added the new test case ZendTest\Config\Processor\Constant to cover the behavior.

Essentially, this allows the following to work (assuming the provided classes and constants exist):

{
    "Acme\\Component::CONFIG_KEY": {
        "host": "Acme\\Component::CONFIG_HOST",
        "dependencies": {
        "Acme\\Foo::class": "Acme\\FooFactory::class",
        }
    }
}

Via:

$config = new Config(Factory::fromFile('config.json'), true);
$processor = new Processor\Constant();
$processor->enableKeyProcessing();
$processor->process($config);
Ocramius commented 7 years ago

I really don't like the fact that a string may now possibly change meaning with a version upgrade.

This should be mentioned as BC Break because of that.

Still, feature is awesome and worth pursuing.

weierophinney commented 7 years ago

This should be mentioned as BC Break because of that.

I'll update the class to accept a flag for enabling the behavior instead, and have that disabled by default; that way we can introduce the feature without requiring a major version bump.

weierophinney commented 7 years ago

Removed BC break flag, as I've made the key processing opt-in at this time.

weierophinney commented 7 years ago

There are already three optional constructor arguments; I can add it, but I think the method call also makes sense.

On Feb 17, 2017 12:10 PM, "Marco Pivetta" notifications@github.com wrote:

@Ocramius requested changes on this pull request.

In doc/book/processor.md https://github.com/zendframework/zend-config/pull/37#discussion_r101813442 :

@@ -107,6 +131,14 @@ echo $config->foo;

This example returns the output: Value is TOKEN,Value is bar.

+As of version 3.1.0, you can also tell the Constant processor to process keys: + +```php +$processor->enableKeyProcessing();

A constructor argument would be preferrable

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/zendframework/zend-config/pull/37#pullrequestreview-22556617, or mute the thread https://github.com/notifications/unsubscribe-auth/AABlV9KiOQSMjijzwMQSm_XP951GLE78ks5rdeKhgaJpZM4MEdNe .